如何解决如何创建可以将html导出到CSV文件的功能?
| 我有以下html文件:<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" dir=\"ltr\" lang=\"en\">
<head>
</head>
<body>
<p align=\"center\"><strong>Claims Search Results</strong></p>
<table id=\"ClaimsListPrint\" class=\"printClaims\">
<thead>
<tr style=\"background-color: rgb(241,241,241);\">
<th style=\"background-color: rgb(215,227,235);\">
Member
</th>
<th style=\"background-color: rgb(215,235);\">
Date of Service
</th>
<th style=\"background-color: rgb(215,235);\">
Provider
</th>
<th style=\"background-color: rgb(215,235);\">
Claim Type
</th>
<th style=\"background-color: rgb(215,235);\">
Status
</th>
<th style=\"background-color: rgb(215,235);\">
Billed Amount
</th>
<th style=\"background-color: rgb(215,235);\">
Paid by Plan
</th>
<th style=\"background-color: rgb(215,235);\">
Member Responsiblity
</th>
</tr>
</thead>
<tbody>
<tr style=\"background-color: rgb(255,255,240);\" class=\"claim\">
<td class=\"claim-member\">
John Sample
</td>
<td class=\"claim-dateOfService\">
02/27/2011
</td>
<td class=\"claim-provider\">
TestProv1
</td>
<td class=\"claim-claimType\">
Medical
</td>
<td class=\"claim-status\">
Processed
</td>
<td class=\"claim-billedAmount\">
$145
</td>
<td class=\"claim-paidByPlan\">
$125
</td>
<td class=\"claim-memberResponsibility\">
$25
</td>
</tr>
<tr style=\"background-color: rgb(241,241);\" class=\"claim\">
<td class=\"claim-member\">
John Sample
</td>
<td class=\"claim-dateOfService\">
02/27/2011
</td>
<td class=\"claim-provider\">
TestProv1
</td>
<td class=\"claim-claimType\">
Medical
</td>
<td class=\"claim-status\">
In-Process
</td>
<td class=\"claim-billedAmount\">
-
</td>
<td class=\"claim-paidByPlan\">
-
</td>
<td class=\"claim-memberResponsibility\">
-
</td>
</tr>
</tbody>
</table>
</body>
</html>
我希望最终用户能够按如下所示链接文件中的链接:
<a id=\"link3\" class=\"links\" href=\"home.html\">Export to Microsoft Excel</a>
当他们单击它时,我希望将表导出为csv文件。我该怎么做?我可以使用Javascript或PHP或其他方式执行此操作吗?
解决方法
考虑使用jQuery插件:HTML表到CSV
它是单线的:
$(\'#mytable\').table2CSV();
您可以在此HTML Table To CSV演示中看到此操作。
, 无论您使用哪种语言来生成页面,我都会使用它导出。如果您已经在其他地方拥有数据,则解析HTML页面以生成CSV没有任何意义。页面如何生成?
, 如果要下载此CSV文件,则可以使用PHP修改文档标题,以告知浏览器强制下载文件。我使用以下功能来强制下载现有文件,但是您可以通过进行一些修改将其修改为输出即时生成的CSV文件。另外,您可以保存一个CSV文件并使用它下载它。
顺便说一句,这不是我的原始代码。我在StackOverflow上的其他地方找到了它,对我来说非常有用。
function download($filePath) {
if(headers_sent()) die(\'Headers alread sent. Download cannot initialize.\');
// Required for some browsers
if (ini_get(\'zlib.output_compression\')) ini_set(\'zlib.output_compression\',\'Off\');
// File Exists?
if( file_exists($filePath) ){
// Parse Info / Get Extension
$fsize = filesize($filePath);
$path_parts = pathinfo($filePath);
$ext = strtolower($path_parts[\"extension\"]);
// Determine Content Type
switch ($ext) {
case \"pdf\": $ctype=\"application/pdf\"; break;
case \"exe\": $ctype=\"application/octet-stream\"; break;
case \"zip\": $ctype=\"application/zip\"; break;
case \"doc\": $ctype=\"application/msword\"; break;
case \"xls\": $ctype=\"application/vnd.ms-excel\"; break;
case \"ppt\": $ctype=\"application/vnd.ms-powerpoint\"; break;
case \"gif\": $ctype=\"image/gif\"; break;
case \"png\": $ctype=\"image/png\"; break;
case \"jpeg\":
case \"jpg\": $ctype=\"image/jpg\"; break;
default: $ctype=\"application/force-download\";
}
header(\"Pragma: public\"); // required
header(\"Expires: 0\");
header(\"Cache-Control: must-revalidate,post-check=0,pre-check=0\");
header(\"Cache-Control: private\",false); // required for certain browsers
header(\"Content-Type: $ctype\");
header(\"Content-Disposition: attachment; filename=\\\"\".basename($filePath).\"\\\";\" );
header(\"Content-Transfer-Encoding: binary\");
header(\"Content-Length: \".$fsize);
ob_clean();
flush();
readfile( $filePath );
} else {
die(\'File Not Found\');
}
}
, 在PHP中:
使用PHP的DOMDocument类将HTML文件加载到DOM对象结构中。
解析DomDocument,找到所需的元素,然后将相关值提取到数组中。
使用PHP的fputsvc()函数将数组另存为CSV文件。
, 尽管@ p.campbell是一个很好的解决方案,它可以为您提供格式化的CSV,但不能为您提供CSV下载。如果希望最终用户以CSV格式下载,则可以使用服务器技术(例如PHP)并返回CSV作为响应类型。
我也同意@mrkmg。如果有数据,请编写一些PHP以CSV格式返回数据。
http://mysite.com/data/csv
传递格式化数据,然后在PHP中将响应类型更改为CSV。用户将获得一个很好的“另存为...”对话框。我相信代码看起来像这样:
<?php
// We\'ll be outputting a CSV
header(\'Content-type: application/csv\');
// It will be called downloaded.csv
header(\'Content-Disposition: attachment; filename=\"downloaded.csv\"\');
// Write CSV...
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。