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');*/

 

posted @ 2020-06-11 16:01  怪-人  阅读(1418)  评论(0编辑  收藏  举报