• 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 = PhpOfficePhpSpreadsheetIOFactory::load($filePath);
    $writer = new PhpOfficePhpSpreadsheetWriterHtml($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 </head>", $hdr);
    echo $writer->generateHTMLAll();
    $writer->save('php://output');*/



    /*word docx 转HTML输出
    $objReader = PhpOfficePhpWordIOFactory::createReader();
    $objPHPFile = $objReader->load($filePath);
    $objPHPFile->setDefaultFontName('Times New Roman');
    $objPHPFile->setDefaultFontSize(16);
    $objWriter = PhpOfficePhpWordIOFactory::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');*/
  • 相关阅读:
    题解 UVA10213 【How Many Pieces of Land ?】
    NOIP 2018 游记
    POJ 1821 Fence(单调队列优化DP)
    HDU 2196 Computer(经典树形DP)
    POJ 2228 Naptime(DP+环形处理)
    POJ 1742 Coins(多重背包?)
    POJ 2311 Cutting Game(SG函数)
    BZOJ 2560(子集DP+容斥原理)
    HDU2841 Visible Trees(容斥原理)
    HDU 1796 How many integers can you find(容斥原理)
  • 原文地址:https://www.cnblogs.com/liuxd/p/13093810.html
Copyright © 2020-2023  润新知