Flex导出DataGrid数据

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qqqqqq12345678901/article/details/84495139
Flex前端+Java后台,要将Flex界面中AdvancedDataGrid的数据导出成Excel文件。有2种方法:

1.直接在前台导出。
使用开源项目as3xls,下载个解决了中文乱码的as3xlsUTF8.swc包,加到Flex工程的引用库中。
AS代码,点击“导出Excel”按钮后的函数:
Java代码 复制代码 收藏代码
var sheet:Sheet = new Sheet();
private function export2ExcelHandel(e:MouseEvent):void
{
//在前台生成Excel
var excelFile:ExcelFile=new ExcelFile();
var arr:ArrayCollection=myTable.dataProvider as ArrayCollection;
for(var i:int=0; i<arr.length; i++)
{
var obj:Object=arr[i];
var j:int=0;
for(var key:String in obj)
{
excelFile.sheets.addItem(generateSheet(0,j,key));
excelFile.sheets.addItem(generateSheet(i,j,obj[key]));
j++;
}
}
var mbytes:ByteArray=excelFile.saveToByteArray();
var file:FileReference=new FileReference();
file.save(mbytes,"测试文件.xls");
}

function generateSheet(i:int,j:int,o:Object):Sheet{
if(!sheet){
sheet = new Sheet();
sheet.resize(10,10);
}
sheet.setCell(i, j, String(o));
return sheet;
}
Java代码 收藏代码
var sheet:Sheet = new Sheet();
private function export2ExcelHandel(e:MouseEvent):void
{
//在前台生成Excel
var excelFile:ExcelFile=new ExcelFile();
var arr:ArrayCollection=myTable.dataProvider as ArrayCollection;
for(var i:int=0; i<arr.length; i++)
{
var obj:Object=arr[i];
var j:int=0;
for(var key:String in obj)
{
excelFile.sheets.addItem(generateSheet(0,j,key));
excelFile.sheets.addItem(generateSheet(i,j,obj[key]));
j++;
}
}
var mbytes:ByteArray=excelFile.saveToByteArray();
var file:FileReference=new FileReference();
file.save(mbytes,"测试文件.xls");
}

function generateSheet(i:int,j:int,o:Object):Sheet{
if(!sheet){
sheet = new Sheet();
sheet.resize(10,10);
}
sheet.setCell(i, j, String(o));
return sheet;
}

FileReference的save方法在Flash10才支持,
在Flex工程上右键,属性,Flex Conpiler中把Require Flash Player version:填为10.0.0;在Flex Build Path中加入playerglobal.swc
,路径为…\sdks\3.2.0\frameworks\libs\player\10\playerglobal.swc

2.通过调JSP或Servlet导出。
AS代码:
Java代码 复制代码 收藏代码
public static function ToExcel(dg:AdvancedDataGrid,name:String)
{
//调用JSP
var url:String="http://localhost:8080/XXX/Export/ExcelExport.jsp";
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");
}

private static function convertDGToHTMLTable(dg:AdvancedDataGrid):String {
//Set default values
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;

//Retrieve the headercolor
if(dg.getStyle("headerColor") != undefined) {
hcolor = [dg.getStyle("headerColor")];
} else {
hcolor = dg.getStyle("headerColors");
}

str+= '<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head><table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'">';
for(var i:int = 0;i<dg.columns.length;i++) {
colors = dg.getStyle("themeColor");
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><tbody>";
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].labelFunction != undefined) {
str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+"</td>";
}
else {
str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]+"</td>";
}
}
}
str += "</tr>";
}
str+="</tbody></table>";
return str;
}
Java代码 收藏代码
public static function ToExcel(dg:AdvancedDataGrid,name:String)
{
//调用JSP
var url:String="http://localhost:8080/XXX/Export/ExcelExport.jsp";
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");
}

private static function convertDGToHTMLTable(dg:AdvancedDataGrid):String {
//Set default values
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;

//Retrieve the headercolor
if(dg.getStyle("headerColor") != undefined) {
hcolor = [dg.getStyle("headerColor")];
} else {
hcolor = dg.getStyle("headerColors");
}

str+= '<head><meta http-equiv="Content-Type" content="text/htm; charset=utf-8"></head><table width="'+dg.width+'" border="1"><thead><tr width="'+dg.width+'" style="background-color:#' +Number((hcolor[0])).toString(16)+'">';
for(var i:int = 0;i<dg.columns.length;i++) {
colors = dg.getStyle("themeColor");
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><tbody>";
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].labelFunction != undefined) {
str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.columns[k].labelFunction(dg.dataProvider.getItemAt(j),dg.columns[k].dataField)+"</td>";
}
else {
str += "<td width=\""+Math.ceil(dg.columns[k].width)+"\" "+style+">"+dg.dataProvider.getItemAt(j)[dg.columns[k].dataField]+"</td>";
}
}
}
str += "</tr>";
}
str+="</tbody></table>";
return str;
}


ExcelExport.jsp放在后台XXX工程的WebRoot下的Export文件夹中。
ExcelExport.jsp:
Java代码 复制代码 收藏代码
<%@ page language="java"%>
<%@ page contentType="application/msexcel;charset=UTF-8"
pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
//String instr=request.getParameter("filename");
response.setHeader("Content-disposition","attachment; filename=excel.xls");
response.setCharacterEncoding("UTF-8");
String str = request.getParameter("htmltable");
out.print(str);
%>
展开阅读全文

关于Flex页面Datagrid导出Excel问题

12-11

[code=HTML]rnrnrn rn rn rn import com.as3xls.xls.Cell;rn import com.as3xls.xls.ExcelFile;rn import com.as3xls.xls.Sheet;rn rn import mx.collections.ArrayCollection;rn import mx.controls.Alert;rn import mx.controls.dataGridClasses.DataGridColumn;rn import mx.formatters.DateFormatter; rn rn private var fileReference:FileReference; rn private var xls:Class; rn private var sheet:Sheet; rn rn [Bindable] rn private var fields:Array = new Array(); rn rn [Bindable] rn private var ItemDGDataProvider:ArrayCollection = new ArrayCollection([ rn name:"Item1",value:"21",qty:"3",cost:"12.21", apples: "GDGZ00000120111129100152#3208939133a582990133a5d861330006#GDGZ000001#11111111111#01#0189F1405A905059393232353352AABBCC#019FF1405A80000009147225C30606A053412006122804000000020407030300000000D8AABBCC#0185F1405801A0B12181AABBCC#019FF1125A80000007565300010D0011324C2006120808001059FFFFFFFFFFFFFFFFFF71AABBCC#018BF11258032D32A229F4822D1002C8AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#0183F1297F1A78AE9FF1295A80000006778164060705B048422006121318000BFD041E0C000000000000DAAABBCC#0182F1295800F4AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1015A80000009134280100808E0475A2006121508001620040A0003280A000000B8AABBCC#0182F1015800CCAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1635A80000009141681020209004B412006121557000C23492D0A03030000000073AABBCC#0182F16358002EAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1415A80000009149671030506B04B462006121941000BFA040A00030204000000CCAABBCC#0182F14158000CAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1725A80000009128183111205C047582006122809000C0407000003030000000008AABBCC#0182F17258003DAABBCC#019FF1565A80000009197737340605A048432002121302000C0305010103030000000867AABBCC#0183F1567F1878D9AABBCC#0182F156580021AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#019FF1785A80000009199260030105D0475520061219560017670B00C403031E00000083AABBCC#0182F178580043AABBCC#019FF1605A800000091414890E0808D0525020061220630016117350800303000000003FAABBCC#0182F16058002BAABBCC#019FF1005A80000009134484050708C04759200403010900128D050900030300000000A6AABBCC#0182F1005800CBAABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABBCC#01AABB", rn ]); rn rn private function fileReference_Cancel(event:Event):void rn rn fileReference = null; rn rn rn private function exportToExcel():void rn rn sheet = new Sheet(); rn var dataProviderCollection:ArrayCollection = rebateByItemDG.dataProvider as ArrayCollection; rn var rowCount:int = dataProviderCollection.length; rn sheet.resize(rowCount + 1,rebateByItemDG.columnCount); rn var columns:Array = rebateByItemDG.columns; rn var i:int = 0; rn //添加标题rn for each (var field:DataGridColumn in columns) rn fields.push(field.dataField.toString()); rn //sheet.setCell(0,i,field.dataField.toString()); rn i++; rn rn rn for(var r:int=0; r < rowCount; r++) rn rn var record:Object = dataProviderCollection.getItemAt(r); rn /*insert record starting from row no 2 else rn headers will be overwritten*/ rn insertRecordInSheet(r+1,sheet,record); rn rn var xls:ExcelFile = new ExcelFile(); rn xls.sheets.addItem(sheet); rn rn var bytes: ByteArray = xls.saveToByteArray(); rn var fr:FileReference = new FileReference(); rn fr.save(bytes,"c_templog.xls"); rn rn private function insertRecordInSheet(row:int,sheet:Sheet,record:Object):void rn rn var colCount:int = rebateByItemDG.columnCount; rn for(var c:int; c <= 10; c++) rn rn var i:int = 0; rn rn var count:int=0;rn var thesize:int=200;rn for each(var field:String in fields) rn for each (var value:String in record) rn if (record[field].toString() == value) rn sheet.setCell(row,i,value);//给sheet添加数据rn rn rn i++; rn rn rn rn rn rn rn rn rnrn rn rn rn rn rn rn rn rn rn rn rn rn rn[/code]rnrn上面这段是用as3xls做的,其中这句sheet.setCell(row,i,value);是给sheet添加数据的,但是这句,添加的数据长度最多是207个字符,我上面有一个列的值有1999个字符,添加进来却只有207个,不管多长,都是如此;求高手们能不能解决问题啊!rn还有,如果value的长度>2000,生成的excel就打不开,说是已损坏!rn 求各位帮帮忙,我明天就得交差,这个方法很方便,觉得很好用,但是有这个问题,我要的功能需要4000左右的字符长度;请问各位有没有生成其他文件格式的方法,或者能解决这个问题,rn 我刚刚接触Flex几天而已,各位最好贴上源码!!!谢谢了!!! 论坛

没有更多推荐了,返回首页