Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  8] [ 4]  / answers: 1 / hits: 66072  / 15 Years ago, thu, may 28, 2009, 12:00:00

I'm using the jQuery Table to CSV Plugin. I've altered the popup so that it tells the browser to download a CSV file.



It was:



function popup(data) {
var generator = window.open('', 'csv', 'height=400,width=600');
generator.document.write('<html><head><title>CSV</title>');
generator.document.write('</head><body >');
generator.document.write('<textArea cols=70 rows=15 wrap=off >');
generator.document.write(data);
generator.document.write('</textArea>');
generator.document.write('</body></html>');
generator.document.close();
return true;
}


I've changed it to:



function popup(data) {
window.location='data:text/csv;charset=utf8,' + encodeURIComponent(data);
return true;
}


It works, for the most part. It still requires that you find your spreadsheet software, and create your own filename...because it creates a strange file name (Example: 14YuskG_.csv.part).



Any suggestions on how to improve this?


More From » jquery

 Answers
4

Found a solution that works (with help from http://www.topsemtips.com/2008/11/save-html-table-to-excel-using-jquery/):



I changed the function to:



function popup(data) {
$(#main div.inner).append('<form id=exportform action=export.php method=post target=_blank><input type=hidden id=exportdata name=exportdata /></form>');
$(#exportdata).val(data);
$(#exportform).submit().remove();
return true;
}


And created the file export.php:



<?php

header(Content-type: application/vnd.ms-excel; name='excel');
header(Content-Disposition: filename=export.csv);
header(Pragma: no-cache);
header(Expires: 0);

print $_REQUEST['exportdata'];

?>


Update:
A more IE7 friendly version:



<?php

header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename=filename.csv');

print $_POST['exportdata'];

?>

[#99440] Sunday, May 24, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
korbindarrionh

Total Points: 598
Total Questions: 113
Total Answers: 104

Location: Burundi
Member since Wed, Nov 25, 2020
4 Years ago
;