PHPExcel筆記, mpdf導(dǎo)出
來(lái)源:易賢網(wǎng) 閱讀:1200 次 日期:2016-08-25 15:12:52
溫馨提示:易賢網(wǎng)小編為您整理了“PHPExcel筆記, mpdf導(dǎo)出”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了PHPExcel筆記, mpdf導(dǎo)出,需要的朋友可以參考下

phpexcel常用處理

##導(dǎo)入類庫(kù)

require 'PHPExcel/Classes/PHPExcel.php';

require 'PHPExcel/Classes/PHPExcel/Writer/Excel5.php'; //非07格式的寫出類

##基礎(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');

利用mpdf庫(kù)從phpexcel導(dǎo)出pdf文件

$filename='a.pdf';

$encoded_filename = rawurlencode($filename);

$rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; //指定通過(guò)mpdf類庫(kù)導(dǎo)出pdf文件

$rendererLibraryPath = 'PHPExcel/MPDF57'; //指定你下載的mpdf類庫(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 類庫(kù)里面多處preg_replace調(diào)用使用了元字符e,而部分低版本php不支持正則表達(dá)式e元字符

e元字符的不當(dāng)使用并導(dǎo)致pdf報(bào)錯(cuò)的觸發(fā)點(diǎn)在類庫(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類庫(kù)有單元格樣式判斷錯(cuò)誤

lib/PHPExcel/Classes/PHPExcel/Writer/HTML.php:1236處加個(gè)if判斷

if (!$this->_useInlineCss) {

  $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:PHPExcel筆記, mpdf導(dǎo)出
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)