function excel(grid){
/**
* @ grid gridPanel 要导出的grid
* @ 客户端必须使用IE浏览器,并且安装了Excel2003
* @ CreatData : 2010-1-20
*/
//提取grid的列
var columns = grid.getColumnModel().config.columns ? grid.getColumnModel().config.columns : grid.getColumnModel().config;
//提取store以及相关参数
var store = grid.getStore();
var colCount = columns.length;//字段数量
var Cols = new Array(colCount);//为导出字段参数做准备
var storeCount = store.getCount();//记录数量
//初始化一个可用ID
var gridID = grid.getId()+'ExcelObj';
//OWC的HTML脚本
var sheetHtml = '<OBJECT id="'+ gridID +'" height="100%" width="100%" classid="clsid:0002E559-0000-0000-C000-000000000046"><PARAM NAME="EnableAutoCalculate" VALUE="-1"><PARAM NAME="DisplayTitleBar" VALUE="0"><PARAM NAME="DisplayToolbar" VALUE="-1"><PARAM NAME="ViewableRange" VALUE="1:65536"></OBJECT>';
var win = new Ext.Window({
title:'ExcelWindows',
closable:true,
width:600,
height:350,
plain:true,
html:sheetHtml
});
win.show(this);
//从OWC的HTML脚本获得OWC对象
var xlBook = document.getElementById(gridID);
var xlsheet = xlBook.ActiveSheet;
//用grid的数据生成HTML的table
var tableHtml = "<table><tr>";
for(var i = 1; i < colCount-3; i++){//生成表头,并读取列参数
tableHtml += "<td width=/"" + columns[i].width + "/">" + columns[i].header + "</td>";
//var col = new Object();
//col.dateIndex = columns[i].dataIndex;
//col.renderer = columns[i].renderer;
//Cols[i] = col;
}
tableHtml += "</tr>";
//生成表格数据体
for(var i = 0; i < storeCount; i++){//行
var tmpTR = "";
tmpTR += "<tr>";
for(var j = 1; j < colCount-3; j++){//列
var date = store.getAt(i);
var tmpTD = "";
if(columns[j].renderer){
tmpTD = "<td>"+ columns[j].renderer(date.get(columns[j].dataIndex)) +"</td>";
}else{
tmpTD = "<td>"+ date.get(columns[j].dataIndex) +"</td>";
}
tmpTR += tmpTD;
}
tmpTR += "</tr>";
tableHtml += tmpTR;
}
tableHtml += "</table>";
//将HTML数据绑定给OWC
xlBook.HTMLData = tableHtml;
//设置OWC的外观参数
var xlsheet = xlBook.ActiveSheet;
xlsheet.Columns.Font.Size = 9;
//销毁对象
xlBook=null;
xlsheet=null;
}
grid to excel
最新推荐文章于 2021-03-17 08:27:55 发布