小編給大家分享一下PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

##導(dǎo)入類(lèi)庫(kù)
require 'PHPExcel/Classes/PHPExcel.php';
require 'PHPExcel/Classes/PHPExcel/Writer/Excel5.php'; //非07格式的寫(xiě)出類(lèi)
##基礎(chǔ)屬性設(shè)定
$objPHPExcel = \PHPExcel_IOFactory::load('a.xls'); //讀入指定excel文件
$objPHPExcel->setActiveSheetIndex(0); //指定活動(dòng)工作表
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setName('宋體');
$objPHPExcel->getProperties()->setTitle('xxx');
##單元格編輯
$objPHPExcel->getActiveSheet()->setCellValue('A3', 'xxx'); //設(shè)定A3單元格值為xxx
##單元格繪圖
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('a.jpg'); //指定圖片路徑。若要遠(yuǎn)程圖片需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文件瀏覽器下載導(dǎo)出
$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顯示(可用于調(diào)試)
$objWriter = new \PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->save('php://output');$filename='a.pdf';
$encoded_filename = rawurlencode($filename);
$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; //指定通過(guò)mpdf類(lèi)庫(kù)導(dǎo)出pdf文件
$rendererLibraryPath = 'PHPExcel/MPDF57'; //指定你下載的mpdf類(lèi)庫(kù)路徑
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導(dǎo)出失敗的一些錯(cuò)誤解決方法
##############################
##1 pdf中文亂碼問(wèn)題
PHPExcel/Classes/PHPExcel/Writer/PDF/mPDF.php:105處加兩行設(shè)定:
$pdf->useAdobeCJK = true;
$pdf->SetAutoFont(AUTOFONT_ALL);
##2 類(lèi)庫(kù)里面多處preg_replace調(diào)用使用了元字符e,而部分低版本php不支持正則表達(dá)式e元字符
e元字符的不當(dāng)使用并導(dǎo)致pdf報(bào)錯(cuò)的觸發(fā)點(diǎn)在類(lèi)庫(kù)里面大概有五六處吧,
由于e元字符是一個(gè)shell下的子進(jìn)程php調(diào)用,所以報(bào)錯(cuò)信息不會(huì)反饋到當(dāng)前php進(jìn)程中,故即便你配置了錯(cuò)誤打印到屏幕, 頁(yè)面也不會(huì)顯示報(bào)錯(cuò)信息, 必須查看php報(bào)錯(cuò)日志
查看php報(bào)錯(cuò)日志,把提示的preg_replace中元字符e的調(diào)用替換為preg_replace_callback形式的調(diào)用
##3 部分版本phpexcel類(lèi)庫(kù)有單元格樣式判斷錯(cuò)誤
lib/PHPExcel/Classes/PHPExcel/Writer/HTML.php:1236處加個(gè)if判斷
if (!$this->_useInlineCss) {
$cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();以上是“PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁(yè)題目:PHPExcel如何實(shí)現(xiàn)mpdf導(dǎo)出-創(chuàng)新互聯(lián)
文章鏈接:http://chinadenli.net/article10/cesddo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、標(biāo)簽優(yōu)化、品牌網(wǎng)站制作、商城網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容