http://blog.csdn.net/Baijinwen/archive/2009/01/06/3722408.aspx
as3xls开源项目的中文乱码问题 收藏
<script type="text/javascript"> document.body.oncopy = function() { if (window.clipboardData) { setTimeout(function() { var text = clipboardData.getData("text"); if (text && text.length>300) { text = text + "\r\n\n本文来自CSDN博客,转载请标明出处:" + location.href; clipboardData.setData("text", text); } }, 100); } } </script><script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
as3xls,read and write Excle in Flex,开源项目首页: http://code.google.com/p/as3xls/
不过它对中文支持的不是很好,不知道现在有没有解决这个问题。有段时间我用过一下,曾经采取一个取巧的方式去解决,但我想并不是很好,如果有谁有更好的解决方法,或者已经对这个东东改造好的,给我一份,嘿嘿。
当初我临时解决的方式就是修改com/as3xls/xls/ExcelFile.as的大概255行那里:
- //var len:uint = String(value).length;
- var len: uint = GetStringLength(String(value)); //Bryan update
- cell.data.writeByte(len);
- //cell.data.writeUTFBytes(value);
- cell.data.writeMultiByte(String(value), "cn-gb" ); //Bryan update
增加两个函数:
- //获取一个字符串的长度,如果是中文字符则 为2,其他的为1 bryan add
- private function GetStringLength(objString:String): int
- {
- var temp: int = 0;
- for (var i: int =0;i<objString.length;i++)
- {
- if (isChinese(objString.substring(i,i+1)))
- {
- temp += 2;
- }
- else {temp += 1;}
- }
- return temp;
- }
示例package cn.ishion.excel.util { import com.as3xls.biff.BIFFVersion; import com.as3xls.biff.BIFFWriter; import com.as3xls.biff.Record; import com.as3xls.xls.ExcelFile; import com.as3xls.xls.Sheet; import com.as3xls.xls.Type; import flash.utils.ByteArray; import mx.utils.StringUtil; //注释 public class ConstomExcelFile extends ExcelFile { public function ConstomExcelFile() { } private function GetStringLength(objString:String):int { var temp:int = 0; for(var i:int=0;i<objString.length;i++) { if(isChinese(objString.substring(i,i+1))) { temp += 2; } else{temp += 1;} } return temp; } //判断是否是中文 bryan add private function isChinese(char:String):Boolean{ if(char == null){ return false; } char = StringUtil.trim(char); var pattern:RegExp = /^[\u0391-\uFFE5]+$/; var result:Object = pattern.exec(char); if(result == null) { return false; } return true; } override public function saveToByteArray():ByteArray{ // var s:Sheet = _sheets[0] as Sheet; //这行源代码 改成如下 var s:Sheet =super.sheets[0] as Sheet; var br:BIFFWriter = new BIFFWriter(); // Write the BOF and header records var bof:Record = new Record(Type.BOF); bof.data.writeShort(BIFFVersion.BIFF2); bof.data.writeByte(0); bof.data.writeByte(0x10); br.writeTag(bof); // Date mode var dateMode:Record = new Record(Type.DATEMODE); dateMode.data.writeShort(1); br.writeTag(dateMode); // Store built in formats var formats:Array = ["General", "0", "0.00", ",0", ",0.00", "", "", "", "", "0%", "0.00%", "0.00E+00", "?/?", "??/??", "M/D/YY", "D-MMM-YY", "D-MMM", "MMM-YY"]; var numfmt:Record = new Record(Type.BUILTINFMTCOUNT); numfmt.data.writeShort(formats.length); br.writeTag(numfmt); for(var n:uint = 0; n < formats.length; n++) { var fmt:Record = new Record(Type.FORMAT); fmt.data.writeByte(formats[n].length); fmt.data.writeUTFBytes(formats[n]); br.writeTag(fmt); } var dimensions:Record = new Record(Type.DIMENSIONS); dimensions.data.writeShort(0); dimensions.data.writeShort(s.rows+1); dimensions.data.writeShort(0); dimensions.data.writeShort(s.cols+1); br.writeTag(dimensions); for(var r:uint = 0; r < s.rows; r++) { for(var c:uint = 0; c < s.cols; c++) { var value:* = s.getCell(r, c).value; var cell:Record = new Record(1); cell.data.writeShort(r); cell.data.writeShort(c); if(value is Date) { var dateNum:Number = (value.time / 86400000) + 24106.667; cell.type = Type.NUMBER; cell.data.writeByte(0); cell.data.writeByte(15); cell.data.writeByte(0); cell.data.writeDouble(dateNum); } else if(isNaN(Number(value)) == false && String(value) != "") { cell.type = Type.NUMBER; cell.data.writeByte(0); cell.data.writeByte(0); cell.data.writeByte(0); cell.data.writeDouble(value); } else if(String(value).length > 0) { cell.type = Type.LABEL; cell.data.writeByte(0); cell.data.writeByte(0); cell.data.writeByte(0); // var len:uint = String(value).length; //源文 改成如下 var len:uint=this.GetStringLength(value); cell.data.writeByte(len); // cell.data.writeUTFBytes(value);//这行源代码 改成如下 cell.data.writeMultiByte(value,"gbk"); } else { cell.type = Type.BLANK; cell.data.writeByte(0); cell.data.writeByte(0); cell.data.writeByte(0); } br.writeTag(cell); } } // Finally, the closing EOF record var eof:Record = new Record(Type.EOF); br.writeTag(eof); br.stream.position = 0; return br.stream; } } }
-
private function exportExcel():void{ var ex:ExcelFile=new ConstomExcelFile(); var arr:ArrayCollection=new ArrayCollection(); var sheet:Sheet=new Sheet(); sheet.header="dsafsdafsaf"; sheet.footer="foot"; sheet.name="hello"; sheet.resize(10,10); for(var i:int=0;i<10;i++){ sheet.setCell(i,i,"大家好"); trace(ex.sheets); } ex.sheets.addItem(sheet); var file:File=File.desktopDirectory.resolvePath("test333.xls"); var fs:FileStream=new FileStream(); fs.open(file,FileMode.WRITE); fs.writeBytes(ex.saveToByteArray()); fs.close(); }