PHP Excel Word 文件轉 HTML輸出


ob_end_clean();
$filePath = './123.xls';
//$filePath = './123.docx';
$savePath = './123.html'; //這里記得將文件名包含進去



//excel 轉html 單個Sheet
$fileType = \PHPExcel_IOFactory::identify($filePath); //文件名自動判斷文件類型
$objReader = \PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($filePath);
$objWriter = new \PHPExcel_Writer_HTML($objPHPExcel);
$objPHPExcel = $objReader->load($filePath);
$sheetIndex = $objPHPExcel->getSheetCount();
//dump($sheetIndex);exit;
$objWriter->setSheetIndex(0); //可以將括號中的0換成需要操作的sheet索引
//$objWriter->save($savePath); //保存為html文件
$objWriter->save('php://output');



//excel 轉html 多個Sheet
/*$a = \PhpOffice\PhpSpreadsheet\IOFactory::load($filePath);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html($a);
$writer->writeAllSheets();
$hdr = $writer->generateHTMLHeader();
$sty = $writer->generateStyles(false); // do not write <style> and </style>
$newstyle = '<style type="text/css">'.$sty.'</style>';
echo preg_replace('@</head>@', "$newstyle\n</head>", $hdr);
echo $writer->generateHTMLAll();
$writer->save('php://output');*/



/*word docx  轉HTML輸出
$objReader = \PhpOffice\PhpWord\IOFactory::createReader();
$objPHPFile = $objReader->load($filePath);
$objPHPFile->setDefaultFontName('Times New Roman');
$objPHPFile->setDefaultFontSize(16);
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($objPHPFile, 'HTML');
$html = $objWriter->getContent();
//取head標簽內容
preg_match("/<head>(.*)<\/head>/si",$html,$match);
$head = trim($match[0]);
$count = strpos($head,"</head>");
$str = substr_replace($head,"",$count,strlen('</head>'));
$str2 = $str.'<style>img{display: block;margin: 20px auto;}</style></head>';
$strer = preg_replace("/<head>(.*)<\/head>/si",$str2,$html);
return $strer;
//$objWriter->save('php://output');*/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM