Dojo数据网格到Excel文件

我有一个关于将dojo数据网格导出为ex​​cel文件的问题。 我已经使用dojo导出器和一些php代码使用csv文件。 但是,如何将其保存为excel文件。 我现在谈梨和其他一些图书馆,但必须有类似的解决scheme,我正在使用的CSV。 另外,当我在dojo中创build自己的导出器时,是否需要更具体一些,然后使用csv导出器的代码。 另外,我需要在php代码中进行更改以使其保存为xls。 代码如下。 非常感谢。

我的道场出口商:

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); }); } 

我的PHP代码与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; ?> 

这是一个不错的PHP工具,非常适合这个目的。

http://www.phpclasses.org/package/1919-PHP-Stream-wrapper-to-read-and-write-MS-Excel-files.html

安装过程非常简单,你可以将大部分安装程序设置为通过.csv文件作为下载附件安装,请尝试下面的代码。

CSV转换为XLS

首先设置文件csv-data和类

 require_once "excel.php"; define('_CSV_SEPARATOR_', ','); // the excel class setsup xlsfile stream writer, point it to a tmp file $export_file = "xlsfile://tmp/example.xls"; // the csv-contents must be put into an array, // serialized and sent to the stream $import_file = "/path/to/CSV_FILE.csv"; $import=explode("\n", file_get_contents($import_file)); // column names should be first line $header = array_shift($import); 

确保一切都很好看

 $header = explode(_CSV_SEPARATOR_, $header); for($i = 0; $i < count($header); $i++) $header[$i] = trim($header[$i]); 

在csv-data的剩余内容中循环行

 // rest of text is data, split em up and list them with array indices, // and associative names as key in the rowdata $assocData = array(); foreach($import as $line) { $row = explode(_CSV_SEPARATOR_, $line); $rowData = array(); $unknowncount = 0; for($i = 0; $i < count($row); $i++) { if(!empty($header[$i])) $column = $header[$i]; else $column = 'UNK'.$unknowncount++; $rowData[$column] = trim($row[$i]); } $assocData[]=$rowData; } 

现在,我们将数据写入导出tmp文件并完成转换

 $fp = fopen($export_file, "wb"); if (!is_resource($fp)) { die("Cannot open $export_file"); } fwrite($fp, serialize($assocData)); fclose($fp); 

将输出的tmp文件传输到客户端

 $export_file = "xlsfile://tmp/example.xls"; header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); header ("Cache-Control: no-cache, must-revalidate"); header ("Pragma: no-cache"); header ("Content-type: application/x-msexcel"); header ("Content-Disposition: attachment; filename=\"" . basename($export_file) . "\"" ); header ("Content-Description: PHP/INTERBASE Generated Data" ); readfile($export_file); exit; 

祝你好运,享受:D