[英]Export PHP table in a HTML file
I need your help. 我需要你的帮助。 I have a php page that through a series of queries populates a table.
我有一个php页面,它通过一系列查询来填充表格。
I function, which through a click of a button I export the table to Excel. 我可以通过单击按钮将表格导出到Excel。 I need to export this table in an html file always clicking on a button.
我需要始终在按钮上单击以html文件形式导出此表。
How could I do? 我该怎么办?
It is 3 a easy step process: 这是3个简单的步骤:
1 - The first thing you have to do is add an application/vnd.ms-excel content-type to your PHP file: 1-您要做的第一件事是将application / vnd.ms-excel内容类型添加到您的PHP文件中:
header("Content-Type: application/vnd.ms-excel");
2 - Data is just as simple. 2-数据同样简单。 Separate cells/columns with tabs (“\\t” in PHP) and move to the next row with a newline (“\\n” in PHP).
用制表符(PHP中的“ \\ t”)分隔单元格/列,并用换行符(PHP中的“ \\ n”)移至下一行。
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
separated the tabs (\\t) and the new lines (\\n) so you can easily see the structure. 分隔了制表符(\\ t)和换行(\\ n),以便您轻松查看结构。 The above would create a spreadsheet that looks like this:
上面将创建一个电子表格,如下所示:
First Name Last Name Phone
John Doe 555-5555
Step 3: Downloading the Spreadsheet 步骤3:下载电子表格
header("Content-disposition: attachment; filename=spreadsheet.xls");
Download the example files from here . 从此处下载示例文件。
You can also use PHPExcel to generate excel files. 您还可以使用PHPExcel生成excel文件。
I use this Java script function 我使用这个Java脚本功能
[link]<script type="text/javascript">
var tableToExcel = (function()
{
var uri = 'data:application/vnd.ms-excel;base64,'
, template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><meta http-equiv="content-type" content="text/plain; charset=UTF-8"/></head><body><table>{table}</table></body></html>'
, base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
, format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
return function(table, name)
{
if (!table.nodeType) table = document.getElementById(table)
var ctx = {worksheet: name || 'Worksheet', table: table.innerHTML}
window.location.href = uri + base64(format(template, ctx))
}
})()
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.