2012-12-15 47 views
1

我是Flex編碼的新手,並且正在嘗試導入Excel文件,以便以後可以使用它。我從下面的兩篇文章中拼湊出足夠多的文章,以便我可以成功加載一個Excel文件並在DataGrid中顯示內容。Flex DataGrid在加載新的Excel文件時沒有更新

但是,如果我嘗試上傳第二個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> 
+0

Appologies,只注意到一些代碼得到了在底部截斷,儘管我懷疑沒有什麼意義的問題。另外還有一些額外的變量試圖解決這個問題... –

+0

我修復了代碼格式化;有時StackOverflow在代碼格式上變得很奇怪。我不確定爲什麼調用grid.initialize();你永遠不需要手動調用它。如果DataGrid沒有改變它,那麼最有可能意味着dataProvider沒有被改變,或者被替換爲相同的元素。頂部項目被刪除的原因是xmlSheet.removeItemAt(0)。聽起來就像你沒有選擇一個新文件,或者fileRef沒有獲取到新文件的引用。 – JeffryHouser

+0

@ www.Flextras.com,感謝您修改代碼格式。自己無法看到如何做到這一點。 –

回答

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> 
+0

真棒...那就做到了。謝謝!我覺得它必須是那樣簡單的東西。現在我只需要找到一個更好的方式來處理標題,這樣任何公式都不會搞砸。 –

+0

錯過了最後一條評論中的標籤。只是想傳遞我的感謝! –