首頁 >後端開發 >php教程 >phpExcel匯出excel加超連結的實例程式碼

phpExcel匯出excel加超連結的實例程式碼

WBOY
WBOY原創
2016-07-25 08:55:231820瀏覽
  1. //寫excel

  2. //包含類別

  3. require_once( 'Classes/PHPExcel.php');
  4. require_once('Classes/PHPExcel/Writer/Excel2007.php');
  5. $objPHPExcel = new PHPExcel();
  6. /* *

  7. * phpExcel匯出excel
  8. * by bbs.it-home.org
  9. */
  10. //設定屬性檔案屬性

  11. $objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”);
  12. $objPHPExcel->getProperties ()->setLastModifiedBy(“Maarten Balliauw”);
  13. $objPHPExcel->getProperties()->setTitle(“Office 2007 XLSX 測試文件”);
  14. $objPHPExcel->getProperties( Office 2007 XLSX 測試文檔」);
  15. $objPHPExcel->getProperties()->setDescription(“Office 2007 XLSX 的測試文檔,使用PHP 類別產生。”);
  16. $objPHPExcel->getProperties( setKeywords(“office 2007 openxml php”);
  17. $objPHPExcel->getProperties()->setCategory(“測試結果檔案”);
  18. //新增一些資料來新增資料

  19. $objPHPExcel->設定ActiveSheetIndex(0);
  20. $objPHPExcel->getActiveSheet()->setCellValue('A1′, 'Hello');//可以指定位置
  21. $objPHPExcel->getActiveSheet()- >setCellValue('A2′,真的);
  22. $objPHPExcel->getActiveSheet()->setCellValue('A3', false);
  23. $objPHPExcel->getActiveSheet()->setCellValue('B2', 'world!');
  24. $objPHPExcel->getActiveSheet()->setCellValue('B3', 2);
  25. $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Hello');
  26. $objPHPExcel->getActiveSheet()->setCellValue('D2', 'world!');
  27. //迴圈
  28. for($i = 1;$i$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i );
  29. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, '測試值') ;
  30. }
  31. //日期格式化
  32. $objPHPExcel->getActiveSheet()->setCellValue('D1', time());
  33. $objPHPExcel->getActiveSheet()->getStyle('D1′)- >getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
  34. / /Add comment 新增註解

  35. $objPHPExcel->getActiveSheet(-100 setAuthor('PHPExcel');
  36. $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('E11')->getText()->createTextRun('PHPExcel:');
  37. getFont()->setBold(true);
  38. $objPHPExcel->getActiveSheet()->getComment('E11′)->getText()->createTextRun(“rn”);
  39. $objPHPExcel->getActiveSheet ()->getComment('E11′)->getText()->createTextRun('目前發票上的總金額,不含增值稅。');
  40. //新增富文本字串新增文字可設定樣式

  41. $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell('A18′) );
  42. $objRichText->createText('這張發票是');
  43. $objPayable = $objRichText->createTextRun('月底後三十天內付款');
  44. $objPayable->getFont()->setBold(true);
  45. $objPayable->getFont()- >setItalic(true);
  46. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );
  47. $objRichText->createText(',除非發票上另有說明。 ');
  48. //合併儲存格合併分隔儲存格

  49. $objPHPExcel->; getActiveSheet()->mergeCells('A18:E22');
  50. $objPHPExcel- >getActiveSheet()->unmergeCells('A18:E22′);
  51. //保護單元格

  52. $objPHPExcel->; getActiveSheet()->getProtection()->setSheet( true);//需要設定為true才能啟用任何工作表保護!
  53. $objPHPExcel->getActiveSheet()->protectCells('A3:E13′, 'PHPExcel');
  54. //設定單元格編號格式數字結構

  55. $objPHPExcel- >getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
  56. $objPHPExcel->getActiveSheet( getStyle('E4′), 'E5:E13′ );
  57. < ;p>//設定列寬度設定列寬度
  58. $objPHPExcel->getActiveSheet()->getColumnDimension('B' )->setAutoSize(true);
  59. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
  60. //設定字體

  61. $ objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
  62. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont ()->setSize(20);
  63. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setBold(true);
  64. $objPHPExcel->getActiveSheet()- >getStyle('B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  65. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->getor() ->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  66. //設定對齊方式

  67. $objPHPExcel->getActiveSheet()->getStyle('D11′)->getAlignment()-> setHorizo​​​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  68. $objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizo​​​​nfiduzo​​n; $objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  69. $objPHPExcel->getActiveSheet( getAlignment()->setWrapText(true);
  70. //設定列borders 已知列

  71. $objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders( )->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  72. $objPHPExcel->getActiveSheet()->getStyle('A10′)->getorderSPH)-> :BORDER_THIN);
  73. $objPHPExcel->getActiveSheet()->getStyle('E10′)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_BorderBORDER_THIN)::BORDER_THIN)
  74. ;$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  75. $objPHP%( E13′)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
  76. //設定邊框顏色為自己的顏色

  77. $objPHPExcel->geteSheet ()->getStyle('D13′)->getBorders()->getLeft()->getColor( )->setARGB('FF993300');
  78. $objPHPExcel->getActiveSheet()->getStyle('D13 ')->getBorders()->getTop()->getColor()->setARGB('FF993300');
  79. $objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()- >getBottom()->getColor()->setARGB('FF993300');
  80. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor( )->setARGB('FF993300′);
  81. //設定填充設定填充

  82. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()- >setFillType(PHPExcel_Style_Fill::填充_固體);
  83. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′) p>
  84. //為工作表新增超連結連結

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

  90. $objDrawing = new PHPExcel_Worksheet_Drawing();
  91. $objDrawing->setName('Logo')
  92. $objDrawing->setDescription('Logo');
  93. $objDrawing->setPath('./images/officelogo.jpg');
  94. $objDrawing->setHeight(36);
  95. $objDrawing ->setCoordinates('B15');
  96. $objDrawing->setOffsetX(110);
  97. $objDrawing->setRotation(25);
  98. $objDrawing->getShadow()->
  99. $objDrawing->getShadow()->setDirection(45);
  100. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  101. //嘗試插入和刪除行與列

  102. $objPHPExcel-> ;getActiveSheet()->insertNewRowBefore(6, 10);
  103. $objPHPExcel->getActiveSheet()->removeRow(6, 10); objobjo> getActiveSheet()->insertNewColumnBefore('E', 5);
  104. $objPHPExcel->getActiveSheet()->removeColumn('E', 5);
  105. ///新增條件格式

  106. $objConditional1 = new PHPExcel_Style_Conditional() ;
  107. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
  108. $dition_Conpgenal1-dob​​perator​​al(Boper); jConditional1->setCondition( '0');
  109. $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
  110. $objConditional1->getStyle()->Font() ->setBold(true);
  111. //設定autofilter自動過濾

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

  114. $objPHPExcel->getActiveSheet ()->getColumnDimension('C')->setVisible(false);
  115. $objPHPExcel-> getActiveSheet()->getColumnDimension('D')->setVisible(false);
  116. //設定文件安全設定文件安全性

  117. $objPHPExcel->getSecurity()->setLockWindows( true);
  118. $objPHPExcel->getSecurity()->setLockStructure(true);
  119. $objPHPExcel->getSecurity()->setWorkbookPassword(“PHPExcel”);
  120. / /設定工作表安全性

  121. $objPHPExcel-> getActiveSheet()->getProtection()->setPassword('PHPExcel');
  122. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true) ;// 應該啟用此功能才能啟用以下任何功能!
  123. $objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
  124. $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(trueInsertRows(true)
  125. $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
  126. //計算資料計算

  127. echo 'Value of B14 [=COUNT (B2:B12) ]: ' . $objPHPExcel->getActiveSheet()->getCell('B14')->getCalculatedValue() 。 「rn」;
  128. //設定大綱層級

  129. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
  130. $objPHPExcel- >getActiveSheet()->getColumnDimension('E')->setVisible(false);
  131. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setCollapsed(true);
  132. //凍結窗格

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

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

  137. $objValidation = $objPHPExcel->getActiveet( ->getCell( 'B3')->getDataValidation();
  138. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
  139. $objValidation->setErrorStyle(Excel_Cell_ValidationdobjValidation->setErrorStyle(Excel_Cell_ValidationValcel_Cell_Data; >setAllowBlank(true);
  140. $objValidation->setShowInputMessage(true);
  141. $objValidation->setShowErrorMessage(true);
  142. $objValidation->setErrorTitle('objorTitle('objor); ->setError('數字不允許!');
  143. $objValidation->setPromptTitle('允許輸入');
  144. $objValidation->setPrompt('只允許使用10 到20 之間的數字。 ');
  145. $objValidation->setFormula1(10);
  146. $objValidation->setFormula2(20);
  147. $objPHPExcel->getActiveSheet()->getCell('B3′)->setDataValidation($$ objValidation);
  148. //建立一個新的工作表,之後預設工作表建立新的工作標籤

  149. $objPHPExcel->createSheet(); bbs.it-home.org
  150. $objPHPExcel->setActiveSheetIndex(1);
  151. //設定頁首和頁尾。當奇數/偶數沒有使用不同的標頭時,假定為奇數標頭。 頁首頁腳

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

  155. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); ;
  156. //重新命名工作表重新命名工作表標籤

  157. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  158. //將活動工作表索引設定為第一個工作表,因此Excel 將其作為第一個工作表開啟sheet

  159. $objPHPExcel->setActiveSheetIndex(0);
  160. / /儲存Excel 2007 檔案保存

  161. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  162. $objWriter->save(str_replace('.php', '.xlsx', __FILE__); >

    //保存Excel 5 檔案保存

  163. require_once( '類別/PHPExcel/Writer/Excel5.php');
  164. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
  165. //1.6.2新版保存

  166. require_once( '類別/PHPExcel/IOFactory.php');
  167. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  168. $objWriter->save(str_replace('.php', '.xls', __FILE__));

    //讀取excel

  169. //包含類別
  170. require_once('Classes/PHPExcel/Reader/Excel2007.php');
  171. $objReader = new PHPExcel_Reader_Excel2007;>$objReader = new PHPExcel_Reader_Excel2007;
  172. $objPHPExcel = $objReader->load(“05featuredemo.xlsx”);
  173. //csv讀寫器

  174. require_once(“05featuredemo.inc.php”);
  175. require_once(“05featuredemo.inc.php”);
  176. require_once(“05featuredemo.inc.php”);
  177. require_once(“05featuredemo.inc.php”);
  178. require_once(“05featuredemo.inc.php”);
  179. require_once(“05featuredemo.inc.php”);
  180. require_once('Classes/PHPExcel/Writer/CSV.php');
  181. require_once('Classes/PHPExcel/Reader/CSV.php');
  182. require_once('Classes/PHPExcel/Writer/Excel2007.phpquire' );
  183. //寫入CSV格式寫

  184. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
  185. $objWriter->setDelimiter(';');
  186. $objWriter->setDelimiter(';');
  187. $ objWriter->setEnclosure(”);
  188. $objWriter->setLineEnding(“rn”);
  189. $objWriter->setSheetIndex(0);
  190. $objWriter->save(str_replace('.php', '.csv', __FILE__));
  191. //從CSV格式讀取讀取

  192. $objReader = new PHPExcel_Reader_CSV () ;
  193. $objReader->setDelimiter(‘;’);
  194. $objReader->setEnclosure(”);
  195. $objReader->setLineEnding(“rn”);
  196. $objReader->setSheetIndex(0);
  197. $objPHPExcelFromCSV = $objReader->load(str_replace('.php', '.csv', __FILE__));
  198. //寫入Excel2007格式

  199. $objWriter2007 =新PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
  200. $objWriter2007->save(str_replace('.php', '.xlsx', __FILE__));
  201. //寫html

  202. require_once(“05featuredemo.inc.php ” ”);
  203. require_once('Classes/PHPExcel/Writer/HTML.php');
  204. //寫入HTML格式

  205. $objWriter = new PHPExcel_Writer_HTML($objPHPExcelobjWriter = new PHPExcel_Writer_HTML($objPHPExcelobjWriter = new PHPExcel_Writer_HTML($objPHPExcelobjWriter = new PHPExcel_Writer_HTML($objPHPExcelobjWriter = new PHPExcel_Writer_HTML($objPHPExcel ;
  206. $objWriter->setSheetIndex(0)
  207. $objWriter->save(str_replace('.php', '.htm', __FILE__));
  208. //寫入pdf

  209. require_once(“05featuredemo.inc.php”);
  210. require_once('Classes/PHPExcel/IOFactory.php');
  211. //寫入PDF格式

  212. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF ');
  213. $objWriter->setSheetIndex(0);
  214. $objWriter->save(str_replace('.php', '.pdf, $objWriter->save(str__ ));
  215. //回顯記憶體使用熱點
  216. echo date('H:i:s') . “記憶體使用熱點:”。 MBrn”;
複製程式碼


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn