【问题标题】:phpsreadsheet create excel from html tablephpspreadsheet 从 html 表创建 excel
【发布时间】:2018-08-03 11:08:26
【问题描述】:

没有关于使用 phpspreadsheet 从 html 表格形成 excel 文件的文档。

使用“jquery.table2excel.js”可以做到这一点,但它似乎很老了;生成旧的 excel 文件并对文件发出警告。

phpspreadsheet 制作了一个很好的 excel 文件,但我找不到任何答案来执行该功能。

【问题讨论】:

    标签: phpspreadsheet


    【解决方案1】:

    如果您从预渲染的 HTML 内容生成 Excel 文件,您可以使用 HTML 阅读器自动执行此操作。当您从将下载/发送给用户的 Web 应用程序内容生成 Excel 文件时,这非常有用。

    $htmlString = '<table>
                      <tr>
                          <td>Hello World</td>
                      </tr>
                      <tr>
                          <td>Hello<br />World</td>
                      </tr>
                      <tr>
                          <td>Hello<br>World</td>
                      </tr>
                  </table>';
    
    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
    $spreadsheet = $reader->loadFromString($htmlString);
    
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
    $writer->save('write.xls'); 
    

    你可以在link阅读更多内容

    【讨论】:

      【解决方案2】:

      您可以使用 sheetJS 库将自定义 html 表格转换为 xslx。

      工作代码示例:

      <script type="text/javascript" src="//unpkg.com/xlsx/dist/shim.min.js"></script>
      <script type="text/javascript" src="//unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
      
      <script type="text/javascript" src="//unpkg.com/blob.js@1.0.1/Blob.js"></script>
      <script type="text/javascript" src="//unpkg.com/file-saver@1.3.3/FileSaver.js"></script>
      
      
      <div id="container2">
        <title>SheetJS Table Export</title>
        <table id="data-table">
          <tr>
            <td>ID</td>
            <td>Name</td>
          </tr>
          <tr>
            <td>1</td>
            <td>Johnny</td>
          </tr>
        </table>
      </div>
      <p id="xportxlsx" class="xport"><input type="submit" value="Export to XLSX!" onclick="doit('xlsx');"></p>
      
      
      <script type="text/javascript">
      
      function doit(type, fn, dl) {
          var elt = document.getElementById('data-table');
          var wb = XLSX.utils.table_to_book(elt, {sheet:"Sheet JS"});
          return dl ?
              XLSX.write(wb, {bookType:type, bookSST:true, type: 'base64'}) :
              XLSX.writeFile(wb, fn || ('test.' + (type || 'xlsx')));
      }
      
      
      function tableau(pid, iid, fmt, ofile) {
          if(typeof Downloadify !== 'undefined') Downloadify.create(pid,{
                  swf: 'downloadify.swf',
                  downloadImage: 'download.png',
                  width: 100,
                  height: 30,
                  filename: ofile, data: function() { return doit(fmt, ofile, true); },
                  transparent: false,
                  append: false,
                  dataType: 'base64',
                  onComplete: function(){ alert('Your File Has Been Saved!'); },
                  onCancel: function(){ alert('You have cancelled the saving of this file.'); },
                  onError: function(){ alert('You must put something in the File Contents or there will be nothing to save!'); }
          });
      }
      tableau('xlsxbtn',  'xportxlsx',  'xlsx',  'test.xlsx');
      
      </script>
      

      【讨论】:

        【解决方案3】:

        使用 HTML 阅读器将数据读入电子表格,然后使用适当的编写器(xls 或 xlsx)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-07
          • 2015-08-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-11
          • 2014-09-22
          • 2019-07-01
          相关资源
          最近更新 更多