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

PHP导出EXCEL快速开发指南--PHPEXCEL的使用详解

程序员文章站 2022-05-25 17:26:02
php导出excel快速开发指南phpexcel有专有的开发文档,详细操作请参考其开发文档,本文档只是对其在使用上作了优化整合,便于在新项目中快速开发。phpexcel生成...
php导出excel快速开发指南
phpexcel有专有的开发文档,详细操作请参考其开发文档,本文档只是对其在使用上作了优化整合,便于在新项目中快速开发。
phpexcel生成文件同样有两种方式,一种方式为直接输出,一种方式为生成静态文件。
直接输出:
主文件为(class目录的同目录文件):
复制代码 代码如下:

<?php
include("./class/class.php"); // 包含class的基本头文件
include("./class/phpexcel/phpexcel.php"); // 生成excel的基本类定义(注意文件名的大小写)
// 如果直接输出excel文件,则要包含此文件
include("./class/phpexcel/phpexcel/iofactory.php");
// 创建phpexcel对象,此对象包含输出的内容及格式
$m_objphpexcel = new phpexcel();
// 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中
// 模板文件将对象$m_objphpexcel进行操作
include("./include/excel.php");
// 输出文件的类型,excel或pdf
$m_exporttype = "excel";
$m_stroutputexcelfilename = date('y-m-j_h_i_s').".xls"; // 输出excel文件名
$m_stroutputpdffilename = date('y-m-j_h_i_s').".pdf"; // 输出pdf文件名
// phpexcel_iofactory, 输出excel
//require_once dirname(__file__).'/classes/phpexcel/iofactory.php';
// 如果需要输出excel格式
if($m_exporttype=="excel"){
$objwriter = phpexcel_iofactory::createwriter($m_objphpexcel, 'excel5');
// 从浏览器直接输出$m_stroutputexcelfilename
header("pragma: public");
header("expires: 0");
header("cache-control:must-revalidate, post-check=0, pre-check=0");
header("content-type:application/force-download");
header("content-type: application/vnd.ms-excel;");
header("content-type:application/octet-stream");
header("content-type:application/download");
header("content-disposition:attachment;filename=".$m_stroutputexcelfilename);
header("content-transfer-encoding:binary");
$objwriter->save("php://output");
}
// 如果需要输出pdf格式
if($m_exporttype=="pdf"){
$objwriter = phpexcel_iofactory::createwriter($m_objphpexcel, 'pdf');
$objwriter->setsheetindex(0);
header("pragma: public");
header("expires: 0");
header("cache-control:must-revalidate, post-check=0, pre-check=0");
header("content-type:application/force-download");
header("content-type: application/pdf");
header("content-type:application/octet-stream");
header("content-type:application/download");
header("content-disposition:attachment;filename=".$m_stroutputpdffilename);
header("content-transfer-encoding:binary");
$objwriter->save("php://output");
}
?>

模板文件内容(附加常用操作)
复制代码 代码如下:

<?php
global $m_objphpexcel; // 由外部文件定义
// 设置基本属性
$m_objphpexcel->getproperties()->setcreator("sun star data center")
->setlastmodifiedby("sun star data center")
->settitle("microsoft office excel document")
->setsubject("test data report -- from sunstar data center")
->setdescription("ld test data report, generate by sunstar data center")
->setkeywords("sunstar ld report")
->setcategory("test result file");
// 创建多个工作薄
$sheet1 = $m_objphpexcel->createsheet();
$sheet2 = $m_objphpexcel->createsheet();
// 通过操作索引即可操作对应的工作薄
// 只需设置要操作的工作簿索引为当前活动工作簿,如
// $m_objphpexcel->setactivesheetindex(0);
// 设置第一个工作簿为活动工作簿
$m_objphpexcel->setactivesheetindex(0);
// 设置活动工作簿名称
// 如果是中文一定要使用iconv函数转换编码
$m_objphpexcel->getactivesheet()->settitle(iconv('gbk', 'utf-8', '测试工作簿'));
// 设置默认字体和大小
$m_objphpexcel->getdefaultstyle()->getfont()->setname(iconv('gbk', 'utf-8', '宋体'));
$m_objphpexcel->getdefaultstyle()->getfont()->setsize(10);
// 设置一列的宽度
$m_objphpexcel->getactivesheet()->getcolumndimension('a')->setwidth(15);
// 设置一行的高度
$m_objphpexcel->getactivesheet()->getrowdimension('6')->setrowheight(30);
// 合并单元格
$m_objphpexcel->getactivesheet()->mergecells('a1:p1');
// 定义一个样式,加粗,居中
$stylearray1 = array(
'font' => array(
'bold' => true,
'color'=>array(
'argb' => '00000000',
),
),
'alignment' => array(
'horizontal' => phpexcel_style_alignment::horizontal_center,
),
);
// 将样式应用于a1单元格
$m_objphpexcel->getactivesheet()->getstyle('a1')->applyfromarray($stylearray1);
// 设置单元格样式(黑色字体)
$m_objphpexcel->getactivesheet()->getstyle('h5')->getfont()->getcolor()->setargb(phpexcel_style_color::color_black); // 黑色
// 设置单元格格式(背景)
$m_objphpexcel->getactivesheet()->getstyle('h5')->getfill()->getstartcolor()->setargb('00ff99cc'); // 将背景设置为浅粉色
// 设置单元格格式(数字格式)
$m_objphpexcel->getactivesheet()->getstyle('f1')->getnumberformat()->setformatcode('0.000');
// 给特定单元格中写入内容
$m_objphpexcel->getactivesheet()->setcellvalue('a1', 'hello baby');
// 设置单元格样式(居中)
$m_objphpexcel->getactivesheet()->getstyle('h5')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center);
// 给单元格中放入图片, 将数据图片放在j1单元格内
$objdrawing = new phpexcel_worksheet_drawing();
$objdrawing->setname('logo');
$objdrawing->setdescription('logo');
$objdrawing->setpath("../logo.jpg"); // 图片路径,只能是相对路径
$objdrawing->setwidth(400); // 图片宽度
$objdrawing->setheight(123); // 图片高度
$objdrawing->setcoordinates('j1');//单元格
$objdrawing->setworksheet($m_objphpexcel->getactivesheet());
// 设置a5单元格内容并增加超链接
$m_objphpexcel->getactivesheet()->setcellvalue('a5', iconv('gbk', 'utf-8', '超链接keiyi.com'));
$m_objphpexcel->getactivesheet()->getcell('a5')->gethyperlink()->seturl('http://www.keiyi.com/');
?>

在服务器端生成静态文件
相比直接生成,这两种方法的主要区别是生成格式的不同,模板文件完全相同,下边是一个在上例基础上更改后的样子,注意与上例的区别。
复制代码 代码如下:

<?php
// 包含class的基本头文件
include("./class/class.php");
// 生成excel的基本类定义(注意文件名的大小写)
include("./class/phpexcel/phpexcel.php");
// 包含写excel5格式的文件,如果需要生成excel2007的文件,包含对应的writer即可
include("./class/phpexcel/phpexcel/writer/excel5.php");
// 包含写pdf格式文件
include("./class/phpexcel/phpexcel/writer/pdf.php");
// 创建phpexcel对象,此对象包含输出的内容及格式
$m_objphpexcel = new phpexcel();
// 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中
// 模板文件将对象$m_objphpexcel进行操作
include("./include/excel.php");
// 输出文件的类型,excel或pdf
$m_exporttype = "pdf";
$m_stroutputexcelfilename = date('y-m-j_h_i_s').".xls"; // 输出excel文件名
$m_stroutputpdffilename = date('y-m-j_h_i_s').".pdf"; // 输出pdf文件名
// 输出文件保存路径,此路径必须可写
$m_stroutputpath = "./output/";
// 如果需要输出excel格式
if($m_exporttype=="excel"){
$objwriter = new phpexcel_writer_excel5($m_objphpexcel);
$objwriter->save($m_stroutputpath.$m_stroutputexcelfilename);
}
// 如果需要输出pdf格式
if($m_exporttype=="pdf"){
$objwriter = new phpexcel_writer_pdf($m_objphpexcel);
$objwriter->save($m_stroutputpath.$m_stroutputpdffilename);
}
?>