Extjs导出Grid数据到Excel

1、调用方式
var excel = new Ext.Excel({
gridId: 'gridId',--GridId
sheetName: 'sheetName',--sheetName
dsIndex:'oprDate','bizKey','custAbbr','refNo','amount','status','bizItemCn','registerOpAuth','attachCnt','isInCot','remark']--数据源
});
excel.extGridToExcel();--调用方法

2、Export.js
/*
*功能:直接由Ext的grid生成Excel
*注意:只支持IE
*参数:gridId, sheetName
*/
Ext.Excel = function(config){
Ext.apply(this,config);
};

Ext.ExcelNew = function(config){
Ext.apply(this,config);
};

Ext.apply(Ext.Excel.prototype,{
extGridToExcel : function(){
try {
if (Ext.getCmp(this.gridId)) {
// grid 生成 Excel
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var grid = Ext.getCmp(this.gridId);
var store = grid.getStore();
var recordCount = store.getCount();
var view = grid.getView();
var cm = grid.getColumnModel();
var colCount = cm.getColumnCount();
var temp_obj = [];
for (var i = 0; i < colCount;i++) {
if (this.dsIndex) {
for(var xi=0; xi < this.dsIndex.length; xi++){
if (this.dsIndex[xi] == cm.config[i].dataIndex){
temp_obj.push(i);
break;
}
}
} else {
if (cm.isHidden(i)) {

} else {
temp_obj.push(i);
}
}
}
for (var i = 1; i <= temp_obj.length;i++) {
oSheet.Cells(1,i).value = cm.getColumnHeader(temp_obj[i - 1]);
}

for(var i = 1 ; i <= recordCount; i++) {
for(var j = 1; j<= temp_obj.length; j++) {
oSheet.Cells(i+1,j).value = view.getCell(i-1,temp_obj[j-1]).innerText;
}
}
oSheet.Columns.AutoFit;
if (this.sheetName){
oSheet.Name = this.sheetName;
}
oXL.UserControl = true;
oXL.Visible = true;
} else {
Ext.Msg.show({
title : '错误',
msg : '明细数据导出失败!',
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR
});
return;
}
}catch(e){
if (Ext) {
Ext.Msg.show({
title : '提示',
msg : '请设置IE的菜单\'工具\'->Internet选项->安全->自定义级别->\'对未标记为可安全执行脚本ActiveX控件初始化并执行脚本\'->选择[启用]  就可以生成Excel',
buttons : Ext.Msg.OK,
icon : Ext.Msg.INFO
});
} else {
alert('不支持ExtJs框架');
return;
}
}
}
});


Ext.apply(Ext.ExcelNew.prototype,{
extGridToExcel : function(){
try {
if (Ext.getCmp(this.gridId)) {
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;
var grid = Ext.getCmp(this.gridId);
var dsIndexs = this.dsIndex;
var store = grid.getStore();
var recordCount = store.getCount();
var view = grid.getView();
var cm = grid.getColumnModel();
var colCount = cm.getColumnCount();
var temp_obj = [];
for (var i = 0; i < colCount;i++) {
var cmConfig = cm.config;
for(var xi=0;xi if(dsIndexs[xi]==cmConfig[i].dataIndex){
temp_obj.push(i);
break;
}
}
}
for (var i = 1; i <= temp_obj.length;i++) {
oSheet.Cells(1,i).value = cm.getColumnHeader(temp_obj[i - 1]);
}

for(var i = 1 ; i <= recordCount; i++) {
for(var j = 1; j<= temp_obj.length; j++) {
oSheet.Cells(i+1,j).value = view.getCell(i-1,temp_obj[j-1]).innerText;
}
}
if (this.sheetName){
oSheet.Name = this.sheetName;
}
oXL.UserControl = true;
oXL.Visible = true;
} else {
Ext.Msg.show({
title : '错误',
msg : '明细数据导出失败!',
buttons : Ext.Msg.OK,
icon : Ext.Msg.ERROR
});
return;
}
}catch(e){
if (Ext) {
Ext.Msg.show({
title : '提示',
msg : '请设置IE的菜单\'工具\'->Internet选项->安全->自定义级别->\'对未标记为可安全执行脚本ActiveX控件初始化并执行脚本\'->选择[启用]  就可以生成Excel',
buttons : Ext.Msg.OK,
icon : Ext.Msg.INFO
});
} else {
alert('不支持ExtJs框架');
return;
}
}
}
});

你可能感兴趣的:(Extjs导出Grid数据到Excel)