PHPExcel 导出2003和2007的excel文档实例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHPExcel 导出2003和2007的excel文档实例相关的知识,希望对你有一定的参考价值。

  1. require_once ‘common/excel/phpExcel.php‘;  
  2. require_once ‘common/excel/phpExcel/Writer/Excel2007.php‘;  
  3. require_once ‘common/excel/phpExcel/Writer/Excel5.php‘;  
  4. include_once ‘common/excel/phpExcel/IOFactory.php‘;  
  5.   
  6. $objExcel = new PHPExcel();  
  7. //设置属性 (这段代码无关紧要,其中的内容可以替换为你需要的)  
  8. $objExcel->getProperties()->setCreator("andy");  
  9. $objExcel->getProperties()->setLastModifiedBy("andy");  
  10. $objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");  
  11. $objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");  
  12. $objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");  
  13. $objExcel->getProperties()->setKeywords("office 2003 openxml php");  
  14. $objExcel->getProperties()->setCategory("Test result file");  
  15. $objExcel->setActiveSheetIndex(0);  
  16.   
  17. $i=0;  
  18. //表头  
  19. $k1="编号";  
  20. $k2="推广代码";  
  21. $k3="访问来源";  
  22. $k4="IP";  
  23. $k5="访问时间";  
  24.   
  25. $objExcel->getActiveSheet()->setCellValue(‘a1‘, "$k1");  
  26. $objExcel->getActiveSheet()->setCellValue(‘b1‘, "$k2");  
  27. $objExcel->getActiveSheet()->setCellValue(‘c1‘, "$k3");  
  28. $objExcel->getActiveSheet()->setCellValue(‘d1‘, "$k4");  
  29. $objExcel->getActiveSheet()->setCellValue(‘e1‘, "$k5");  
  30. //debug($links_list);  
  31. foreach($links_list as $k=>$v) {  
  32.     $u1=$i+2;  
  33.     /*----------写入内容-------------*/  
  34.     $objExcel->getActiveSheet()->setCellValue(‘a‘.$u1, $v["id"]);  
  35.     $objExcel->getActiveSheet()->setCellValue(‘b‘.$u1, $v["num"]);  
  36.     $objExcel->getActiveSheet()->setCellValue(‘c‘.$u1, $v["referer"]);  
  37.     $objExcel->getActiveSheet()->setCellValue(‘d‘.$u1, $v["ip"]);  
  38.     $objExcel->getActiveSheet()->setCellValue(‘e‘.$u1, $v["dateline"]);  
  39.     $i++;  
  40. }  
  41.   
  42. // 高置列的宽度  
  43. $objExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(10);  
  44. $objExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(10);  
  45. $objExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(70);  
  46. $objExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(15);  
  47. $objExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(15);  
  48.   
  49. $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(‘&L&BPersonal cash register&RPrinted on &D‘);  
  50. $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(‘&L&B‘ . $objExcel->getProperties()->getTitle() . ‘&RPage &P of &N‘);  
  51.   
  52. // 设置页方向和规模  
  53. $objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);  
  54. $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);  
  55. $objExcel->setActiveSheetIndex(0);  
  56. $timestamp = time();  
  57. if($ex == ‘2007‘) { //导出excel2007文档  
  58.     header(‘Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet‘);  
  59.     header(‘Content-Disposition: attachment;filename="links_out‘.$timestamp.‘.xlsx"‘);  
  60.     header(‘Cache-Control: max-age=0‘);  
  61.     $objWriter = PHPExcel_IOFactory::createWriter($objExcel, ‘Excel2007‘);  
  62.     $objWriter->save(‘php://output‘);  
  63.     exit;  
  64. else {  //导出excel2003文档  
  65.     header(‘Content-Type: application/vnd.ms-excel‘);  
  66.     header(‘Content-Disposition: attachment;filename="links_out‘.$timestamp.‘.xls"‘);  
  67.     header(‘Cache-Control: max-age=0‘);  
  68.     $objWriter = PHPExcel_IOFactory::createWriter($objExcel, ‘Excel5‘);  
  69.     $objWriter->save(‘php://output‘);  
  70.     exit;  

以上是关于PHPExcel 导出2003和2007的excel文档实例的主要内容,如果未能解决你的问题,请参考以下文章

tp5做的excel导入(用的PHPexcel类),有缺陷2007与2003只能二选一

PHPExcel导出xls

PHPExcel数据导出到Excel文件

phpexcel 导出方法

PHPEXCEL汇总两表的数据并加工

php 导入excel文件