Flex实现将Wrod文档和Excel文档转xml,以及读取csv

今天产品那边反馈,感觉数据录入,尤其是题目录入那块太繁琐了,希望能够用倒入excel文件的方式,这样,我的任务又来了。之前没有接触过这块。到网上百度了下。网上有直接针对excel的处理加载方法,但是这个方法是用到一个封装的类包,并且有一些的版本要求。这条路不可行,于是就决定通过加载csv文件的方式来实现。   这里贴上源代码: public function inputQuestion():void { inputQF = new FileReference(); inputQF.addEventListener(Event.SELECT, onSelectQestionFile); inputQF.addEventListener(ProgressEvent.PROGRESS, progressHandle); inputQF.addEventListener(Event.COMPLETE, completeInputQuesetion); inputQF.browse(getQuestionTypeFilter()); } private function getQuestionTypeFilter() : Array { // var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png"); var docFilter:FileFilter = new FileFilter("Documents", "*.csv"); return [docFilter]; } protected function onSelectQestionFile(event:Event):void { var fileReference:FileReference=event.target as FileReference; if(fileReference.type==".csv") { try { fileReference.load(); }catch(e:Error) { trace(fileReference.name,fileReference.type) } }else trace("请选择正确的格式的文件") } protected function completeInputQuesetion(event:Event):void { // TODO Auto-generated method stub trace(event.target.data); var temp:Array; var bytes:ByteArray = event.target.data as ByteArray; var data:String = bytes.readUTFBytes(bytes.length); // var a1:Array = content.split("/n"); }   具体实现上也么有遇到太大的问题,只是加载然后是二进制读取的方法。其实csv也只是个简单的文本文件只是他们的属性值之间是用","来分隔开的。但是从中遇到一个很纠结的问题,加载文件格式的问题,中文乱码,这个就需要先对文件进行转码,然后再读取处理! 后面贴了一些网上的相关问题处理方法,喜欢的可以关注下。 一、flex与word协作:    对于word,可将word文档模板文件存成xml格式,而此xml文档中的图片是以“base-64 encoded”,而flex3可以将生成的图片快照下来再转换成此格式,flex能够操作xml文件,从而可以修改word xml模板文件中的内容,从而与word协作。 将ImageSnapshot对象利用flex3的encodeImageAsBase64()方法转换为base-64 encoded格式的例子:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2007/12/07/converting-an-imagesnapshot-object-into-a-base-64-encoded-string-in-flex-3/ --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"        layout="vertical"        verticalAlign="middle"        backgroundColor="white">   <mx:Script>        <![CDATA[            import flash.events.FocusEvent;            import flash.system.System;            import mx.graphics.ImageSnapshot;           private function button_click(evt:MouseEvent):void {                var ohSnap:ImageSnapshot = ImageSnapshot.captureImage(img);                textArea.text = ImageSnapshot.encodeImageAsBase64(ohSnap);            }           private function textArea_focusIn(evt:FocusEvent):void {                textArea.setSelection(0, textArea.text.length);            }        ]]>    </mx:Script>    <mx:ApplicationControlBar dock="true">        <mx:Button label="Capture and encode"                click="button_click(event);" />    </mx:ApplicationControlBar>    <mx:Form>        <mx:FormItem label="source:">            <mx:Image id="img"                    source="@Embed('images/flex_logo.jpg')" />        </mx:FormItem>        <mx:FormItem label="Base64:">            <mx:TextArea id="textArea"                    editable="false"                    showScrollTips="true"                    width="320"                    height="160"                    focusIn="textArea_focusIn(event);" />        </mx:FormItem>        <mx:FormItem>            <mx:Button label="Copy to clipboard"                    enabled="{textArea.text.length > 0}"                    click="System.setClipboard(textArea.text);" />        </mx:FormItem>    </mx:Form> </mx:Application>    
(本例来自:Flex Examples   mx.graphics.ImageSnapshot 是一种帮助器类,用于捕获实现了 flash.display.IBitmapDrawable 的任何 Flash 组件(包括 Flex UIComponent)的快照。    将base-64 encoded文本转换为图片可以使用开源的flexlib.controls.Base64Image类实现。这里是示例。 二、flex与excel的协作:    可以用as3xls实现,参考:http://code.google.com/p/as3xls/(仅支持到excel2003)    以下例子来自JAVAEYE实现将flex(air应用)中的数据利用as3xls-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>    
 

posted on 2012-07-25 23:11  AS3.0  阅读(832)  评论(0编辑  收藏  举报

导航