PHPExcel筆記, mpdf導(dǎo)出

字號:


    這篇文章主要介紹了PHPExcel筆記, mpdf導(dǎo)出,需要的朋友可以參考下
    phpexcel常用處理
    ##導(dǎo)入類庫
    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); //指定活動工作表
    $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庫從phpexcel導(dǎo)出pdf文件
    $filename='a.pdf';
    $encoded_filename = rawurlencode($filename);
    $rendererName = \PHPExcel_Settings::PDF_RENDERER_MPDF; //指定通過mpdf類庫導(dǎo)出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導(dǎo)出失敗的一些錯誤解決方法
    ##############################
    ##1 pdf中文亂碼問題
    PHPExcel/Classes/PHPExcel/Writer/PDF/mPDF.php:105處加兩行設(shè)定:
    $pdf->useAdobeCJK = true;
    $pdf->SetAutoFont(AUTOFONT_ALL);
    ##2 類庫里面多處preg_replace調(diào)用使用了元字符e,而部分低版本php不支持正則表達(dá)式e元字符
    e元字符的不當(dāng)使用并導(dǎo)致pdf報錯的觸發(fā)點(diǎn)在類庫里面大概有五六處吧,
    由于e元字符是一個shell下的子進(jìn)程php調(diào)用,所以報錯信息不會反饋到當(dāng)前php進(jìn)程中,故即便你配置了錯誤打印到屏幕, 頁面也不會顯示報錯信息, 必須查看php報錯日志
    查看php報錯日志,把提示的preg_replace中元字符e的調(diào)用替換為preg_replace_callback形式的調(diào)用
    ##3 部分版本phpexcel類庫有單元格樣式判斷錯誤
    lib/PHPExcel/Classes/PHPExcel/Writer/HTML.php:1236處加個if判斷
    if (!$this->_useInlineCss) {
      $cssClass .= ' style' . $pSheet->getCell($endCellCoord)->getXfIndex();