PHP導(dǎo)出Excel的類庫—PHPExcel,支持附帶圖片PHP導(dǎo)出Excel比較簡單的辦法有: 一、直接通過PHP的header來導(dǎo)出Excel。 01. header( "Content-type:application/vnd.ms-excel" ); 02. header( "Content-Disposition:attachment;filename=excel_data.xls" ); 03. 04. $tx = 'HEADER' ; 05. echo $tx . "\n\n" ; 06. //輸出內(nèi)容如下: 07. echo "Name" . "\t" ; 08. echo "Age" . "\t" ; 09. echo "\n" ; 10. echo "lilei" . "\t" ; 11. echo "25" . "\t" ; 二、采用Excel_XML類。 Excel_XML是一個簡單的PHP導(dǎo)出XML格式Excel的類。 下載地址:http://code.google.com/p/php-excel/downloads/list 使用方法: 01. // load library 02. require 'php-excel.class.php' ; 03. 04. // create a simple 2-dimensional array 05. $data = array ( 06. 1 => array ( 'Name' , 'Surname' ), 07. array ( 'Schwarz' , 'Oliver' ), 08. array ( 'Test' , 'Peter' ) 09. ); 10. 11. // generate file (constructor parameters are optional) 12. $xls = new Excel_XML( 'UTF-8' , false, 'My Test Sheet' ); 13. $xls ->addArray( $data ); 14. $xls ->generateXML( 'my-test' ); 以上兩種是比較簡單的導(dǎo)出Excel的方法,但功能不夠強(qiáng)大,只能滿足一般的需求。 這里介紹一個功能強(qiáng)大的PHP導(dǎo)出Excel的類庫——PHPExcel 。 PHPExcel支持Excel附帶圖片,支持Excel 2003、Excel 2007、PDF、HTML等。。?;谧杂绍浖腛penXML標(biāo)準(zhǔn)開發(fā)。 下載地址:1、本文附件 使用示例: 01. require_once './Classes/PHPExcel.php' ; 02. require_once './Classes/PHPExcel/IOFactory.php' ; 03. $objPHPExcel = new PHPExcel(); 04. $objPHPExcel ->getProperties()->setCreator( "Maarten Balliauw" ) 05. ->setLastModifiedBy( "Maarten Balliauw" ) 06. ->setTitle( "Office 2007 XLSX Test Document" ) 07. ->setSubject( "Office 2007 XLSX Test Document" ) 08. ->setDescription( "Document for Office 2007 XLSX, generated using PHP classes." ) 09. ->setKeywords( "office 2007 openxml php" ) 10. ->setCategory( "Test result file" ); 11. 12. $objPHPExcel ->setActiveSheetIndex(0); 13. $objRichText = new PHPExcel_RichText(); 14. $objRichText ->createText( '' ); 15. $objPayable = $objRichText ->createTextRun( 'PHP導(dǎo)出的Excel' ); 16. $objPayable ->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_RED )); 17. $objPayable ->getFont()->setBold(true); 18. $objPayable ->getFont()->setSize(24); 19. $objPHPExcel ->getActiveSheet()->getCell( 'B1' )->setValue( $objRichText ); 20. $objPHPExcel ->getActiveSheet()->getStyle( 'B1' )->getFont()->setBold(true); // 加粗 21. $objPHPExcel ->getActiveSheet()->getStyle( 'B1' )->getFont()->setSize(24); // 字體大小 22. $objPHPExcel ->getActiveSheet()->getStyle( 'B1' )->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); // 文本顏色 23. $objPHPExcel ->getActiveSheet()->getStyle( 'A1:G1' )->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); 24. $objPHPExcel ->getActiveSheet()->getStyle( 'A1:G1' )->getFill()->getStartColor()->setARGB( '00FFFFE3' ); // 底紋 25. 26. // 列寬 27. //$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true); 28. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'A' )->setWidth(12); 29. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'B' )->setWidth(18); 30. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'C' )->setWidth(12); 31. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'D' )->setWidth(18); 32. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'E' )->setWidth(12); 33. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'F' )->setWidth(18); 34. $objPHPExcel ->getActiveSheet()->getColumnDimension( 'G' )->setWidth(20); 35. 36. // 行高 37. for ( $i = 2; $i <= 17; $i ++) 38. { 39. $objPHPExcel ->getActiveSheet()->getRowDimension( $i )->setRowHeight(22); 40. } 41. 42. $objPHPExcel ->getActiveSheet()->setCellValue( 'A2' , '姓名' ); 43. $objPHPExcel ->getActiveSheet()->setCellValue( 'A3' , 'PHP' ); 44. $objPHPExcel ->getActiveSheet()->setCellValue( 'A4' , 'EXCEL' ); 45. $objPHPExcel ->getActiveSheet()->setCellValue( 'A5' , '' ); 46. $objPHPExcel ->getActiveSheet()->setCellValue( 'A6' , 'XXX' ); 47. $objPHPExcel ->getActiveSheet()->setCellValue( 'A7' , 'V' ); 48. $objPHPExcel ->getActiveSheet()->setCellValue( 'A8' , 'd' ); 49. $objPHPExcel ->getActiveSheet()->setCellValue( 'A9' , 'name' ); 50. $objPHPExcel ->getActiveSheet()->setCellValue( 'A10' , '描述' ); 51. for ( $i = 2; $i <= 16; $i ++) 52. { 53. $objPHPExcel ->getActiveSheet()->getStyle( 'A' . $i )->getFont()->setBold(true); // 加粗 54. } 55. 56. $objPHPExcel ->getActiveSheet()->setCellValue( 'B2' , '[姓名]' ); 57. $objPHPExcel ->getActiveSheet()->setCellValue( 'B3' , '導(dǎo)出帶圖片的EXCEL' ); 58. $objPHPExcel ->getActiveSheet()->setCellValue( 'B4' , 'XXXXX' ); 59. 60. // 為excel加圖片 61. $objDrawing = new PHPExcel_Worksheet_Drawing(); 62. $objDrawing ->setName( 'Photo' ); 63. $objDrawing ->setDescription( 'Photo' ); 64. $objDrawing ->setPath( './images/PIC.jpg' ); 65. $objDrawing ->setHeight(170); 66. $objDrawing ->setWidth(120); 67. $objDrawing ->setCoordinates( 'G2' ); 68. $objDrawing ->setWorksheet( $objPHPExcel ->getActiveSheet()); 69. 70. $objPHPExcel ->getActiveSheet()->setCellValue( 'A17' , '其它要求' ); 71. $objPHPExcel ->getActiveSheet()->setCellValue( 'B17' , '[其它要求]' ); 72. 73. $objPHPExcel ->getActiveSheet()->getStyle( 'A17' )->getFont()->setBold(true); // 加粗 74. 75. $objPHPExcel ->setActiveSheetIndex(0); 76. $objPHPExcel ->getActiveSheet()->setTitle( 'Excel表' ); 77. $objWriter = PHPExcel_IOFactory::createWriter( $objPHPExcel , 'Excel5' ); 78. $objWriter ->save( 'PHP導(dǎo)出Excel.xls' ); 附件下載: |
|