加载新Excel文件时Flex DataGrid不更新

时间:2012-12-15 23:51:13

标签: flex datagrid flex4 filereference as3xls

我是Flex编码的新手,我正在尝试导入Excel文件,以便我以后可以使用它。我从下面的两篇文章中拼凑了足够的东西,以便我可以成功加载一个Excel文件并在DataGrid中显示内容。

但是,如果我尝试上传第二个Excel文件,则DataGrid的内容不会更改。 (当代码将标头重新写入DataGrid时,我最终会从顶部截断一行。)

  1. http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class
  2. http://code.google.com/p/as3xls/wiki/Tutorial
  3. 完整代码如下。关于我哪里出错的任何想法?

    提前干杯谢谢!

    科里

    PS:我如何处理标题有一个小问题,因为Excel表格中的任何公式在删除标题后仍然引用原始行。他们指着他们应该在哪一排。

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" xmlns="*" creationComplete="init()" height="727" width="777">
    
    <mx:Script>
    
        <![CDATA[
            import com.as3xls.xls.ExcelFile;
            import com.as3xls.xls.Sheet;
    
            import mx.collections.ArrayCollection;
    
            //Based on example from: http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class/
    
            private var fileRef:FileReference;
            private var ba:ByteArray;
            private var xlFile:ExcelFile;
            private var hdrs:Array;
            private var runOnce:Boolean;
    
    
            [Bindable]
            private var xlsheet:ArrayCollection;
    
            private const FILE_URL:String = "http://localhost:8500/fileref/uploader.cfm";
            private const XLS_FILTER:FileFilter = new FileFilter("EXCEL FILES (*.xls, *.xlsx)", "*.xls; *.xlsx");
            private const TXT_FILTER:FileFilter = new FileFilter("TEXT FILES (*.txt, *.csv, *.tsv)", "*.txt; *.csv; *.tsv");
            private const ALL_FILTER:FileFilter = new FileFilter("ALL FILES (*.*)", "*.*");
    
            private function init():void {
                fileRef = new FileReference();
                fileRef.addEventListener(Event.SELECT, fileRef_select);
                fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
                fileRef.addEventListener(Event.COMPLETE, fileRef_complete);
    
                ba = new ByteArray();
                xlFile = new ExcelFile();
                hdrs = new Array();
                xlsheet = new ArrayCollection();
            }
    
            private function browseAndUpload():void {
                fileRef.browse([XLS_FILTER, TXT_FILTER, ALL_FILTER]);
                message.text = "";
    
    
            }
    
            private function fileRef_select(evt:Event):void {
                try {
                    message.text = "size (bytes): "+ numberFormatter.format(fileRef.size);
                    message.text += " | " + fileRef.name
    
                    //Alert.show (fileRef.name);
                    fileRef.load();
    
                } catch(err:Error) {
                    message.text = "ERROR: zero-byte file";
                }
            }
    
            private function fileRef_progress(evt:ProgressEvent):void{
                progressBar.visible = true;
            }
    
            private function fileRef_complete(evt:Event):void{
    
                try {
                    message.text += " (complete)";
                    progressBar.visible = false;
                    grid.initialize()               
    
                    ba=fileRef["data"];
                    xlFile.loadFromByteArray(ba);
                    xlsheet = xlFile.sheets[0].values;
    
                    hdrs =  xlsheet[0];
                    xlsheet.removeItemAt(0);
    
                    grid.dataProvider = xlsheet;
                } catch (err:Error) {
                    message.text = "An error occurred";
                }
    
            }
    
            private function updateHeaders(): void {
                if(grid.columnCount>=1){
                    for (var i:int=0; i<=grid.columnCount-1; i++){
                        grid.columns[i].headerText=hdrs[i];
                    }
    
                }
            }
    
        ]]>
    
    </mx:Script>
    
    <mx:NumberFormatter id="numberFormatter"/>
    <mx:Button label="Upload File"
               click="browseAndUpload();" labelPlacement="left"/>
    <mx:Label id="message"/>
    <mx:ProgressBar id="progressBar"
                    indeterminate="true"
                    visible = "false"/>
    
    <mx:DataGrid id="grid"
        updateComplete="updateHeaders();"/>
    
    
    </mx:Application>
    

1 个答案:

答案 0 :(得分:2)

代码中唯一的错误是每次加载新内容时xlFile对象都会递增。所以表[0]总是一样的! 如果您的文件有3张,则新文件的第一张纸位于图纸[3]对象中。

要更正此问题,请尝试在每次加载新文件时启动xlFile对象。我已经完成了它,它完美无缺!这是我的代码(我已经擦了一些线来简化它)

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
            xmlns:mx="library://ns.adobe.com/flex/mx" 
            layout="vertical" 
            height="727" width="777" creationComplete="init()">

<fx:Script>
    <![CDATA[
        import com.as3xls.xls.ExcelFile;
        import com.as3xls.xls.Sheet;
        import mx.collections.ArrayCollection;

        private var fileRef:FileReference;
        private var ba:ByteArray;
        private var xlFile:ExcelFile;
        private var hdrs:Array;
        private var runOnce:Boolean;

        [Bindable]private var xlsheet:ArrayCollection;

        private const XLS_FILTER:FileFilter = new FileFilter("EXCEL FILES (*.xls)", "*.xls");

        private function init():void 
        {
            fileRef = new FileReference();
            fileRef.addEventListener(Event.SELECT, fileRef_select);
            fileRef.addEventListener(ProgressEvent.PROGRESS, fileRef_progress);
            fileRef.addEventListener(Event.COMPLETE, fileRef_complete);

            ba = new ByteArray();
            hdrs = new Array();
            xlsheet = new ArrayCollection();
        }

        private function browseAndUpload():void {
            fileRef.browse([XLS_FILTER]);
            message.text = "";
        }

        private function fileRef_select(evt:Event):void {
            try {
                message.text = "size (bytes): "+ fileRef.size;
                message.text += " | " + fileRef.name
                fileRef.load();
            } catch(err:Error) {
                message.text = "ERROR: zero-byte file";
            }
        }

        private function fileRef_progress(evt:ProgressEvent):void{
            progressBar.visible = true;
        }

        private function fileRef_complete(evt:Event):void{
            try {
                xlFile = new ExcelFile();

                message.text += " (complete)";
                progressBar.visible = false;

                ba=fileRef["data"];
                xlFile.loadFromByteArray(ba);
                xlsheet = xlFile.sheets[0].values;

                hdrs =  xlsheet[0];
                xlsheet.removeItemAt(0);

                grid.dataProvider = xlsheet;
            } catch (err:Error) {
                message.text = "An error occurred";
            }
        }

        private function updateHeaders(): void {
            if(grid.columnCount>=1){
                for (var i:int=0; i<=grid.columnCount-1; i++){
                    grid.columns[i].headerText=hdrs[i];
                }
            }
        }

    ]]>
</fx:Script>

<mx:Button label="Upload File" click="browseAndUpload();" labelPlacement="left"/>
<mx:Label id="message"/>
<mx:ProgressBar id="progressBar" indeterminate="true" visible = "false"/>
<mx:DataGrid id="grid" updateComplete="updateHeaders();"/>
</mx:Application>