flex 读取excel文件的方法

 

 
  
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
>
import com.as3xls.xls.ExcelFile;
import com.as3xls.xls.Sheet;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.controls.Image;
import mx.core.IVisualElement;
private var file:FileReference;
private var xls:ExcelFile;
var dp:ArrayCollection = new ArrayCollection;
/**
* 点击文件选择弹出框
* */
private function upload():void{
file = new FileReference();
file.addEventListener(Event.SELECT, file_select);
file.addEventListener(Event.COMPLETE,
file_complete);
file.addEventListener(ProgressEvent.PROGRESS,
file_progress);
file.browse();
}
/**
* 选择了excel之后
* */
private function file_select (e:Event):void {
if (file.size > 0) {
file.load();
}
}
/**
* excel加载完毕
* */
private function file_complete (e:Event):void {
xls = new ExcelFile();
try{
xls.loadFromByteArray(e.target.data);
}catch(eofe:EOFError){
trace(eofe);
}
var sheet:Sheet = xls.sheets[0];
//sheet.rows
dp = sheet.values;
dp.removeItemAt(sheet.rows);
dg.dataProvider = dp;
}
/**
* 进度条控制
* */
private function file_progress (e:ProgressEvent):void {
progressBar.label = Math.round(100 *
e.bytesLoaded / e.bytesTotal) + "%";
progressBar.setProgress(Math.round(100 *
e.bytesLoaded / e.bytesTotal), 100);
}
/**
* 获取表格数据
* */
protected function gainDgData(event:MouseEvent):void
{
var s:String = "";
var shuju:ArrayCollection = dg.dataProvider as ArrayCollection;
//行数:shuju.source.length-1 范围:0 到 shuju.source.length-2
for(var i:uint = 0 ; i < shuju.source.length-1 ; i++){ //一行一行(一条记录一条记录)的遍历
//每一行的列数为shuju[i]
//s += shuju[i][0].value;
}
//var t1 = shuju.source.length;
}
]]>
paddingLeft="20" paddingBottom="20" paddingRight="20"
paddingTop="20">

你可能感兴趣的:(flex)