将PHPExcel文件解析为HTML

时间:2013-02-18 16:16:13

标签: php phpexcel

我有一个上传页面供用户上传excel文件,这是任务请求类型的一部分。我试图这样做,所以当提交任务时,你可以点击一个链接,然后显示他们上传的excel文件的html表。 PHP-excel-reader可以很好地工作,但它不支持xlsx文件。我正在看PHPExcel,但不太明白我如何获取这些输出并从中创建一个html表。我也担心我不能在PHP中支持ZipArchive。

有没有人知道他们将PHPExcel示例转换为html表的示例?

修改

以下代码效果很好。它在脚本运行时创建一个新文件。我无法弄清楚如何正确地重命名文件。目前,它正在将包含以下代码的实际phpexcel.php文件重命名为phpexcel.htm。我想取$inputFileName的名称并将其重命名为exceluploads/Book7.htm。我不确定它是否像改变$objWriter->save(path/name.htm);一样简单但是没有用,我收到了:

Warning: fopen(/exceluploads/Book7.htm) [function.fopen]: failed to open stream: No such file or directory in /var/www/Classes/PHPExcel/Writer/HTML.php on line 164

Fatal error: Uncaught exception 'Exception' with message 'Could not open file /exceluploads/Book7.htm for writing.'

代码:

    <?php

    /** Error reporting */
    error_reporting(E_ALL);

    /** Include PHPExcel */
    require_once dirname(__FILE__) . '/Classes/PHPExcel.php';


    // Create new PHPExcel object
    echo date('H:i:s') , " Create new PHPExcel object" , PHP_EOL;
    $objPHPExcel = new PHPExcel();

    $inputFileName = 'exceluploads/Book7.xlsx';
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

    echo date('H:i:s') , " Write to HTML format" , EOL;
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
    $objWriter->setSheetIndex(0);
    //$objWriter->setImagesRoot('http://www.example.com');
    $objWriter->save(str_replace('.php', '.htm', __FILE__));

    echo date('H:i:s') , " File written to " , str_replace('.php', '.htm', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;


    // Echo memory peak usage
    echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL;

    // Echo done
    echo date('H:i:s') , " Done writing file" , EOL;
    echo 'File has been created in ' , getcwd() , EOL;

    ?>

1 个答案:

答案 0 :(得分:9)

阅读文件:

$inputFileName = 'example.xls';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);

写一个文件:

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('example.html');