Monday, August 27, 2012

Export Special Characters to Excel

In this post, i share while we export our datagrid values to excel which includes special characters.

You may using as3xls library file, to include that but main drawback is you couldnt add more than 256 characters into the excel cell. Because, we coulnt use writeMultiBytes() over there. Its not working properly to me.

So, i convert all to CSV format. If u want to insert Some special characters like pound(£), dollor($) etc, have to use writeMultibytes().


Example:

var finalCSVString:String; // csvLine holds all values as String.
var csvBytes:ByteArray = new ByteArray();
csvBytes.writeMultiByte(finalCSVString, "utf-16");

var csvFile:FileReference = new FileReference();
csvFile.save(csvBytes,"Export.csv");


Converting & Understanding CSV format:

The Comma-Seperated Values format, its copy the strings in to cell by comma. So Each values should converted as string. The below example shows how to we insert string into correspond cell.

var headLine:String;
var empName:String = "Yamuna"; //Values may cahnge dynamicaaly
var sal:String = "£1000";
 headLine = "\"Employee Name\"";
headLine += ",\"" + empName +"\"\n";

headLine = "\"Salary\"";
headLine += ",\"" + sal +"\"\n";


headLine += "\"End Here"";


",\"" - It is used to add comma between string to insert strings into two consecutive cells.

Now headLine looks like
"Employee Name","Yamuna"
"Salary","£1000"
"Ends Here"


So i Excel Sheet
       A1                                  A2       
1   Employee Name           Yamuna
2   Salary                            £1000
3   Ends Here

3 comments:

  1. You can do that on exporting excel itself. just replace the excelFile class from as3xls by setting the bytearray as writemultibytes. i already done that in crmspike. just refer it...

    ReplyDelete
    Replies
    1. It works fine. But using as3xls you can write up-to 256 characters.. beyond that it will truncate characters automatically..

      Delete
  2. There is a version of the as3xl library that hangles special chararcters

    https://github.com/djw/as3xls

    ReplyDelete