PHPExcel笔记, mpdf导出
作者:世界呆 时间:2024-06-05 09:44:57
phpexcel常用处理
##导入类库require 'PHPExcel/Classes/PHPExcel.php';require 'PHPExcel/Classes/PHPExcel/Writer/Excel5.php'; //非07格式的写出类 ##基础属性设定$objPHPExcel = \PHPExcel_IOFactory::load('a.xls'); //读入指定excel文件$objPHPExcel->setActiveSheetIndex(0); //指定活动工作表$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('宋体');$objPHPExcel->getProperties()->setTitle('xxx'); ##单元格编辑$objPHPExcel->getActiveSheet()->setCellValue('A3', 'xxx'); //设定A3单元格值为xxx ##单元格绘图$objDrawing = new \PHPExcel_Worksheet_Drawing();$objDrawing->setPath('a.jpg'); //指定图片路径。若要远程图片需PHPExcel/Classes/PHPExcel/Worksheet/Drawing.php:106处file_exists换成file_get_contents$objDrawing->setCoordinates('A4'); //指定在A4单元格绘图$objDrawing->setName('Photo');$objDrawing->setDescription('Photo');$objDrawing->setHeight(120);$objDrawing->setWidth(100);$objDrawing->setOffsetX(7);$objDrawing->setOffsetY(7);$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); ##excel文件浏览器下载导出$filename='a.xls';$encoded_filename = rawurlencode($filename);$ua = $_SERVER["HTTP_USER_AGENT"];header('Content-type: application/vnd.ms-excel');if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua) || preg_match("/Edge/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');} else if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8''" . $filename . '"');} else { header('Content-Disposition: attachment; filename="' . $filename . '"');}header("Pragma:no-cache");header("Expires:0");$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');$objWriter->save('php://output'); ##excel文件html显示(可用于调试)$objWriter = new \PHPExcel_Writer_HTML($objPHPExcel);$objWriter->save('php://output');
利用mpdf库从phpexcel导出pdf文件
$filename='a.pdf';
$encoded_filename = rawurlencode($filename);
$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; //指定通过mpdf类库导出pdf文件
$rendererLibraryPath = 'PHPExcel/MPDF57'; //指定你下载的mpdf类库路径
if (!\PHPExcel_Settings::setPdfRenderer(
$rendererName,
$rendererLibraryPath
)) {
die(
'Please set the $rendererName and $rendererLibraryPath values' .
PHP_EOL .
' as appropriate for your directory structure'
);
}
header('Content-type: application/pdf');
if (preg_match("/MSIE/", $ua) || preg_match("/Trident\/7.0/", $ua) || preg_match("/Edge/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header("Content-Disposition: attachment; filename*=\"utf8''" . $file_name . '"');
} else {
header('Content-Disposition: attachment; filename="' . $file_name . '"');
}
header("Pragma:no-cache");
header("Expires:0");
$objWriter = new \PHPExcel_Writer_PDF($objPHPExcel);
$objWriter->setPreCalculateFormulas(false);
$objWriter->save('php://output');
##############################
##pdf导出失败的一些错误解决方法
##############################
##1 pdf中文乱码问题
PHPExcel/Classes/PHPExcel/Writer/PDF/mPDF.php:105处加两行设定:
$pdf->useAdobeCJK = true;
$pdf->SetAutoFont(AUTOFONT_ALL);
##2 类库里面多处preg_replace调用使用了元字符e,而部分低版本php不支持正则表达式e元字符
e元字符的不当使用并导致pdf报错的触发点在类库里面大概有五六处吧,
由于e元字符是一个shell下的子进程php调用,所以报错信息不会反馈到当前php进程中,故即便你配置了错误打印到屏幕, 页面也不会显示报错信息, 必须查看php报错日志
查看php报错日志,把提示的preg_replace中元字符e的调用替换为preg_replace_callback形式的调用
##3 部分版本phpexcel类库有单元格样式判断错误
lib/PHPExcel/Classes/PHPExcel/Writer/HTML.php:1236处加个if判断
if (!$this->_useInlineCss) {
$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#操作本地文件及保存文件到数据库的基本方法总结
浅谈go中cgo的几种使用方式
![](https://img.aspxhome.com/file/2023/0/112190_0s.png)
JS设计模式之单例模式(一)
LotusPhp笔记之:基于ObjectUtil组件的使用分析
Python使用htpasswd实现基本认证授权的例子
![](https://img.aspxhome.com/file/2023/2/120342_0s.png)
解决golang编译提示dial tcp 172.217.160.113:443: connectex: A connection attempt failed(推荐)
php浅析反序列化结构
CentOS 7 安装 MySQL 5.6遇到的各种问题小结
![](https://img.aspxhome.com/file/2023/4/70014_0s.png)
java与php的区别浅析
go语言里包的用法实例
javascript基本包装类型介绍
![](https://img.aspxhome.com/file/2023/7/132517_0s.jpg)
Python OpenCV实现图形检测示例详解
![](https://img.aspxhome.com/file/2023/8/103498_0s.jpg)
请给PNG8一个机会
Python urllib库的使用指南详解
PHP实现数组根据某个字段进行水平合并,横向合并案例分析
PHP在线生成二维码代码(google api)
vue el-upload上传文件的示例代码
Python编程之序列操作实例详解
![](https://img.aspxhome.com/file/2023/1/115431_0s.jpg)
深入理解Mysql事务隔离级别与锁机制问题
![](https://img.aspxhome.com/file/2023/4/115354_0s.png)
一起来了解mysql数据库
![](https://img.aspxhome.com/file/2023/4/121294_0s.png)