1

我是 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

完整代码如下。关于我要去哪里错的任何想法?

提前干杯和感谢!

科里

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>
4

1 回答 1

2

代码中的唯一错误是每次加载新内容时 xlFile 对象都会递增。所以 sheet[0] 总是一样的!如果您的文件有 3 张纸,则新文件的第一张纸在 sheet[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>
于 2012-12-16T16:33:11.827 回答