[Flex] as3xls读取excel,修改保存单表(二)

时间:2023-12-31 22:00:26

这个方法仅用了as3xls读取excel的功能,修改保存独立出来了。

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
creationComplete="initViews(event)" width="1024" height="1000">
<fx:Script>
<![CDATA[
import com.as3xls.xls.ExcelFile;
import com.as3xls.xls.Sheet; import mx.events.FlexEvent; private var _headerArr:Array = [];
private var _dataArr:Array = [];
protected function initViews(event:FlexEvent):void
{
var file:File = new File("E:/AS/Examples/ReadAndSaveExcel/src/样表.xls");
//读取文件
var stream:FileStream = new FileStream();
stream.open(file,FileMode.READ);
var ba:ByteArray = new ByteArray();
stream.readBytes(ba);
stream.close(); var xls:ExcelFile = new ExcelFile();
xls.loadFromByteArray(ba);
trace(xls.sheets.length);
var sheet:Sheet = xls.sheets[1];
var rowLen:int = sheet.rows;
var cloumnLen:int = sheet.cols;
//trace(sheet.getCell(0,0).value,sheet.getCell(0,1).value,sheet.getCell(2,0).value);
trace(rowLen,cloumnLen); for(var i:int = 0;i<cloumnLen;i++){
_headerArr.push(sheet.getCell(0,i).value);
}
for(var j:int = 0;j<_headerArr.length;j++){
var title:TextField = getText(120,20,_headerArr[j]);
title.x = j*120+100;
title.y =0;
container.addChild(title);
} for(j = 1;j<rowLen;j++){
if(sheet.getCell(j,i).value == ""){
//trace("为空");
}else{ }
var obj:Object = {};
for(i = 0;i<_headerArr.length;i++){
if(i == 3){
obj[_headerArr[i]] = Math.floor(Math.random()*2000).toString();
}else{
obj[_headerArr[i]] = sheet.getCell(j,i).value;
}
}
_dataArr.push(obj);
var count:int = 0;
for(var n:int = 0;n<_headerArr.length;n++){
var t:TextField = getText(120,20,obj[_headerArr[n]]);
t.x = 120*count+100;
t.y = 20*j;
count++;
container.addChild(t);
}
}
}
protected function saveBtn_clickHandler(event:MouseEvent):void
{
save(_dataArr,_headerArr,_headerArr);
}
private function save(datas:Array,headerArr:Array,objArr:Array):void
{
var xlsStr:String = "<html><head></head><body><table>";
xlsStr += "<tr>";
for each(var fieldHead:String in headerArr)
{
xlsStr += "<td>" + fieldHead + "</td>";
}
xlsStr += "</tr>";
for each(var item:Object in datas)
{
xlsStr += "<tr>";
for each(var field:String in objArr)
{
xlsStr += "<td>" + item[field] + "</td>";
}
xlsStr += "</tr>";
}
xlsStr += "</table></body></html>"; var bytes:ByteArray = new ByteArray();
//bytes.writeUTFBytes(xlsStr);
bytes.writeMultiByte(xlsStr, "GB2312");
new FileReference().save(bytes, "data.xlsx"); //运用File可以达到同样效果,而且不用弹出保存对话框
/*var file:File = File.desktopDirectory.resolvePath("data.xls");
var fs:FileStream = new FileStream();
fs.open(file, flash.filesystem.FileMode.WRITE);
fs.writeMultiByte(xlsStr,"GB2312");
fs.close();*/
} private function getText(w:Number,h:Number,text:String):TextField
{
var t:TextField = new TextField();
t.width = w;
t.height = h;
//t.autoSize = "center";
t.selectable = false;
t.border = true;
t.text = text;
return t;
}
]]>
</fx:Script>
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<s:Scroller width="900" height="800">
<s:Group width="100%" height="100%">
<mx:UIComponent id="container" width="800" height="100%"/>
</s:Group>
</s:Scroller>
<s:Button id="saveBtn" label="保存" left="910" top="10" click="saveBtn_clickHandler(event)"> </s:Button>
</s:WindowedApplication>