欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHPExcel导出2003和2007的excel文档功能示例

程序员文章站 2024-03-06 12:02:43
本文实例讲述了phpexcel导出2003和2007的excel文档功能。分享给大家供大家参考,具体如下: require_once 'common/excel/...

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php数组(array)操作技巧大全》、《php排序算法总结》、《php常用遍历算法与技巧总结》、《php数据结构与算法教程》、《php程序设计算法总结》、《php数学运算技巧总结》、《php正则表达式用法总结》、《php运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总

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