【问题标题】:Dojo data grid to Excel fileDojo 数据网格到 Excel 文件
【发布时间】:2012-08-01 07:44:27
【问题描述】:

我有一个关于将 dojo 数据网格导出到 excel 文件的问题。我已经使用 dojo 导出器和一些 php 代码使其与 csv 文件一起工作。但是,如何将其保存为 excel 文件。我现在介绍 pear 和其他一些库,但必须有与我用于 csv 的解决方案类似的解决方案。此外,当我在 dojo 中创建自己的导出器时,它是否需要比我用于 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;
?> 

【问题讨论】:

  • 什么意思? .csv 一个excel文件,或者最后可以将数据加载到excel中..您的问题是现实中的问题,询问如何将.csv转换为.xls?
  • 虽然 csv 在 excel 中可用,但不是同一个文件。用户案例是,如果我有 excel 进入我的程序;我不能随心所欲地更改文件扩展名。我仍然需要弄清楚如何输入excel文件,但这是另一个仪表。可能是如果有一个服务将 csv 转换为 xls 然后提示用户保存......
  • 这不适合javascript引擎,但在某种程度上可能。而是找出 csv2xls 过程,用合适的类查看答案

标签: php excel spreadsheet dojox.grid


【解决方案1】:

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

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

设置很简单,大部分设置都是通过.csv文件作为下载附件传递,试试下面的代码。

CSV 到 XLS 的转换

首先设置文件 csv-data 和 classes

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

【讨论】:

  • 感谢您的回复,这是一个很好的起点。如果有效,我会告诉你们。
猜你喜欢
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 1970-01-01
  • 1970-01-01
  • 2010-12-15
  • 2012-05-29
  • 2023-04-03
相关资源
最近更新 更多