Flex代码:
/**** * 导出excel的方法 * */ private function exportExcel():void{ ExportExcel.loadDGInExcel(this.dataGrid,'ExcelExport.jsp'); } <mx:DataGrid id="dataGrid" allowMultipleSelection="true" selectable="true" verticalScrollPolicy="on" x="0" y="0" width="100%" doubleClickEnabled="true" itemDoubleClick="dataGridDoubleClick(event)" height="85%" dataProvider="{attributeDefineList}" textAlign="center" > <mx:columns> <mx:DataGridColumn headerText="属性类型ID" dataField="typeDescription"/> <mx:DataGridColumn headerText="原子属性" dataField="ifatom" labelFunction="yesNoVerdict" /> <mx:DataGridColumn headerText="依赖考试生成" dataField="ifrelytestcreate" labelFunction="yesNoVerdict" /> <mx:DataGridColumn headerText="依赖考试过滤" dataField="ifrelytestfilter" labelFunction="yesNoVerdict" /> <mx:DataGridColumn headerText="属性内部代码" dataField="adcode"/> <mx:DataGridColumn headerText="属性业务编码" dataField="adbcode"/> <mx:DataGridColumn headerText="名称" dataField="name"/> <mx:DataGridColumn headerText="简称" dataField="shortname"/> <mx:DataGridColumn headerText="多/单根节点" dataField="ifsinglenode" labelFunction="yesNoVerdict" /> <mx:DataGridColumn headerText="有无具体值" dataField="isvalue" labelFunction="yesNoVerdict" /> <mx:DataGridColumn headerText="实现方法" dataField="implementation"/> <mx:DataGridColumn headerText="操作" itemRenderer="ui.form.ufmDTAttrDefineOper" /> </mx:columns> </mx:DataGrid>
ExportExcel.as脚本:
package util { import flash.errors.*; import flash.events.*; import flash.external.*; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; import flash.net.navigateToURL; import mx.controls.Alert; import mx.controls.DataGrid; import mx.controls.dataGridClasses.DataGridColumn; public class ExportExcel { public function ExportExcel() { } /** 在导出数据的时候有可能出现单元格数据长度过长而导致Excel在显示时 * 出现科学计数法或者#特殊符号,在此设置单元格宽度比例WIDTHSCALE,在 * 代码中每个单元格的宽度扩展适当的比例值WIDTHSCALE。 */ private static var WIDTHSCALE:Number=2.0; /** * 将DataGrid转换为htmltable * @author rentao * @param: dg 需要转换成htmltable的DataGrid * @return: String */ private static function convertDGToHTMLTable(dg:DataGrid):String { //设置默认的DataGrid样式 var font:String = dg.getStyle('fontFamily'); var size:String = dg.getStyle('fontSize'); var str:String = ''; var colors:String = ''; var style:String = 'style="font-family:'+font+';font-size:'+size+'pt;"'; var hcolor:Array; //检索DataGrid的 headercolor if(dg.getStyle("headerColor") != undefined) { hcolor = [dg.getStyle("headerColor")]; } else { hcolor = dg.getStyle("headerColors"); } // Alert.show(hcolor+""); var x :String = "" ; if(hcolor == null){ x = "0x323232" ; }else{ x = Number((hcolor[0])).toString(16); } // str+= '<table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'" mce_style="background-color:#' +Number((hcolor[0])).toString(16)+'">'; str+= '<table align="center" width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +x+'" mce_style="background-color:#' +x+'">'; //设置tableheader数据(从datagrid的header检索headerText信息) for(var i:int = 0;i<dg.columns.length;i++) { colors = dg.getStyle("themeColor"); if(dg.columns[i].dataField == null || dg.columns[i].dataField == ""){}else{ //表示不是操作列 if(dg.columns[i].headerText != undefined) { str+="<th "+style+">"+dg.columns[i].headerText+"</th>"; } else { str+= "<th "+style+">"+dg.columns[i].dataField+"</th>"; } } } str += "</tr></thead>"; colors = dg.getStyle("alternatingRowColors"); for(var j:int =0;j<dg.dataProvider.length;j++) { str+="<tr width=/""+Math.ceil(dg.width)+"/">"; for(var k:int=0; k < dg.columns.length; k++) { if(dg.dataProvider.getItemAt(j) != undefined && dg.dataProvider.getItemAt(j) != null) { if((dg.columns[k] as DataGridColumn).dataField == null || (dg.columns[k] as DataGridColumn).dataField == ""){}else{ //表示的是字段列 if((dg.columns[k] as DataGridColumn).labelFunction != undefined) { var dataGridColumn:DataGridColumn = dg.columns[k] as DataGridColumn ; // str += "<td width=/""+Math.ceil((dg.columns[k] as DataGridColumn).width*WIDTHSCALE)+"/" "+style+">"+(dg.columns[k] as DataGridColumn).labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+"</td>"; str += "<td align='center' width=/""+Math.ceil(dataGridColumn.width*WIDTHSCALE)+"/" "+style+">"+dataGridColumn.labelFunction(dg.dataProvider.getItemAt(j),dataGridColumn)+"</td>"; // //"+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+" } else { str += "<td align='center' width=/""+Math.ceil(dg.columns[k].width*WIDTHSCALE)+"/" "+style+">"+dg.dataProvider.getItemAt(j)[(dg.columns[k] as DataGridColumn).dataField]+"</td>"; } } } } str += "</tr>"; } str+="</table>"; return str; } /** * 将制定的DataGrid加载到Excel文件,此方法传入一个htmltable字符串参数到后台Script脚本,然后浏览器给用户提供一个Excel下载 * @author Chenwenfeng * @params dg 需要导入的数据源DataGrid * @params url excel文件下载路径 */ public static function loadDGInExcel(dg:DataGrid,url:String):void { //设置URLVariables参数变量,动态增加属性htmltable var variables:URLVariables = new URLVariables(); variables.htmltable = convertDGToHTMLTable(dg); var u:URLRequest = new URLRequest(url); u.data = variables; u.method = URLRequestMethod.POST; navigateToURL(u,"_self"); //"_self" 指定当前窗口中的当前帧。 //"_blank" 指定一个新窗口。 //"_parent" 指定当前帧的父级。 //"_top" 指定当前窗口中的顶级帧。 } } }
ExcelExport.jsp:
<%@ page language="java"%> <%@ page contentType="application/msexcel;charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("UTF-8"); response.setHeader("Content-disposition","attachment; filename=excel.xls"); String str = request.getParameter("htmltable"); out.print(str); %>
中文问题: 查看源代码时发现JSP文件中写死的中文为乱码,则在JSP文件:
<%@ page contentType="text/html; charset=GBK" %> <% response.setContentType("application/msexcel;charset=UTF-8"); %> <HTML> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <head><title>Test</title></head> <body> <% request.setCharacterEncoding("UTF-8"); response.setHeader("Content-disposition","attachment; filename=excel.xls"); String str = request.getParameter("htmltable"); out.print(str); %> </body> </HTML>