PHPExcel筆記, mpdf導出,phpexcelmpdf
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();