• 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');*/
  • 相关阅读:
    python 学习记录
    Oracle Spatial构建自定义投影坐标系
    GeoWebCache参数之pixelSize(像素大小)
    MapViewer提示错误之Cannot read USER_SDO_TILE_ADMIN_TASKS view from database
    《微软ASP.NET和AJAX:构建网络应用程序》
    Microsoft Dynamics CRM 2011 beta 今天发布!
    重要:欢迎光临新版微软中文CRM论坛!
    快讯:CRM Update Rollup(升级包) 每8周一个!
    微软的云策略
    风向标:Microsoft Dynamics 与企业生产力
  • 原文地址:https://www.cnblogs.com/liuxd/p/13093810.html
Copyright © 2020-2023  润新知