Ext Grid 导出Excel

方法一:只导出数据无表头

tabpanel toolbar 调用

xtype : 'tbbutton',
text : '导出',
iconCls : 'toolbar-search',
handler : function(){
var excel = new Ext.Excel({//通过Ext.Excel直接引用到下面的Ext.Excel
gridId : 'typeCodeGrid',
sheetName : '案件统计'
});
excel.extGridToExcel();
}


导出Excel显示

Ext.Excel = function(config){
Ext.apply(this,config);
};
Ext.apply(Ext.Excel.prototype,{
extGridToExcel : function(){
try{
var tabpanel = Ext.getCmp('caseTabs');
var activeId = tabpanel.getActiveTab().getId();
var gridId = "typeCodeGrid";
if(activeId=="typeCode"){
gridId = 'typeCodeGrid';
}else if(activeId=='caseCost'){
gridId = 'caseCostGrid';
}else if(activeId=='natureCode'){
gridId = "natureCodeGrid";
}else if(activeId=='caseType'){
gridId = "caseTypeGrid";
}
if(Ext.getCmp(gridId)){
/******************* grid 生成 Excel ********************/
var oXL = new ActiveXObject("Excel.Application");
var oWB = oXL.Workbooks.Add();
var oSheet = oWB.ActiveSheet;

var grid = Ext.getCmp(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(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;
}
}
if(this.sheetName){
oSheet.Name = this.sheetName;
}
oXL.UserControl = true;
oXL.Visible = true;
}else{
Ext.Msg.alert('Error','明细数据grid没有创建成功!');
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;
}
}
}
});


方法二:导出表头+数据
tabpanel toolbar 调用

xtype : 'tbbutton',
text : '导出',
iconCls : 'toolbar-search',
handler : outputAddress


导出Excel显示

function outputAddress(){
var tabpanel = Ext.getCmp('caseTabs');
var activeId = tabpanel.getActiveTab().getId();
var gridId = Ext.getCmp("typeCodeGrid");
if(activeId=="typeCode"){
gridId = Ext.getCmp('typeCodeGrid');
}else if(activeId=='caseCost'){
gridId = Ext.getCmp('caseCostGrid');
}else if(activeId=='natureCode'){
gridId = Ext.getCmp("natureCodeGrid");
}else if(activeId=='caseType'){
gridId = Ext.getCmp("caseTypeGrid");
}
try {
var xls = new ActiveXObject("Excel.Application");
}
catch (e) {
alert("要打印该表,您必须安装Excel电子表格软件,同时浏览器须使用“ActiveX 控件”,您的浏览器须允许执行控件。 请点击【帮助】了解浏览器设置方法!");
//return "";
}

xls.visible = true; //设置excel为可见
var xlBook = xls.Workbooks.Add;
var xlSheet = xlBook.Worksheets(1);

var cm = gridId.getColumnModel();
var colCount = cm.getColumnCount();
var temp_obj = [];
//只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示)
//临时数组,存放所有当前显示列的下标
for (i = 1; i < colCount; i++) {
if (cm.isHidden(i) == true) {
}
else {
temp_obj.push(i);
}
}
for (i = 1; i <= temp_obj.length - 2; i++) {
//显示列的列标题
xlSheet.Cells(1, i).Value = cm.getColumnHeader(temp_obj[i - 1]);
}
var store = gridId.getStore();
var recordCount = store.getCount();
var view = gridId.getView();
for (i = 1; i <= recordCount; i++) {
for (j = 1; j <= temp_obj.length; j++) {
//EXCEL数据从第二行开始,故row = i + 1;
xlSheet.Cells(i + 1, j).Value = view.getCell(i - 1, temp_obj[j - 1]).innerText;
}
}
xlSheet.Columns.AutoFit;
xls.ActiveWindow.Zoom = 75
xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制
xls = null;
xlBook = null;
xlSheet = null;
}

你可能感兴趣的:(Ext Grid 导出Excel)