phpexcel類別函式庫的基本用法,phpexcel入門範例大全

WBOY
發布: 2016-07-25 08:51:36
原創
946 人瀏覽過
  1. //寫入excel檔案

  2. //包含類別
  3. require_once('Classes/ PHPExcel. php');
  4. require_once('Classes/PHPExcel/Writer/Excel2007.php');
  5. $objPHPExcel = new PHPExcel();
  6. //設定properties 設定檔案屬性

  7. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  8. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Ballia9");setTitle("Office 2007 XLSX 測試文件");
  9. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX 測試文件");
  10. $objPHPExcel-get>Properties() "Office 2007 XLSX 的測試文檔,使用PHP 類別產生。");
  11. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  12. $objPHPExcel-get>Properties( setCategory("測試結果檔");
  13. //加入一些資料加入資料

  14. $ objPHPExcel->setActiveSheetIndex(0);
  15. $objPHPExcel->getActiveSheet()-> setCellValue('A1', 'Hello');//可以指定位置
  16. $objPHPExcel->getActiveSheet() ->setCellValue('A2', true);
  17. $objPHPExcel->getActiveSheet()->setCellValue ('A3', false);
  18. $objPHPExcel->getActiveSheet()->setCellValue ('B2', '世界!');
  19. $objPHPExcel->getActiveSheet()->setCellValue('B3', 2);
  20. $objPHPExcel->getActiveSheet()->setCellValue( 'C1', 'Hello');
  21. $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');
  22. //迴圈

  23. for($i = 1;$i$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i);
  24. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, '測試值');
  25. }
  26. //日期整理

  27. $objPHPExcel->getActiveSheet()->setCellValue('D1', time());
  28. $objPHPExcel-> getActiveSheet()->getStyle ('D1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
  29. //新增註解

  30. $objPHPExcel-geteCommet(M 'E11')->setAuthor('PHPExcel');
  31. $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11') ->getText()->createTextRun('PHPExcel:');
  32. $objCommentRichText->getFont()->setBold(true);
  33. $objPHPExcel->getActiveSheet()-> getComment('E11')->getText()->createTextRun("rn");
  34. $objPHPExcel->getActiveSheet()->getComment('E11')->getText()-> ;createTextRun('目前發票總金額,不含增值稅。');
  35. //新增富文本字串新增文字可設定樣式

  36. $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18') );
  37. $objRichText->createText('這張發票是');
  38. $objPayable = $objRichText->createTextRun('月底後三十天內付款');
  39. $objPayable->getFont()->setBold(true);
  40. $objPayable- >getFont()->setItalic(true) ;
  41. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
  42. $objRich>create"有說明。 ');< ;/p>
  43. //合併單元格合併分離單元格

  44. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
  45. $objPHPExcel- > getActiveSheet()->unmergeCells('A18:E22');
  46. //保護單元格保護單元格

  47. $objPHPExcel->getActiveSheet()->getProtection( )-> setSheet(true);//需要設定為true 才能啟用任何工作表保護!
  48. $objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel' );
  49. //設定單元格數字格式數字格式化

  50. $objPHPExcel ->getActiveSheet()->getStyle('E4')->getNumberFormat()- >setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  51. $objPHPExcel->getActivelicpcel->getp-Sexcel-v >getStyle('E4'), 'E5:E13' ) ;
  52. //設定列寬度設定列寬度

  53. $objPHPExcel->getActiveSheet()->getColumnDimension('B' )->setAutoSize(true);
  54. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  55. //設定字體

  56. $objPHPExcel ->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
  57. $objPHPExcel->getActiveSheet()->getStyle('B1' )-> getFont()->setSize(20);
  58. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
  59. $objPHPExcel->getActiveSheet() ->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  60. $objPHPExcel->getActiveSheet()->setUnderline(PHPExcel_Style_Font::oPH-SINGA); getActiveSheet()-> getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  61. //設定對齊方式設定對齊

  62. $objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizo​​​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  63. $objPHPHPExcel-getS8 ')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  64. $objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment(-Pexim)d-Pic VERTICAL_CENTER);
  65. $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setWrapText(true);
  66. //設定列邊框已知欄位

  67. $objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border) ::BORDER_THIN);getStyle('A10')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  68. $objPHPExcel->getActiveSheet()->getStyle('E10')( )->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  69. $objPHPExcel->getActiveSheet ()->getStyle('D13')->getorderLes()-> :BORDER_THICK);
  70. $objPHPExcel->getActiveSheet()->getStyle('E13')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  71. //設定邊框顏色熟悉顏色
  72. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->getColor()-> ;setARGB('FF993300');
  73. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB ('FF993300');
  74. $objPHPExcel->getActive ()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB(' FF993300');
  75. $objPHPExcel->getActiveSheet()->getStyle('E13 ')->getBorders()->getRight()->getColor()->setARGB('FF993300' );
  76. //設定填滿設定填入

  77. $objPHPExcel->getActiveSheet ()->getStyle('A1')->getFill()->setFillType (PHPExcel_Style_Fill::FILL_SOLID);
  78. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()-> getStartColor()->setARGB('FF808080');
  79. //為工作表新增超連結新增連結

  80. $objPHPExcel->getActiveSheet()->setCellValue('E26' , 'www.phpexcel.net');
  81. $objPHPExcel->getActiveSheet()->getCell('E26')->getHyperlink()->setUrl('http://www.phpexcel.net');
  82. $objPHPExcel- >getActiveSheet()->getCell('E26')->getHyperlink()->setTooltip('導航到網站');
  83. $objPHPExcel->getActiveSheet()->getStyle('E26 ) ')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  84. //在工作表中加入繪圖新增圖片

  85. $objDrawing =

    //在工作表中加入繪圖新增圖片

  86. $objDrawing = new PHPDceling = newHet_heet_Works_heet_heet_ );
  87. $objDrawing->setName('Logo');
  88. $objDrawing->setDescription('Logo');
  89. $objDrawing->setPath('./images/officelogo.jpg ');
  90. $objDrawing->setHeight(36);
  91. $objDrawing->setCoordinates('B15');
  92. $objDrawing->setOffsetX(110);
  93. $objDrawing-setRotation(
  94. $objDrawing->getShadow()->setVisible(true);
  95. $objDrawing->getShadow()->setDirection(45);
  96. $objDrawing ->setWorksheet($objPHPExcel-get>Sheet(objDraweet( );
  97. //嘗試插入和刪除行和列

  98. $objPHPExcel->getActiveSheet()- >insertNewRowBefore(6, 10);
  99. $objPHPExcel->getActiveSheet() ->removeRow(6, 10);
  100. $objPHPExcel->getActiveSheet()->insertNewColumnBefore('E', 5);
  101. $objPHPExcel->getActiveSheet()->removemove ;
  102. //新增條件格式

  103. $objConditional1 = new PHPExcel_Style_Conditional();
  104. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);$objConditional1->setCondition('0 ') ;
  105. $objConditional1->getStyle()->getFont()->getCol_m)-> ;
  106. $objConditional1->getStyle()->getFont ()->setBold(true);
  107. //設定autofilter自動過濾

  108. $objPHPExcel->getActiveSheet()- >setAutoFilter('A1:C9') ;
  109. //隱藏「電話」與「傳真」欄位

  110. $objPHPExcel->getActiveSheet()->getColumnDimension('C')- >setVisible (false);
  111. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
  112. //設定文件安全性文件安全性

  113. $objPHPExcel->getSecurity()->setLockWindows(true);
  114. $objPHPExcel->getSecurity()->setLockStructure(true);
  115. $objPHPExcel->getSecurity( )->WosetWocelbook( );
  116. //設定工作表安全性

  117. $objPHPExcel->getActiveSheet()->getProtection()- >setPassword('PHPExcel');
  118. $objPHPExcel->getActiveSheet ()->getProtection()->setSheet(true);// 應該啟用此功能才能啟用以下任何功能!
  119. $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
  120. $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);getActiveSheet()->getProtection()->setFormatCells(true);
  121. //計算資料計算

  122. echo 'B14的值[ =COUNT(B2:B12)]: ' . $objPHPExcel->getActiveSheet()->getCell('B14')->getCalculatedValue() 。 “rn”;
  123. //設定大綱層級

  124. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
  125. $objPHPExcel->getActiveSheet()->getActiveSheet()-> setOutlineLevel(1);
  126. $objPHPExcel->getActiveSheet()-> ;getColumnDimension('E')->setVisible(false);
  127. $objPHPExcel->getActiveSheet()->getColumn' >setCollapsed(true);
  128. //凍結窗格

  129. $objPHPExcel->getActiveSheet()->freezePane('A2');
  130. //在頂部重複的行

  131. $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1);
  132. //設定資料驗證驗證輸入值

  133. $ objValidation = $objPHPExcel->getActiveSheet()->getCell('B3')->getDataValidation();
  134. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE ); PHPExcel_Cell_DataValidation::STYLE_STOP );
  135. $objValidation->setAllowBlank(true);
  136. $objValidation->setShowInputMessage(true);
  137. $objValidation->setShowInputMessage(true);
  138. $objValidation->setSashom-Message(trueE)-Ladm-Tad-uset-Tun​​rd-Message(Moo​​pet-Mr.> ('輸入錯誤');
  139. $objValidation->setError('數字不允許!');
  140. $objValidation->setPromptTitle('允許輸入');
  141. $objValidation->setPrompt('僅僅允許使用10 到20 之間的數字。 ');
  142. $objValidation->setFormula1(10);
  143. $objValidation->setFormula2(20);
  144. $objPHPExcel->getActiveSheet()->getCell('B3')->setDataValidation($$ objValidation);
  145. //建立一個新的工作表,預設工作表建立後新的工作標籤

  146. $objPHPExcel->createSheet();
  147. $objPHPExcel->setActiveSheetIndex (1);
  148. //設定頁首和頁尾。當奇數/偶數沒有使用不同的標頭時,假定為奇數標頭。 頁眉頁腳

  149. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&C&H請將此文件視為機密!');
  150. $objPHPExcel->getActiveSheet()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeader()->setOddHeD()->setOddHeader()->setOddHea()-> '&C&H請將此文件視為機密! ');
  151. $objPHPExcel->getActiveSheet()-> ;getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');
  152. //設定頁面方向與大小方向大小

  153. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Works::heet_PageSetSetSCA );
  154. $objPHPExcel->getActiveSheet()->getPageSetup ()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
  155. /重新命名工作表重新命名工作表標籤>$objPHPExcel->getActiveSheet()->setTitle( 'Simple');

  156. //將活動工作表索引設定為第一個工作表,以便Excel 將其作為第一個工作表開啟

  157. $objPHPExcel->setActiveSheetIndex(0);
  158. //儲存Excel 2007 檔案儲存

  159. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); objWriter->save(str_replace('.php', '. xlsx', __FILE__));
  160. //儲存Excel 5 檔案儲存
  161. require_once('Classes/PHPExcel/Writer/Excel5.php' );
  162. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel) ;
  163. $objWriter->save(str_replace('.php', '.xls', __FILE__));
  164. //1.6.2新版保存

  165. require_once ('Classes/PHPExcel/IOFactory.php');
  166. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');讀excel
  167. //包含類別

  168. require_once('Classes/PHPExcel/Reader/Excel2007. php');
  169. $objReader = new PHPExcel_Reader_Excel2007;
  170. $objPHPExcel = $objReader->load("05featuredemo.xlsx");
  171. Readercsvrequire_once('Classes/PHPExcel/Writer/CSV.php');
  172. require_once('Classes/PHPExcel/Reader/CSV.php');
  173. require_once('Classes /PHPExcel/Writer/ Excel2007.php');
  174. //寫入CSV格式寫
  175. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  176. $objWriter->setDelimiter('🎜>$objWriter->setDelimiter('🎜); >$objWriter- >setEnclosure('');
  177. $objWriter->setLineEnding("rn");
  178. $objWriter->setSheetIndex(0);
  179. $objWriter->save(str_replace('. php', '.csv', __FILE__));
  180. //從CSV 格式讀取讀取
  181. $objReader = new PHPExcel_Reader_CSV();
  182. $objReader->setDelimiter(';');$objReader->setEnclosure('');
  183. $objReader->setLineEnding("rn");
  184. $objReader->setSheetIndex(0);
  185. $objPHPExcelFromCSV = $objReader-> ;load( str_replace('.php', '.csv', __FILE__));
  186. //寫入Excel2007格式
  187. $objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV); .php', '.xlsx', __FILE__));
  188. 寫html
  189. require_once("05featuredemo.inc.php");

  190. require_once('Classes/PHPExcel/Writer/HTML.php');
  191. //寫入HTML格式
  192. $objWriter = new PHPExcel_Wter_HTHTML (Wter_HTHTML $objPHPExcel);
  193. $objWriter->setSheetIndex(0);
  194. $objWriter->save(str_replace('.php', '.htm', __FILE__));
  195. 寫pdf寫pdf
  196. require_once("05featuredemo.inc.php");

  197. require_once('Classes/PHPExcel/IOFactory.php');
  198. //寫入PDF格式
  199. $ objWriter =FactorExcel_IOter =Factory: :createWriter($objPHPExcel, 'PDF');
  200. $objWriter->setSheetIndex(0);
  201. $objWriter->save(str_replace('.php', '.pdf', __FILE__));//回顯記憶體峰值使用
  202. echo date('H:i:s') . “記憶體使用峰值:”。 (記憶體取得峰值使用情況(真)/ 1024 / 1024)。 " MBrn";
複製程式碼


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板