as 如何实现 导出 csv
http://code.google.com/p/csvlib/wiki/QuickStart
- package
- {
- import flash.events.Event
- import flash.events.MouseEvent;
- import flash.events.TimerEvent;
- import flash.net.URLRequest;
- import flash.display.Sprite
- import com.shortybmc.data.parser.CSV;
- import flash.utils.Timer
- import com.koolou.net.KUrlLoader;
- import flash.net.FileReference;
- import flash.utils.ByteArray;
- import flash.events.SecurityErrorEvent
- /**
- * …
- * @author mowa
- */
- public class SaveCSV extends Sprite
- {
- private var csv:CSV=new CSV();
- private var CsvData:String;
- private var dataId:int= 0;
- private var timer:Timer;
- private var url:String = “网址”;
- private var file:FileReference;
- private var strings:String = “mobile” + “,” + “Be Member” + “\n”;
- private var ss:String;
- public function SaveCSV() {
- btn.addEventListener(MouseEvent.CLICK, onClick);
- }
- private function onClick(e:MouseEvent):void {
- csv.embededHeader = false;
- csv.fieldSeperator = ‘,’;
- csv.recordsetDelimiter = ‘\n’
- loadInfo()
- }
- private function loadInfo() {
- var kl:KUrlLoader = new KUrlLoader();
- var obj={}
- dataId += 1;
- obj.id = dataId;
- obj.pwd = “harbinnbagyp20120622”;
- kl.load(url, obj, onFinished);
- btn.removeEventListener(MouseEvent.CLICK, onClick);
- }
- private function onFinished(kurl :Object):void {
- if (kurl.success) {
- ss += “第” + dataId + “行数据” + “\n”;
- data_txt.text = ss
- if (kurl.data.length < 100) {
- var arr = kurl.data.split(“,”)
- if (String(arr[1]) == “1”) {
- arr[1]=”Yes”
- }else {
- arr[1]=”No”
- }
- csv.addRecordSet(arr);
- } else {
- }
- loadInfo()
- }else {
- trace(“错了么 ?”)
- writeCSV()
- }
- }
- private function writeCSV()
- {
- var file:FileReference = new FileReference();
- var bytes:ByteArray = new ByteArray();
- var str:String=strings+csv.data.join(‘\n’)
- bytes.writeUTFBytes(str);
- file.addEventListener(Event.OPEN, fileOpen);
- file.addEventListener(Event.COMPLETE, fileComplete);
- file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, sError);
- file.save(bytes, ‘data.csv’);
- }
- private function fileOpen(e:Event):void {
- }
- private function fileComplete(e:Event):void {
- trace(“保存文件”)
- }
- private function sError(event:SecurityErrorEvent):void {
- trace(“securityErrorHandler: ” + event+”先关闭文件,再保存”);
- }
- }
- }
复制代码
把数据存.csv文件
另附第三方库
http://code.google.com/p/csvlib/
本文出自 传播、沟通、分享,转载时请注明出处及相应链接。
本文永久链接: https://www.nickdd.cn/?p=1957