Phpspreadsheet phpsreadsheet从html表创建excel

Phpspreadsheet phpsreadsheet从html表创建excel,phpspreadsheet,Phpspreadsheet,没有使用PHPSReadSheet从html表形成excel文件的文档 使用“jquery.table2excel.js”可以做到这一点,但它似乎很旧;生成旧excel文件并发出有关该文件的警告 phpspreadsheet是一个很好的excel文件,但是我找不到任何答案来执行此功能。使用HTML阅读器将数据读入电子表格,然后使用适当的编写器(xls或xlsx)您可以使用sheetJS库将自定义HTML表转换为xslx 工作代码示例: <script type="text/javascri

没有使用PHPSReadSheet从html表形成excel文件的文档

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


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

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

您可以使用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>

表格导出
身份证件
名称
1.
约翰尼

函数doit(类型、fn、dl){ var elt=document.getElementById('data-table'); var wb=XLSX.utils.table_to_book(elt,{sheet:“sheet JS”}); 返回dl? write(wb,{bookType:type,bookst:true,type:'base64'}): writeFile(wb,fn | | |('test.+(type | |'XLSX')); } 功能表(pid、iid、fmt、ofile){ 如果(typeof Downloadify!=“未定义”)Downloadify.create(pid{ swf:'downloadify.swf', downloadImage:'download.png', 宽度:100, 身高:30, 文件名:ofile,数据:function(){返回doit(fmt,ofile,true);}, 透明:假, 附加:false, 数据类型:“base64”, onComplete:function(){alert('您的文件已保存!');}, onCancel:function(){alert('您已取消保存此文件');}, onError:function(){alert('您必须在文件内容中放入一些内容,否则将无法保存任何内容!';} }); } tableau('xlsxbtn','xportxlsx','xlsx','test.xlsx');
如果要从预呈现的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'); 
$htmlString='1!'
你好,世界
你好
世界 你好
世界 '; $reader=new\PhpOffice\PhpSpreadsheet\reader\Html(); $spreadsheet=$reader->loadFromString($htmlString); $writer=\PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet,'Xls'); $writer->save('write.xls');
你可以在这本书里读到更多