对于ext grid 导出excel
下面是个简单的列子哦
js代码
var sendform=""; Ext.onReady(function(){ var fm = Ext.form; var isDisplay=false; if(userType==2){ isDisplay=true; } var grid = null; var cm = new Ext.grid.ColumnModel({ defaults: { sortable: true // columns are not sortable by default }, columns: [ { id:'description', header: '日期', dataIndex: 'billDate' },{ header: '单号', dataIndex: 'billCode' },{ header: '部门', dataIndex: 'departName' },{ header: '专供客户', dataIndex: 'KHComSName' },{ header: '供应商', dataIndex: 'ComSName' },{ header: '仓库', dataIndex: 'storeName' },{ header: '发票类型', dataIndex: 'taxRate' },{ header: '物料编号', dataIndex: 'code' }, { header: '物料简称', dataIndex: 'shortName' }, { header: '型号', dataIndex: 'style' }, { header: '封装', dataIndex: 'APPROVALNO' },{ header: '厂家', dataIndex: 'FACTORY' }, { header: '单位', dataIndex: 'unitName' },{ header: '数量', dataIndex: 'unitCount' },{ header: '单价', dataIndex: 'unitPrice', hidden:isDisplay, hideable:isDisplay },{ header: '金额', dataIndex: 'jine', hidden:isDisplay, hideable:isDisplay },{ header: '单价(不含税)', dataIndex: 'priceHan', hidden:isDisplay, hideable:isDisplay },{ header: '金额(不含税)', dataIndex: 'jineHan', hidden:isDisplay, hideable:isDisplay },{ header: '备注', dataIndex: 'memo' },{ header: '操作员', dataIndex: 'userName' },{ header: '业务员', dataIndex: 'emName' },{ header: '审核人', dataIndex: 'userDef1' } ] }); var zlUrl=getRootPath()+'servlet/ReportServlet?change=PROALONE'; var store = new Ext.data.Store({ proxy: new Ext.data.HttpProxy({ url: zlUrl }), reader: new Ext.data.JsonReader({ totalProperty: 'results', root: 'rows', fields: [ {name:'billDate'}, {name:'billCode'}, {name:'departName'}, {name:'KHComSName'}, {name:'ComSName'}, {name:'storeName'}, {name:'taxRate'}, {name: 'code', mapping: 'code'}, {name: 'shortName'}, {name: 'style'}, {name: 'APPROVALNO'}, {name: 'FACTORY'}, {name: 'unitName'}, {name:'unitCount'}, {name:'unitPrice'}, {name: 'jine'}, {name: 'priceHan'}, {name: 'jineHan'}, {name:'memo'}, {name:'userName'}, {name:'emName'}, {name:'userDef1'} ] }) }); function gridToExce(fileName){//这里是主要的 var vExportContent = grid.getExcelXml(); if (Ext.isIE8||Ext.isIE6 || Ext.isIE7 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) { var fd=Ext.get('frmDummy'); if (!fd) { fd=Ext.DomHelper.append(Ext.getBody(),{tag:'form',method:'post',id:'frmDummy',action:'exportexcel.jsp', target:'_blank',name:'frmDummy',cls:'x-hidden',cn:[ {tag:'input',name:'fileName',id:'fileName',type:'hidden'}, {tag:'input',name:'exportContent',id:'exportContent',type:'hidden'} ]},true); } fd.child('#fileName').set({value:fileName}); fd.child('#exportContent').set({value:vExportContent}); fd.dom.submit(); } else { document.location = 'data:application/vnd.ms-excel;base64,'+Base64.encode(vExportContent); } } grid = new Ext.grid.EditorGridPanel({ autoScroll:true, store: store, sm : new Ext.grid.RowSelectionModel(), cm: cm, renderTo: 'editor-grid', width: Ext.get("content").getWidth() , height: Ext.get("content").getHeight(), autoExpandColumn : 'description', autoWidth:true, title: '采购订单查询', frame: true, viewConfig:{ forceFit:true, autoFill:true, sortAscText:'升序', sortDescText:'降序', columnsText:'显示列' }, clicksToEdit: 1, tbar:[{ text: '导出到Excel', handler: function() {gridToExce('exportExcel')} },{ text: '查询', handler:function(){displayture()} } ] }); store.load({ params:{ billdates:Ext.get('billdates').dom.value, billdatee:Ext.get('billdatee').dom.value, billCode:Ext.get('billCode').dom.value, departid:Ext.get('departid').dom.value, khwlid:Ext.get('khwlid').dom.value, wlid:Ext.get('wlid').dom.value, storeid:Ext.get('storeid').dom.value } }); window.onresize = function(){ grid.setWidth(0); grid.setHeight(0); grid.setWidth(Ext.get("content").getWidth()); grid.setHeight(Ext.get("content").getHeight()); }; sendform=function(){ displayfalse(); store.load({ params:{ billdates:Ext.get('billdates').dom.value, billdatee:Ext.get('billdatee').dom.value, billCode:Ext.get('billCode').dom.value, departid:Ext.get('departid').dom.value, khwlid:Ext.get('khwlid').dom.value, wlid:Ext.get('wlid').dom.value, storeid:Ext.get('storeid').dom.value } }); } });
类代码exportexcel.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%
String content = request.getParameter("exportContent");
String fileName = request.getParameter("fileName");
content = new String(content.getBytes("ISO8859_1"));
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
response.setHeader("Content-Type","application/force-download");
response.setHeader("Content-Type","application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+fileName+".xls");
out.print(content);
%>
OK 就完成啦..