【问题标题】:Parse PHPExcel file as HTML将 PHPExcel 文件解析为 HTML
【发布时间】:2013-02-03 03:19:04
【问题描述】:

我有一个上传页面供用户上传 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;

    ?>

【问题讨论】:

  • 你看过Tests/Examples目录下的17html.php吗?
  • 我明白了,虽然我不确定为什么我对如何让 PHPExcel 读取某些文件感到如此困惑。我到处找,不知道在哪里可以告诉它读取某个 excel 文件。
  • /exceluploads/Book7.htm !== exceluploads/Book7.htm - 这是文件系统路径,而不是网络服务器路径

标签: php phpexcel


【解决方案1】:

读取文件:

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

写入文件:

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

【讨论】:

  • 我已经编辑了我的原始帖子,似乎没有必要发布一个新帖子,您的示例效果很好,我只是在命名文件时遇到了问题。
猜你喜欢
  • 2011-09-08
  • 2015-10-31
  • 2011-08-12
  • 2013-07-11
  • 2017-04-09
  • 2014-11-12
  • 1970-01-01
  • 2011-03-14
  • 1970-01-01
相关资源
最近更新 更多