我是 Flex 编码的新手,正在尝试导入一个 Excel 文件,以便以后可以使用它。 我已经从下面的两篇文章中拼凑了足够多的内容,以便我可以成功加载一个 Excel 文件并在 DataGrid 中显示内容。
但是,如果我尝试上传第二个 Excel 文件,数据网格的内容不会更改。 (当代码将标头重写到 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>
代码中唯一的错误是每次加载新内容时 xlFile 对象都会递增。所以床单[0]总是相同的!如果您的文件有 3 张纸,那么新文件的第一张纸在 sheets[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>