phpexecl 的基本操作
基本使用方法分三部分:
一、引入接口
// PHPExcel_IOFactory
require_once dirname(__FILE__).‘/Classes/PHPExcel/IOFactory.php‘;
二、定义EXCEL实体
即定义一个PHPEXCEL对象,并设置EXCEL对象内显示内容
// Excel开始
// 准备EXCEL的包括文件
// Error reporting
error_reporting(E_ALL);
// PHPExcel
require_once dirname(__FILE__) . ‘/../Classes/PHPExcel.php‘;
require_once dirname(__FILE__) . ‘/../Classes/PHPExcel/RichText.php‘;
// 生成新的excel对象
$objPHPExcel = new PHPExcel();
// 设置excel文档的属性
$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 data report")
->setCategory("Test result file");
// 开始操作excel表
// 操作第一个工作表
$objPHPExcel->setActiveSheetIndex(0);
// 设置工作薄名称
$objPHPExcel->getActiveSheet()->setTitle(iconv(‘gbk‘, ‘utf-8‘, ‘数据报表(本报表由光恒数据中心程序自动生成)‘));
// 设置默认字体和大小
$objPHPExcel->getDefaultStyle()->getFont()->setName(iconv(‘gbk‘, ‘utf-8‘, ‘宋体‘));
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
三、输出文件
// 如果需要输出EXCEL格式
if($m_exportType=="excel"){
$objWriter = PHPExcel_IOFactory::createWriter($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($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");
}
设置一列的宽度:
$objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(15);
设置一行的高度:
$objPHPExcel->getActiveSheet()->getRowDimension(‘6‘)->setRowHeight(30);
合并单元格:
$objPHPExcel->getActiveSheet()->mergeCells(‘A1:P1‘);
设置A1单元格加粗,居中:
$styleArray1 = array(
‘font‘ => array(
‘bold‘ => true,
‘color‘=>array(
‘argb‘ => ‘00000000‘,
),
),
‘alignment‘ => array(
‘horizontal‘ => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 将A1单元格设置为加粗,居中
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->applyFromArray($styleArray1);
给特定单元格中写入内容:
$objPHPExcel->getActiveSheet()->setCellValue(‘A1‘, ‘Hello Baby‘);
设置单元格样式(居中):
$objPHPExcel->getActiveSheet()->getStyle(‘H5‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
设置单元格样式(黑色字体):
$objPHPExcel->getActiveSheet()->getStyle(‘H5‘)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
设置单元格格式(背景):
$objPHPExcel->getActiveSheet()->getStyle(‘H5‘)->getFill()->getStartColor()->setARGB(‘00ff99cc‘); // 将背景设置为浅粉色
设置单元格格式(数字格式):
$objPHPExcel->getActiveSheet()->getStyle(‘F‘.$iLineNumber)->getNumberFormat()->setFormatCode(‘0.000‘);
给单元格中放入图片:
// 将数据中心图片放在J1单元格内
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘Logo‘);
$objDrawing->setDescription(‘Logo‘);
$objDrawing->setPath(‘../get/detail/images/logo_01.jpg‘);
$objDrawing->setWidth(400);
$objDrawing->setHeight(123);
$objDrawing->setCoordinates(‘J1‘);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
在单元格中设置超链接:
$objPHPExcel->getActiveSheet()->setCellValue(‘J9‘, iconv(‘gbk‘, ‘utf-8‘, ‘谷歌‘));
$objPHPExcel->getActiveSheet()->getCell(‘J9‘)->getHyperlink()->setUrl(‘http://www.g.cn/‘);
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。