发布于 2017-07-26 21:47:45 | 138 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

PHPExcel PHP类库

PHPExcel 是用来操作Office Excel 文档的一个PHP类库,它基于微软的OpenXML标准和PHP语言。可以使用它来读取、写入不同格式的电子表格,如 Excel (BIFF) .xls, Excel 2007 (OfficeOpenXML) .xlsx, CSV, Libre/OpenOffice Calc .ods, Gnumeric, PDF, HTML等等。


这篇文章主要介绍了PHPExcel导出2003和2007的excel文档功能,结合实例形式分析了PHPExcel属性设置及文档导出操作相关技巧,需要的朋友可以参考下

本文实例讲述了PHPExcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下:


require_once 'common/excel/PHPExcel.php';
require_once 'common/excel/phpExcel/Writer/Excel2007.php';
require_once 'common/excel/phpExcel/Writer/Excel5.php';
include_once 'common/excel/phpExcel/IOFactory.php';
$objExcel = new PHPExcel();
//设置属性 (这段代码无关紧要,其中的内容可以替换为你需要的)
$objExcel->getProperties()->setCreator("andy");
$objExcel->getProperties()->setLastModifiedBy("andy");
$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
$objExcel->getProperties()->setKeywords("office 2003 openxml php");
$objExcel->getProperties()->setCategory("Test result file");
$objExcel->setActiveSheetIndex(0);
$i=0;
//表头
$k1="编号";
$k2="推广代码";
$k3="访问来源";
$k4="IP";
$k5="访问时间";
$objExcel->getActiveSheet()->setCellValue('a1', "$k1");
$objExcel->getActiveSheet()->setCellValue('b1', "$k2");
$objExcel->getActiveSheet()->setCellValue('c1', "$k3");
$objExcel->getActiveSheet()->setCellValue('d1', "$k4");
$objExcel->getActiveSheet()->setCellValue('e1', "$k5");
//debug($links_list);
foreach($links_list as $k=>$v) {
  $u1=$i+2;
  /*----------写入内容-------------*/
  $objExcel->getActiveSheet()->setCellValue('a'.$u1, $v["id"]);
  $objExcel->getActiveSheet()->setCellValue('b'.$u1, $v["num"]);
  $objExcel->getActiveSheet()->setCellValue('c'.$u1, $v["referer"]);
  $objExcel->getActiveSheet()->setCellValue('d'.$u1, $v["ip"]);
  $objExcel->getActiveSheet()->setCellValue('e'.$u1, $v["dateline"]);
  $i++;
}
// 高置列的宽度
$objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
$objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(70);
$objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
$objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
$objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');
// 设置页方向和规模
$objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
$objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objExcel->setActiveSheetIndex(0);
$timestamp = time();
if($ex == '2007') { //导出excel2007文档
  header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xlsx"');
  header('Cache-Control: max-age=0');
  $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
  $objWriter->save('php://output');
  exit;
} else { //导出excel2003文档
  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xls"');
  header('Cache-Control: max-age=0');
  $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
  $objWriter->save('php://output');
  exit;
}

希望本文所述对大家PHP程序设计有所帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务