读书人

关于Flex页面Datagrid导出Excel有关问

发布时间: 2012-05-09 12:13:59 作者: rapoo

关于Flex页面Datagrid导出Excel问题

HTML code
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                xmlns:s="library://ns.adobe.com/flex/spark"                xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="init()">        <fx:Script>        <![CDATA[            import com.as3xls.xls.Cell;            import com.as3xls.xls.ExcelFile;            import com.as3xls.xls.Sheet;                        import mx.collections.ArrayCollection;            import mx.controls.Alert;            import mx.controls.dataGridClasses.DataGridColumn;            import mx.formatters.DateFormatter;                         private var fileReference:FileReference;             private var xls:Class;             private var sheet:Sheet;                         [Bindable]             private var fields:Array = new Array();                         [Bindable]             private var ItemDGDataProvider:ArrayCollection = new ArrayCollection([                 {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"},             ]);                         private function fileReference_Cancel(event:Event):void             {                 fileReference = null;             }                         private function exportToExcel():void             {                 sheet = new Sheet();                 var dataProviderCollection:ArrayCollection = rebateByItemDG.dataProvider as ArrayCollection;                 var rowCount:int = dataProviderCollection.length;                 sheet.resize(rowCount + 1,rebateByItemDG.columnCount);                 var columns:Array = rebateByItemDG.columns;                 var i:int = 0;                 //添加标题                for each (var field:DataGridColumn in columns){                     fields.push(field.dataField.toString());                     //sheet.setCell(0,i,field.dataField.toString());                     i++;                 }                                 for(var r:int=0; r < rowCount; r++)                 {                     var record:Object = dataProviderCollection.getItemAt(r);                     /*insert record starting from row no 2 else                     headers will be overwritten*/                     insertRecordInSheet(r+1,sheet,record);                 }                 var xls:ExcelFile = new ExcelFile();                 xls.sheets.addItem(sheet);                                 var bytes: ByteArray = xls.saveToByteArray();                 var fr:FileReference = new FileReference();                 fr.save(bytes,"c_templog.xls");              }             private function insertRecordInSheet(row:int,sheet:Sheet,record:Object):void             {                 var colCount:int = rebateByItemDG.columnCount;                 for(var c:int; c <= 10; c++)                 {                     var i:int = 0;                                         var count:int=0;                    var thesize:int=200;                    for each(var field:String in fields){                         for each (var value:String in record){                             if (record[field].toString() == value) {                                sheet.setCell(row,i,value);//给sheet添加数据                            }                        }                         i++;                     }                 }             }                                  ]]>    </fx:Script>        <mx:VBox width="100%" height="100%">        <mx:Form>            <mx:FormItem label="Export Datagrid items to Excel?" fontWeight="bold">                <mx:Form>                    <mx:HBox width="100%" verticalAlign="middle">                        <mx:DataGrid id="rebateByItemDG" includeInLayout="true" visible="true" dataProvider="{ItemDGDataProvider}" width="100%" editable="true"/>                        <mx:Button label="Export To Excel" click="exportToExcel();"/>                    </mx:HBox>                </mx:Form>            </mx:FormItem>        </mx:Form>    </mx:VBox></s:Application>  



上面这段是用as3xls做的,其中这句sheet.setCell(row,i,value);是给sheet添加数据的,但是这句,添加的数据长度最多是207个字符,我上面有一个列的值有1999个字符,添加进来却只有207个,不管多长,都是如此;求高手们能不能解决问题啊!
还有,如果value的长度>2000,生成的excel就打不开,说是已损坏!
求各位帮帮忙,我明天就得交差,这个方法很方便,觉得很好用,但是有这个问题,我要的功能需要4000左右的字符长度;请问各位有没有生成其他文件格式的方法,或者能解决这个问题,
我刚刚接触Flex几天而已,各位最好贴上源码!!!谢谢了!!!

[解决办法]
啊 100分的帖都木人回复啊 flex,不懂啊 给你顶下吧
[解决办法]
楼主,你看下
http://www.cnblogs.com/80hou/articles/2169589.html
[解决办法]
对Flex实在是不熟悉,建议你考虑下用其它Java的Excel组件,比如JXL或者Aspose.Excel,都挺好用的。

另外,还可以考虑Google下是否Flex关于Excel的操作是否有参数可以调整。

最后,这个论坛估计也不会有太多人熟悉Flex,去专业的Flex社区吧。

[解决办法]
我导excel是通过ireport生成 pdf excel word都可以生的,很好用 表格大小无限制

读书人网 >Java Web开发

热点推荐