论坛首页 编程语言技术论坛

将flex页面数据导出到excel

浏览 12729 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-04  

本例实现将flex中的数据利用as3xls-1[1].0.swc导出到excel文件中

 

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:Script>
         <![CDATA[
            import mx.controls.CheckBox;
            import mx.controls.Alert;        
            import com.as3xls.xls.ExcelFile;
   import com.as3xls.xls.Sheet;
      import flash.filesystem.*;
       [Bindable]
         private var dp:Array = [
        {idx:1, names: "test1", sex: "b" },
        {idx:2, names: "test2", sex: "g" }
       
        ];
        public function doSelect(o:Object):void
        {
            Alert.show("行的数据分别是:"+o.idx+"/"+o.names+"/"+o.sex);
           
        }
   
  
   
   private var sheet:Sheet;
   private function onCreate():void {

    var excelFile:ExcelFile = new ExcelFile();
    sheet = new Sheet();
    sheet.resize(10, 10);
    sheet.setCell(0, 0, "Today's date:");
    sheet.setCell(0, 1, new Date());
    excelFile.sheets.addItem(sheet);
    var mbytes:ByteArray = excelFile.saveToByteArray();
    
   
                                var stream:FileStream = new FileStream();               
                                var docsDir:File = File.documentsDirectory.resolvePath("abc.xls");  // 定死文件名

                                try
                                {
                                    docsDir.browseForSave("Save As");
                                    docsDir.addEventListener(Event.SELECT, saveData);
                                }
                                catch (error:Error)
                                {
                                    trace("Failed:", error.message)
                                }
                               
                               
                                function saveData(event:Event):void
                                {
                                    var newFile:File = event.target as File; 

                                    if (!newFile.exists)
                                    {
                                        var stream:FileStream = new FileStream();
                                        stream.open(newFile, FileMode.WRITE);
                                        stream.writeBytes(mbytes); 
                                        // 写文件流
                                        stream.close();
                                    }
                                }


    
   }
       
    ]]>
     </mx:Script>
    
     <mx:Panel>
      
 
     <mx:Button   label="导出" click="onCreate()"/>
    <mx:DataGrid id="dg1"  dataProvider ="{dp}">
       
        <mx:columns>
        <mx:DataGridColumn  width="20" headerText="" >
             <mx:itemRenderer>
                        <mx:Component>
                        <mx:CheckBox  change=" {outerDocument.doSelect(data as Object)} "    />
                        </mx:Component>
                    </mx:itemRenderer>
                    </mx:DataGridColumn> 
            <mx:DataGridColumn headerText="names" dataField="names" width="200" />
               <mx:DataGridColumn headerText="sex" dataField="sex" width="300" />
       
        </mx:columns>
    </mx:DataGrid>
       </mx:Panel>
</mx:WindowedApplication>

   发表时间:2008-08-19  
请问楼主 导入flash.filesystem.*需要什么环境或者加载什么包,急切。。。
0 请登录后投票
   发表时间:2008-08-19  
flash.filesystem.*
需要flex AIR工程环境
在flex web application 工程环境下,没有flash.filesystem.*
0 请登录后投票
   发表时间:2008-11-21  
windowedApplication. 
如果是普通的flex怎么办
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics