[AIR] AIR将数据保存并导出为Excel
1 package 2 { 3 import flash.display.Sprite; 4 import flash.events.MouseEvent; 5 import flash.filesystem.File; 6 import flash.filesystem.FileMode; 7 import flash.filesystem.FileStream; 8 import flash.net.FileReference; 9 import flash.net.ObjectEncoding; 10 import flash.text.TextField; 11 import flash.utils.ByteArray; 12 13 /** 14 * @author Frost.Yen 15 * @E-mail 871979853@qq.com 16 * @create 2015-8-1 下午8:54:59 17 * 18 */ 19 [SWF(width="1024",height="768")] 20 public class ExportExcel extends Sprite 21 { 22 private var _fields:Array = ["姓名", "地址","联系电话","QQ","电子邮箱"]; 23 private var _fields_en:Array = ["name", "address", "tel", "qq", "email"]; 24 private var _datas:Array = []; 25 private var _btn:TextField; 26 public function ExportExcel() 27 { 28 initViews(); 29 } 30 31 private function initViews():void 32 { 33 for(var j:int = 0;j<_fields.length;j++){ 34 var title:TextField = getText(100,20,_fields[j]); 35 title.x = j*100+100; 36 title.y = 50; 37 this.addChild(title); 38 } 39 for(var i:int = 0;i<10;i++){ 40 var obj:Object = {name:"Name"+i,address:"address"+i,tel:"tel"+i,qq:"qq"+i,email:"email"+i}; 41 _datas.push(obj); 42 var count:int = 0; 43 for(var key:String in obj){ 44 var t:TextField = getText(100,20,obj[key]); 45 t.x = 100*count+100; 46 t.y = 20*i+70; 47 count++; 48 this.addChild(t); 49 } 50 } 51 _btn = new TextField(); 52 _btn.selectable = false; 53 _btn.autoSize = "left"; 54 _btn.border = true; 55 _btn.text = "导出Excel"; 56 _btn.x = 620; 57 _btn.y = 50; 58 _btn.addEventListener(MouseEvent.CLICK,onExport); 59 this.addChild(_btn); 60 } 61 private function onExport(e:MouseEvent):void 62 { 63 save(); 64 } 65 private function save():void 66 { 67 var xlsStr:String = "<html><head></head><body><table>"; 68 xlsStr += "<tr>"; 69 for each(var fieldHead:String in _fields) 70 { 71 xlsStr += "<td>" + fieldHead + "</td>"; 72 } 73 xlsStr += "</tr>"; 74 for each(var item:Object in _datas) 75 { 76 xlsStr += "<tr>"; 77 for each(var field:String in _fields_en) 78 { 79 xlsStr += "<td>" + item[field] + "</td>"; 80 } 81 xlsStr += "</tr>"; 82 } 83 xlsStr += "</table></body></html>"; 84 85 var bytes:ByteArray = new ByteArray(); 86 //bytes.writeUTFBytes(xlsStr); 87 bytes.writeMultiByte(xlsStr, "GB2312"); 88 new FileReference().save(bytes, "data.xls"); 89 90 //运用File可以达到同样效果,而且不用弹出保存对话框 91 /*var file:File = File.desktopDirectory.resolvePath("data.xls"); 92 var fs:FileStream = new FileStream(); 93 fs.open(file, flash.filesystem.FileMode.WRITE); 94 fs.writeMultiByte(xlsStr,"GB2312"); 95 fs.close();*/ 96 } 97 private function getText(w:Number,h:Number,text:String):TextField 98 { 99 var t:TextField = new TextField(); 100 t.width = w; 101 t.height = h; 102 //t.autoSize = "center"; 103 t.selectable = false; 104 t.border = true; 105 t.text = text; 106 return t; 107 } 108 } 109 }
------------------------------------------------------------------
Always put yourself in the other's shoes.If you feel that it hurts you,it probably hurts others,too.------------------------------------------------------------------