php Excel常用方法详解大全

  1. //Include class 
  2. require_once(‘Classes/PHPExcel.php’); 
  3. require_once(‘Classes/PHPExcel/Writer/Excel2007.php’); 
  4. $objPHPExcel = new PHPExcel(); 
  5.  
  6. //Set properties 设置文件属性 
  7. $objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”); 
  8. $objPHPExcel->getProperties()->setLastModifiedBy(“Maarten Balliauw”); 
  9. $objPHPExcel->getProperties()->setTitle(“Office 2007 XLSX Test Document”); 
  10. $objPHPExcel->getProperties()->setSubject(“Office 2007 XLSX Test Document”); 
  11. $objPHPExcel->getProperties()->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”); 
  12. $objPHPExcel->getProperties()->setKeywords(“office 2007 openxml php”); 
  13. $objPHPExcel->getProperties()->setCategory(“Test result file”); 
  14.  
  15. //Add some data 添加数据 
  16. $objPHPExcel->setActiveSheetIndex(0); 
  17. $objPHPExcel->getActiveSheet()->setCellValue(‘A1′, ‘Hello’);//可以指定位置 
  18. $objPHPExcel->getActiveSheet()->setCellValue(‘A2′, true); 
  19. $objPHPExcel->getActiveSheet()->setCellValue(‘A3′, false); 
  20. $objPHPExcel->getActiveSheet()->setCellValue(‘B2′, ‘world!’); 
  21. $objPHPExcel->getActiveSheet()->setCellValue(‘B3′, 2); 
  22. $objPHPExcel->getActiveSheet()->setCellValue(‘C1′, ‘Hello’); 
  23. $objPHPExcel->getActiveSheet()->setCellValue(‘D2′, ‘world!’); 
  24. //循环 
  25. for($i = 1;$i<200;$i++) { 
  26. $objPHPExcel->getActiveSheet()->setCellValue(‘A’ . $i$i); 
  27. $objPHPExcel->getActiveSheet()->setCellValue(‘B’ . $i, ‘Test value’); 
  28. //日期格式化 
  29. $objPHPExcel->getActiveSheet()->setCellValue(‘D1′, time()); 
  30. $objPHPExcel->getActiveSheet()->getStyle(‘D1′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH); 
  31.  
  32. //Add comment 添加注释 
  33. $objPHPExcel->getActiveSheet()->getComment(‘E11′)->setAuthor(‘PHPExcel’); 
  34. $objCommentRichText = $objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(‘PHPExcel:’); 
  35. $objCommentRichText->getFont()->setBold(true); 
  36. $objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(“\r\n”); 
  37. $objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(‘Total amount on the current invoice, excluding VAT.’); 
  38.  
  39. //Add rich-text string 添加文字 可设置样式 
  40. $objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell(‘A18′) ); 
  41. $objRichText->createText(‘This invoice is ‘); 
  42. $objPayable = $objRichText->createTextRun(‘payable within thirty days after the end of the month’); 
  43. $objPayable->getFont()->setBold(true); 
  44. $objPayable->getFont()->setItalic(true); 
  45. $objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) ); 
  46. $objRichText->createText(‘, unless specified otherwise on the invoice.’); 
  47.  
  48. //Merge cells 合并分离单元格 
  49. $objPHPExcel->getActiveSheet()->mergeCells(‘A18:E22′); 
  50. $objPHPExcel->getActiveSheet()->unmergeCells(‘A18:E22′); 
  51.  
  52. //Protect cells 保护单元格 
  53. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//Needs to be set to true in order to enable any worksheet protection! 
  54. $objPHPExcel->getActiveSheet()->protectCells(‘A3:E13′, ‘PHPExcel’); 
  55.  
  56. //Set cell number formats 数字格式化 
  57. $objPHPExcel->getActiveSheet()->getStyle(‘E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE); 
  58. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle(‘E4′), ‘E5:E13′ ); 
  59.  
  60. //Set column widths 设置列宽度 
  61. $objPHPExcel->getActiveSheet()->getColumnDimension(‘B’)->setAutoSize(true); 
  62. $objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(12); 
  63.  
  64. //Set fonts 设置字体 
  65. $objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setName(‘Candara’); 
  66. $objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setSize(20); 
  67. $objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setBold(true); 
  68. $objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); 
  69. $objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); 
  70.  
  71. //Set alignments 设置对齐 
  72. $objPHPExcel->getActiveSheet()->getStyle(‘D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  73. $objPHPExcel->getActiveSheet()->getStyle(‘A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY); 
  74. $objPHPExcel->getActiveSheet()->getStyle(‘A18′)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 
  75. $objPHPExcel->getActiveSheet()->getStyle(‘A3′)->getAlignment()->setWrapText(true); 
  76.  
  77. //Set column borders 设置列边框 
  78. $objPHPExcel->getActiveSheet()->getStyle(‘A4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  79. $objPHPExcel->getActiveSheet()->getStyle(‘A10′)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  80. $objPHPExcel->getActiveSheet()->getStyle(‘E10′)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 
  81. $objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK); 
  82. $objPHPExcel->getActiveSheet()->getStyle(‘E13′)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK); 
  83.  
  84. //Set border colors 设置边框颜色 
  85. $objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getLeft()->getColor()->setARGB(‘FF993300′); 
  86. $objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getTop()->getColor()->setARGB(‘FF993300′); 
  87. $objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300′); 
  88. $objPHPExcel->getActiveSheet()->getStyle(‘E13′)->getBorders()->getRight()->getColor()->setARGB(‘FF993300′); 
  89.  
  90. //Set fills 设置填充 
  91. $objPHPExcel->getActiveSheet()->getStyle(‘A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); 
  92. $objPHPExcel->getActiveSheet()->getStyle(‘A1′)->getFill()->getStartColor()->setARGB(‘FF808080′); 
  93.  
  94. //Add a hyperlink to the sheet 添加链接 
  95. $objPHPExcel->getActiveSheet()->setCellValue(‘E26′, ‘www.phpexcel.net’); 
  96. $objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setUrl(‘http://www.phpexcel.net’); 
  97. $objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setTooltip(‘Navigate to website’); 
  98. $objPHPExcel->getActiveSheet()->getStyle(‘E26′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
  99.  
  100. //Add a drawing to the worksheet 添加图片 
  101. $objDrawing = new PHPExcel_Worksheet_Drawing(); 
  102. $objDrawing->setName(‘Logo’); 
  103. $objDrawing->setDescription(‘Logo’); 
  104. $objDrawing->setPath(‘./images/officelogo.jpg’); 
  105. $objDrawing->setHeight(36); 
  106. $objDrawing->setCoordinates(‘B15′); 
  107. $objDrawing->setOffsetX(110); 
  108. $objDrawing->setRotation(25); 
  109. $objDrawing->getShadow()->setVisible(true); 
  110. $objDrawing->getShadow()->setDirection(45); 
  111. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 
  112.  
  113. //Play around with inserting and removing rows and columns 
  114. $objPHPExcel->getActiveSheet()->insertNewRowBefore(6, 10); 
  115. $objPHPExcel->getActiveSheet()->removeRow(6, 10); 
  116. $objPHPExcel->getActiveSheet()->insertNewColumnBefore(‘E’, 5); 
  117. $objPHPExcel->getActiveSheet()->removeColumn(‘E’, 5); 
  118.  
  119. //Add conditional formatting 
  120. $objConditional1 = new PHPExcel_Style_Conditional(); 
  121. $objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS); 
  122. $objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN); 
  123. $objConditional1->setCondition(’0′); 
  124. $objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED); 
  125. $objConditional1->getStyle()->getFont()->setBold(true); 
  126.  
  127. //Set autofilter 自动过滤 
  128. $objPHPExcel->getActiveSheet()->setAutoFilter(‘A1:C9′); 
  129.  
  130. //Hide “Phone” and “fax” column 隐藏列 
  131. $objPHPExcel->getActiveSheet()->getColumnDimension(‘C’)->setVisible(false); 
  132. $objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setVisible(false); 
  133.  
  134. //Set document security 设置文档安全 
  135. $objPHPExcel->getSecurity()->setLockWindows(true); 
  136. $objPHPExcel->getSecurity()->setLockStructure(true); 
  137. $objPHPExcel->getSecurity()->setWorkbookPassword(“PHPExcel”); 
  138.  
  139. //Set sheet security 设置工作表安全 
  140. $objPHPExcel->getActiveSheet()->getProtection()->setPassword(‘PHPExcel’); 
  141. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// This should be enabled in order to enable any of the following! 
  142. $objPHPExcel->getActiveSheet()->getProtection()->setSort(true); 
  143. $objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true); 
  144. $objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true); 
  145.  
  146. //Calculated data 计算 
  147. echo ‘Value of B14 [=COUNT(B2:B12)]: ‘ . $objPHPExcel->getActiveSheet()->getCell(‘B14′)->getCalculatedValue() . “\r\n”; 
  148.  
  149. //Set outline levels 
  150. $objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setOutlineLevel(1); 
  151. $objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setVisible(false); 
  152. $objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setCollapsed(true); 
  153.  
  154. //Freeze panes 
  155. $objPHPExcel->getActiveSheet()->freezePane(‘A2′); 
  156.  
  157. //Rows to repeat at top 
  158. $objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(1, 1); 
  159.  
  160. //Set data validation 验证输入值 
  161. $objValidation = $objPHPExcel->getActiveSheet()->getCell(‘B3′)->getDataValidation(); 
  162. $objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE ); 
  163. $objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP ); 
  164. $objValidation->setAllowBlank(true); 
  165. $objValidation->setShowInputMessage(true); 
  166. $objValidation->setShowErrorMessage(true); 
  167. $objValidation->setErrorTitle(‘Input error’); 
  168. $objValidation->setError(‘Number is not allowed!’); 
  169. $objValidation->setPromptTitle(‘Allowed input’); 
  170. $objValidation->setPrompt(‘Only numbers between 10 and 20 are allowed.’); 
  171. $objValidation->setFormula1(10); 
  172. $objValidation->setFormula2(20); 
  173. $objPHPExcel->getActiveSheet()->getCell(‘B3′)->setDataValidation($objValidation); 
  174.  
  175. //Create a new worksheet, after the default sheet 创建新的工作标签 
  176. $objPHPExcel->createSheet(); 
  177. $objPHPExcel->setActiveSheetIndex(1); 
  178.  
  179. //Set header and footer. When no different headers for odd/even are used, odd header is assumed. 页眉页脚 
  180. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(‘&C&HPlease treat this document as confidential!’); 
  181. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(‘&L&B’ . $objPHPExcel->getProperties()->getTitle() . ‘&RPage &P of &N’); 
  182.  
  183. //Set page orientation and size 方向大小 
  184. $objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE); 
  185. $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); 
  186.  
  187. //Rename sheet 重命名工作表标签 
  188. $objPHPExcel->getActiveSheet()->setTitle(‘Simple’); 
  189.  
  190. //Set active sheet index to the first sheet, so Excel opens this as the first sheet 
  191. $objPHPExcel->setActiveSheetIndex(0); 
  192.  
  193. //Save Excel 2007 file 保存 
  194. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
  195. $objWriter->save(str_replace(‘.php’, ‘.xlsx’, __FILE__)); 
  196.  
  197. //Save Excel 5 file 保存 
  198. require_once(‘Classes/PHPExcel/Writer/Excel5.php’); 
  199. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 
  200. $objWriter->save(str_replace(‘.php’, ‘.xls’, __FILE__)); 
  201.  
  202. //1.6.2新版保存 
  203. require_once(‘Classes/PHPExcel/IOFactory.php’); 
  204. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007′); 
  205. $objWriter->save(str_replace(‘.php’, ‘.xls’, __FILE__)); 
  206.  
  207. 读excel 
  208. //Include class 
  209. require_once(‘Classes/PHPExcel/Reader/Excel2007.php’); 
  210. $objReader = new PHPExcel_Reader_Excel2007; 
  211.  
  212. $objPHPExcel = $objReader->load(“05featuredemo.xlsx”); 
  213.  
  214. 读写csv 
  215.  
  216. require_once(“05featuredemo.inc.php”); 
  217. require_once(‘Classes/PHPExcel/Writer/CSV.php’); 
  218. require_once(‘Classes/PHPExcel/Reader/CSV.php’); 
  219. require_once(‘Classes/PHPExcel/Writer/Excel2007.php’); 
  220.  
  221. //Write to CSV format 写 
  222. $objWriter = new PHPExcel_Writer_CSV($objPHPExcel); 
  223. $objWriter->setDelimiter(‘;’); 
  224. $objWriter->setEnclosure(”); 
  225. $objWriter->setLineEnding(“\r\n”); 
  226. $objWriter->setSheetIndex(0); 
  227. $objWriter->save(str_replace(‘.php’, ‘.csv’, __FILE__)); 
  228.  
  229. //Read from CSV format 读 
  230. $objReader = new PHPExcel_Reader_CSV(); 
  231. $objReader->setDelimiter(‘;’); 
  232. $objReader->setEnclosure(”); 
  233. $objReader->setLineEnding(“\r\n”); 
  234. $objReader->setSheetIndex(0); 
  235. $objPHPExcelFromCSV = $objReader->load(str_replace(‘.php’, ‘.csv’, __FILE__)); 
  236.  
  237. //Write to Excel2007 format 
  238. $objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV); 
  239. $objWriter2007->save(str_replace(‘.php’, ‘.xlsx’, __FILE__)); 
  240.  
  241. 写html 
  242.  
  243. require_once(“05featuredemo.inc.php”); 
  244. require_once(‘Classes/PHPExcel/Writer/HTML.php’); 
  245.  
  246. //Write to HTML format 
  247. $objWriter = new PHPExcel_Writer_HTML($objPHPExcel); 
  248. $objWriter->setSheetIndex(0); 
  249. $objWriter->save(str_replace(‘.php’, ‘.htm’, __FILE__)); 
  250.  
  251. 写pdf 
  252.  
  253. require_once(“05featuredemo.inc.php”); 
  254. require_once(‘Classes/PHPExcel/IOFactory.php’); 
  255.  
  256. //Write to PDF format 
  257. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘PDF’); 
  258. $objWriter->setSheetIndex(0); 
  259. $objWriter->save(str_replace(‘.php’, ‘.pdf’, __FILE__)); 
  260. //Echo memory peak usage 
  261. echo date(‘H:i:s’) . ” Peak memory usage: ” . (memory_get_peak_usage(true) / 1024 / 1024) . ” MB\r\n”; 

 

转载请注明:代码家园 » php Excel常用方法详解大全

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)