Flex导出Excel方法与乱码解决.doc_第1页
Flex导出Excel方法与乱码解决.doc_第2页
Flex导出Excel方法与乱码解决.doc_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

原理是把DataGrid中的数据发送到Server端,然后通过Jsp中的attachment; filename=VD.xls 和 contentType=application/msexcel; charset=UTF-8 使其返回EXCEL代码有一个问题,因为我在DataGrid中有过滤的,当DataGrid为全量数据的时候,导出的Excel没有问题,但是当过滤一部分数据的时候,导出的Excel就是乱码了的。解决办法是把设置response.setCharacterEncoding(GBK);大概是Excel不懂UTF-8吧先把Flex端的代码如下:Flex代码:var excelURL:String = http:/localhost:8080/FlexApp/toExcel.jsp;ActionScript代码:import mx.controls.DataGrid;private function convertDGToHTMLTable(dg:DataGrid):String /Set default valuesvar str:String = ; /Set the htmltabel based upon knowlegde from the datagridstr+= ;/Set the tableheader data (retrieves information from the datagrid header for(var i:int = 0;idg.columns.length;i+) if(dg.columnsi.headerText != undefined) str+=+dg.columnsi.headerText+; else str+= +dg.columnsi.dataField+; str += ;/Loop through the records in the dataprovider and /insert the column information into the tablefor(var j:int =0;jdg.dataProvider.length;j+) str+=; for(var k:int=0; k dg.columns.length; k+) /Do we still have a valid item? if(dg.dataProvider.getItemAt(j) != undefined & dg.dataProvider.getItemAt(j) != null) if(dg.columnsk.labelFunction != undefined) str += +dg.columnsk.labelFunction(dg.dataProvider.getItemAt(j),dg.columnsk.dataField)+; else str += +dg.dataProvider.getItemAt(j)dg.columnsk.dataField+; str += ;str+=;return str;private function loadDGInExcel(dg:DataGrid,url:String):void /Pass the htmltable in a variable so that it can be delivered/to the backend scriptvar variables:URLVariables = new URLVariables(); variables.htmltable = convertDGToHTMLTable(dg);/Setup a new request and make sure that we are /sending the data through a postvar u:URLRequest = new URLRequest(url);u.data = variables; /Pass the variablesu.method = URLRequestMethod.POST; /Dont forget that we need to send as POST/Navigate to the script/We can use _self here, since the script will through a filedownload header/which results in offering a download to the user (and still remaining in you Flex app.) navigateToURL(u,_self);Jsp代码:这样出来后有一个问题,因为我在DataGrid中有过滤的,当DataGrid为全量数据的时候,导出的Excel没有问题,但是当过滤一部分数据的时候,导出的Excel就是乱码了的。后来也不知道什么原因,通过System.out.println(str)打印出request上来的str也没有错,我实在是搞不懂这个是什么问题了,查到了好多资料但都没有说道这个问题,只是知道了Excel的编码可能有问题,于是在Jsp中改成了response.setCharacte

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论