Moja Ra Moja Ra - 28 days ago 6
PHP Question

How can I create functionality where html can be exported to a CSV file?

I have the following html file:

<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, 227, 235);">
Date of Service
</th>
<th style="background-color: rgb(215, 227, 235);">
Provider
</th>
<th style="background-color: rgb(215, 227, 235);">
Claim Type
</th>
<th style="background-color: rgb(215, 227, 235);">
Status
</th>
<th style="background-color: rgb(215, 227, 235);">
Billed Amount
</th>
<th style="background-color: rgb(215, 227, 235);">
Paid by Plan
</th>
<th style="background-color: rgb(215, 227, 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, 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>


I would like the end user to be able to clink on a link in the file like the following:

<a id="link3" class="links" href="home.html">Export to Microsoft Excel</a>


When they click on it I would like the table to be exported as a csv file. How can I accomplish this? Can I do this with Javascript or PHP or whatever?

Answer

Consider using the jQuery plugin: HTML Table to CSV

It's a one-liner:

$('#mytable').table2CSV();

You can see this in action at this HTML Table To CSV demo.