I am dynamically generating a div which is like :
<div id='PrintDiv'>
<table id="mainTable">
<tr>
<td>
Col1
</td>
<td>
Col2
</td>
<td>
Col3
</td>
</tr>
<tr>
<td>
Val1
</td>
<td>
Val2
</td>
<td>
Val3
</td>
</tr>
<tr>
<td>
Val11
</td>
<td>
Val22
</td>
<td>
Val33
</td>
</tr>
<tr>
<td>
Val111
</td>
<td>
Val222
</td>
<td>
Val333
</td>
</tr>
</table>
</div>
And there are lot more elements on the page as well. Now, how can i get a csv file like this :
Col1,Col2,Col3
Val1,Val2,Val3
Val11,Val22,Val33
Val111,Val222,Val333
using jQuery ?
need a file save dailog box too,like this :
Thanks.
You can do that in the client side only, in browser that accept Data URIs:
In your example the Data URI must be:
You can call this URI by:
window.open
window.location
To test, simply copy the URIs above and paste in your browser address bar. Or test the anchor below in a HTML page:
To create the content, getting the values from the table, you can use table2CSV and do:
Most, if not all, versions of IE don't support navigation to a data link, so a hack must be implemented, often with an
iframe
. Using aniFrame
combined withdocument.execCommand('SaveAs'..)
, you can get similar behavior on most currently used versions of IE.