ceshi.xls数据格式如下所示:
flex实现如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Script>
<![CDATA[
import com.as3xls.xls.ExcelFile;
import com.as3xls.xls.Sheet;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
private var xls : ExcelFile;
private var sheet : Sheet;
[Bindable]
public var arr:ArrayCollection = new ArrayCollection();
[Bindable]
private var stateText : String = "请选择要导入的Execl表.";
private var file : FileReference = new FileReference();
protected override function createChildren():void
{
super.createChildren();
file.addEventListener(Event.SELECT,file_select);
//是否上传完成
file.addEventListener(Event.COMPLETE,file_complete);
//上传的进度
file.addEventListener(ProgressEvent.PROGRESS,file_progerss);
}
//选择上传的Execl
private function file_select(e:Event):void
{
stateText = file.name;
}
//上传状态
private function file_complete(e:Event):void
{
var byteArray:ByteArray = new ByteArray();
byteArray = file.data;
byteArray.position = 0;
xls = new ExcelFile();
xls.loadFromByteArray(byteArray);
sheet = xls.sheets[0];
var i:int;
var len : int = sheet.values.length;
for(i = 2; i < len - 1; i++)
{
arr.addItem({xm:sheet.values[i][0],nl:sheet.values[i][1],xl:sheet.values[i][2],lxdh:sheet.values[i][3],lxdz:sheet.values[i][4]});
}
Alert.okLabel = "确定";
Alert.show("共导入:"+(sheet.values.length - 3)+"条数据!","恭喜",Alert.OK);
}
//进度
private function file_progerss(e:ProgressEvent):void
{
progressBar.label = "已上传 " + Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
progressBar.setProgress(Math.round(100 * e.bytesLoaded / e.bytesTotal),100);
}
//上传
private function upload():void
{
if(file.size > 0)
{
file.load();
}
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Panel x="23" y="34" width="334" height="177" title="读取Execl" dropShadowVisible="false">
<s:TextInput x="25" y="27" width="196" text="{stateText}"/>
<s:Button x="239" y="27" label="浏览..." click="file.browse();"/>
<mx:ProgressBar x="25" y="78" height="26" width="195" id="progressBar"/>
<s:Button x="239" y="72" label="上传" click="upload();"/>
</s:Panel>
<mx:DataGrid x="365" y="57" width="404" height="154" dataProvider="{arr}">
<mx:columns>
<mx:DataGridColumn headerText="姓名" dataField="xm"/>
<mx:DataGridColumn headerText="年龄" dataField="nl"/>
<mx:DataGridColumn headerText="学历" dataField="xl"/>
<mx:DataGridColumn headerText="联系电话" dataField="lxdh"/>
<mx:DataGridColumn headerText="联系地址" dataField="lxdz"/>
</mx:columns>
</mx:DataGrid>
<s:Label x="365" y="37" text="显示导入的Execl数据" width="160" height="22"/>
</s:Application>
实现效果如下: