phpexcel常用处理
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
##导入类库
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' );
|
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
$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();
|
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 96
-
2025-06-04 46
-
详解Java中Comparable和Comparator接口的区别
2025-05-29 40 -
2025-06-04 97
-
H5官网建站服务器的云服务优势:为什么越来越多的企业选择云服务器?
2025-06-05 95
热门评论