Onyx Mueller

AdvancedDataGrid CSV Export Utility Class

20 August 2011

Recently, I needed a way to export data from Adobe Flex’s AdvancedDataGrid control to CSV-formatted files for use in Microsoft Excel. Since Flex did not provide a native method, I looked into some existing third-party libraries. Those did not work out, so I ended up building my own AdvancedDataGrid CSV export utility class.

I first experimented with the excellent AlivePDF library. I had used AlivePDF in the past to…well…create PDFs. One of the things I remembered from using it was that there was CSV export functionality. But after spending some time experimenting, I was disappointed to find it required quite a bit of overhead and would not be able to fully satisfy my needs. Next, I turned my attention towards the AS3XLS library. From what I could gather, the library seems like what you’d want if you were trying to read & write native Excel files, but it didn’t provide CSV support.

So, I started searching the intertubes for another option. I found a great post by Abdul Qabiz, demonstrating a home baked solution he had come up with. The code he presented was quite dated (it was written for Flex 2, targeting the DataGrid control) and was missing some features I needed. However, it was exactly what I needed as a starting point. Learning from what he provided, I rolled my own solution that provided everything I was looking for.

Below is the AdvancedDataGrid CSV export utility class I came up with. It compiles in Flex 3.5, Flex 3.6 and Flex 4.5, handles grouped column headers, can double-quote values, and is capable of saving the CSV data of any language to a file (through the use of UTF-16 encoding). I am posting this simple class in case somebody else might find it useful. If you do, please leave a comment. Also, let me know if you find ways to improve it!

Note: The final version of the code uses a tab as the default CSV delimiter. This is due to a limitation in Excel when dealing with a comma-separated value file in UTF-16/Unicode encoding. Despite the use of a tab character, the format of the file is still viewed as a “CSV”. 🙂

Download the code.

CSVUtil.as:

package net.onyxmueller.util<br></br>{<br></br> import flash.net.FileReference;<br></br> import flash.utils.ByteArray;

import mx.collections.ICollectionView;
import mx.collections.IViewCursor;
import mx.controls.AdvancedDataGrid;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
import mx.controls.advancedDataGridClasses.AdvancedDataGridColumnGroup;

public class CSVUtil
{
private var _csvSeparator:String;
private var _lineSeparator:String;
private var _doubleQuoteValues:Boolean;
private var _doubleQuoteRexExPattern:RegExp = /\“/g;
private var _encoding:String;

public function CSVUtil(csvSeparator:String = “\t”, lineSeparator:String = “\n”, doubleQuoteValues:Boolean = true)
{
_csvSeparator = csvSeparator;
_lineSeparator = lineSeparator;
_doubleQuoteValues = doubleQuoteValues;
}

public function formatAsCSVString(items:Array):String
{
if (_doubleQuoteValues)
{
// escape any existing double quotes then place double quotes around values
for (var i:int = 0; i

— Onyx