I have a question about exporting dojo data grid to excel file. I have made it work with csv file using the dojo exporter and some php code. However, how do I make it to save as excel file. I now about pear and some other libraries, but there has to be similar solution to the one I am using for the csv. Also, when I create my own exporter in dojo, does it need to have something more specific then the code I am using for the csv exporter. Also, what do I need to change in the php code to make it save as xls. The code is below. Thanks a lot in advance.
My dojo exporter:
function exportCsv(){
var g = dijit.byId("grid");
g.exportGrid("csv",{
writerArgs: {
separator: ","
}
}, function(str){
var form = document.createElement('form');
dojo.attr(form, 'method', 'POST');
document.body.appendChild(form);
dojo.io.iframe.send({
url: "csv.php",
form: form,
method: "POST",
content: {exp: str},
timeout: 15000
});
document.body.removeChild(form);
});
}
My php code working with csv:
<?
$time = time();
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=\"grid_$time.csv\"");
$exportedData = $_POST['exp'];
echo stripslashes($exportedData);
exit;
?>
Here is a nice PHP tool, well suited for the purpose.
http://www.phpclasses.org/package/1919-PHP-Stream-wrapper-to-read-and-write-MS-Excel-files.html
The setup is quite simple, you have most of it setup to passthrough the .csv file as download attachment allready, try the following code.
Conversion of CSV to XLS
First setup files csv-data and classes
Making sure, everything is looking nicely
Looping lines in remaining contents of csv-data
Now, we write data to the export tmp-file and conversion is done
Throughputting the outputted tmp-file to client
Good luck and enjoy 😀