我是Flex編碼的新手,並且正在嘗試導入Excel文件,以便以後可以使用它。我從下面的兩篇文章中拼湊出足夠多的文章,以便我可以成功加載一個Excel文件並在DataGrid中顯示內容。Flex DataGrid在加載新的Excel文件時沒有更新
但是,如果我嘗試上傳第二個Excel文件,則DataGrid的內容不會更改。 (我結束了從頂部,當代碼重新寫頭到DataGrid截斷一行。)
- http://blog.flexexamples.com/2007/09/21/uploading-files-in-flex-using-the-filereference-class
- 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>
Appologies,只注意到一些代碼得到了在底部截斷,儘管我懷疑沒有什麼意義的問題。另外還有一些額外的變量試圖解決這個問題... –
我修復了代碼格式化;有時StackOverflow在代碼格式上變得很奇怪。我不確定爲什麼調用grid.initialize();你永遠不需要手動調用它。如果DataGrid沒有改變它,那麼最有可能意味着dataProvider沒有被改變,或者被替換爲相同的元素。頂部項目被刪除的原因是xmlSheet.removeItemAt(0)。聽起來就像你沒有選擇一個新文件,或者fileRef沒有獲取到新文件的引用。 – JeffryHouser
@ www.Flextras.com,感謝您修改代碼格式。自己無法看到如何做到這一點。 –