Ext gridPanel 导出到Excel 2

Ext.onReady(function() { var readerCfList = new Ext.data.ArrayReader({}, [ { name: 'id' }, { name: 'name' }, { name: 'date', type: "date", dateFormat: "Y-m-d" } ]); var cfListStore = new Ext.data.Store({ reader: readerCfList, data: [ [1, "123", "2011-01-02"], [2, "223", "2011-05-06"], [3, "323", "2010-12-30"], [4, "423"], [5, "523"], [6, "123"], [7, "223"], [8, "323"], [9, "423"], [10, "523"] ] }); var grid = new Ext.grid.GridPanel({ tbar: [{ text: "导出到Excel", handler: function() { downloadViewData(grid); } }], store: cfListStore, loadMask: { msg: "正在加载..." }, columns: [ //new Ext.grid.RowNumberer({}), {header: "code", width: 120, sortable: true, dataIndex: 'id' }, { header: "name", width: 200, sortable: true, dataIndex: 'name' }, { header: "date", width: 200, sortable: true, dataIndex: 'date', renderer: function(v) { if (v !== "") { var d = new Date(v); return d.format("Y-m-d") } } } ], viewConfig: { forceFit: true, emptyText: '

暂无数据显示
', deferEmptyText: false }, title: "Grid导出到Excel实例", renderTo: document.body, height: 300, autoScroll: true }); //公用下载Grid到Execl方法 function downloadViewData(grid) { 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); xlSheet.name = grid.title; var cm = grid.getColumnModel(); var colCount = cm.getColumnCount(); var temp_obj = []; //只下载没有隐藏的列(isHidden()为true表示隐藏,其他都为显示) //临时数组,存放所有当前显示列的下标 for (i = 0; i < colCount; i++) { if (cm.getColumnHeader(i) == "" || cm.isHidden(i) == true) { //不下载CheckBox列和隐藏列 } else { temp_obj.push(i); } } for (i = 1; i <= temp_obj.length; i++) { //显示列的列标题 xlSheet.Cells(1, i).Value = cm.getColumnHeader(temp_obj[i - 1]); xlSheet.Cells(1, i).Interior.ColorIndex = 15; xlSheet.Cells(1, i).Font.Bold = true; //设置导出的列格式,解决字符串在导出到excel中变成数值型 var fld = grid.getStore().recordType.prototype.fields.get(cm.getDataIndex(i - 1)); if (fld.type.type == "auto") { xlSheet.Columns(i).NumberFormatLocal = "@"; } } var store = grid.getStore(); var recordCount = store.getCount(); var view = grid.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 = 100 xls.UserControl = true; //很重要,不能省略,不然会出问题 意思是excel交由用户控制 xls = null; xlBook = null; xlSheet = null; } });  

你可能感兴趣的:(excel,ext,function,date,header,浏览器)