没有使用 phpspreadsheet 从 html 表形成 excel 文件的文档。
使用“jquery.table2excel.js”可以做到这一点,但它看起来很旧;制作旧的 excel 文件并对文件发出警告。
phpspreadsheet 制作了一个很好的 excel 文件,但我找不到任何答案来执行该功能。
没有使用 phpspreadsheet 从 html 表形成 excel 文件的文档。
使用“jquery.table2excel.js”可以做到这一点,但它看起来很旧;制作旧的 excel 文件并对文件发出警告。
phpspreadsheet 制作了一个很好的 excel 文件,但我找不到任何答案来执行该功能。
使用 HTML 阅读器将数据读入电子表格,然后使用适当的编写器(xls 或 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');
您可以在此链接中阅读更多内容
您可以使用 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>