zoukankan      html  css  js  c++  java
  • [Flex] as3xls读取excel,修改保存单表(二)

    这个方法仅用了as3xls读取excel的功能,修改保存独立出来了。

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                           xmlns:s="library://ns.adobe.com/flex/spark" 
                           xmlns:mx="library://ns.adobe.com/flex/mx"
                           creationComplete="initViews(event)" width="1024" height="1000">
        <fx:Script>
            <![CDATA[
                import com.as3xls.xls.ExcelFile;
                import com.as3xls.xls.Sheet;
                
                import mx.events.FlexEvent;
                
                private var _headerArr:Array = [];
                private var _dataArr:Array = [];
                protected function initViews(event:FlexEvent):void
                {
                    var file:File = new File("E:/AS/Examples/ReadAndSaveExcel/src/样表.xls");
                    //读取文件
                    var stream:FileStream = new FileStream();
                    stream.open(file,FileMode.READ);
                    var ba:ByteArray = new ByteArray();
                    stream.readBytes(ba);
                    stream.close();
                    
                    var xls:ExcelFile = new ExcelFile();
                    xls.loadFromByteArray(ba);
                    trace(xls.sheets.length);
                    var sheet:Sheet = xls.sheets[1];
                    var rowLen:int = sheet.rows;
                    var cloumnLen:int = sheet.cols;
                    //trace(sheet.getCell(0,0).value,sheet.getCell(0,1).value,sheet.getCell(2,0).value);
                    trace(rowLen,cloumnLen);
                    
                    for(var i:int = 0;i<cloumnLen;i++){
                        _headerArr.push(sheet.getCell(0,i).value);
                    }
                    for(var j:int = 0;j<_headerArr.length;j++){
                        var title:TextField = getText(120,20,_headerArr[j]);
                        title.x = j*120+100;
                        title.y =0;
                        container.addChild(title);
                    }
                    
                    for(j = 1;j<rowLen;j++){
                        if(sheet.getCell(j,i).value == ""){
                            //trace("为空");
                        }else{
                            
                        }
                        var obj:Object = {};
                        for(i = 0;i<_headerArr.length;i++){
                            if(i == 3){
                                obj[_headerArr[i]] = Math.floor(Math.random()*2000).toString();
                            }else{
                                obj[_headerArr[i]] = sheet.getCell(j,i).value;
                            }
                        }
                        _dataArr.push(obj);
                        var count:int = 0;
                        for(var n:int = 0;n<_headerArr.length;n++){
                            var t:TextField = getText(120,20,obj[_headerArr[n]]);
                            t.x = 120*count+100;
                            t.y = 20*j;
                            count++;
                            container.addChild(t);
                        }
                    }
                }
                protected function saveBtn_clickHandler(event:MouseEvent):void
                {
                    save(_dataArr,_headerArr,_headerArr);
                }
                private function save(datas:Array,headerArr:Array,objArr:Array):void
                {
                    var xlsStr:String = "<html><head></head><body><table>";
                    xlsStr += "<tr>";
                    for each(var fieldHead:String in headerArr)
                    {
                        xlsStr += "<td>" + fieldHead + "</td>";
                    }
                    xlsStr += "</tr>";
                    for each(var item:Object in datas)
                    {
                        xlsStr += "<tr>";
                        for each(var field:String in objArr)
                        {
                            xlsStr += "<td>" + item[field] + "</td>";
                        }
                        xlsStr += "</tr>";
                    }
                    xlsStr += "</table></body></html>";
                    
                    var bytes:ByteArray = new ByteArray();
                    //bytes.writeUTFBytes(xlsStr);
                    bytes.writeMultiByte(xlsStr, "GB2312");
                    new FileReference().save(bytes, "data.xlsx");
                    
                    //运用File可以达到同样效果,而且不用弹出保存对话框
                    /*var file:File = File.desktopDirectory.resolvePath("data.xls");
                    var fs:FileStream = new FileStream();
                    fs.open(file, flash.filesystem.FileMode.WRITE);
                    fs.writeMultiByte(xlsStr,"GB2312");
                    fs.close();*/
                }
                
                private function getText(w:Number,h:Number,text:String):TextField
                {
                    var t:TextField = new TextField();
                    t.width = w;
                    t.height = h;
                    //t.autoSize = "center";
                    t.selectable = false;
                    t.border = true;
                    t.text = text;
                    return t;
                }
            ]]>
        </fx:Script>
        <fx:Declarations>
            <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        </fx:Declarations>
        <s:Scroller  width="900" height="800">
            <s:Group width="100%" height="100%">
                <mx:UIComponent id="container" width="800" height="100%"/>
            </s:Group>
        </s:Scroller>
        <s:Button id="saveBtn" label="保存" left="910" top="10" click="saveBtn_clickHandler(event)">
            
        </s:Button>
    </s:WindowedApplication>
  • 相关阅读:
    CSS定位DIV绝对底部
    Android平台语音交友软件源码开发,语音通话的实现
    小视频app源码审核机制优化,直播间的优化重点
    为什么一对一直播系统源码功耗高,这些原因你了解吗?
    短视频app源码多元化发展,加深与电商的渊源
    【日本語新聞選読】第7回:4月14日
    JSP属性的四种保存范围(page request session application)
    CDC之fast->slow (1)
    openMSP430之openmsp430-loader
    最简单的DWR例子
  • 原文地址:https://www.cnblogs.com/frost-yen/p/4952360.html
Copyright © 2011-2022 走看看