ExtJs Grid导出到Excel(修正版)
以下的几个问题我都已经整理和修改:
1、没有考虑到含有序号和选择框的grid,
2、utf8转换bug.
3、宽度的bug
4、不支持ie6、ie7和Safari
修改后的代码见附件的gridToExcel.js.zip
注:我的文件编码都是utf8格式的(no bom),需要其它格式的请自行修改。
使用方法更简单,不需要定义linkbutton,直接使用标准的button
01.
new
Ext.Button({
02.
text:
'导出到Excel'
,
03.
handle:
function
() {
04.
var
vExportContent = grid.getExcelXml();
05.
if
(Ext.isIE6 || Ext.isIE7 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) {
06.
if
(! Ext.fly(
'frmDummy'
)) {
07.
var
frm = document.createElement(
'form'
);
08.
frm.id =
'frmDummy'
;
09.
frm.name = id;
10.
frm.className =
'x-hidden'
;
11.
document.body.appendChild(frm);
12.
}
13.
Ext.Ajax.request({
14.
url:
'/exportexcel.php'
,
15.
method:
'POST'
,
16.
form: Ext.fly(
'frmDummy'
),
17.
callback:
function
(o, s, r) {
18.
//alert(r.responseText);
19.
},
20.
isUpload:
true
,
21.
params: {exportContent: vExportContent}
22.
})
23.
}
else
{
24.
document.location =
'data:application/vnd.ms-excel;base64,'
+ Base64.encode(vExportContent);
25.
}
26.
});
以上以php为例,其它语言方法相同
exportexcel.php
01.
<?php
02.
header(
"Pragma: public"
);
03.
header(
"Expires: 0"
);
// set expiration time
04.
header(
"Cache-Control: must-revalidate, post-check=0, pre-check=0"
);
05.
header(
"Content-Type: application/force-download"
);
06.
header(
'Content-type: application/vnd.ms-excel'
);
07.
header(
"Content-Disposition:attachment;filename=export"
);
08.
echo $_REQUEST[
'exportContent'
];
09.
?>