<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="onCreation()"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" >
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.as3xls.xls.ExcelFile;
import flash.net.FileReference;
import flash.utils.ByteArray;
import mx.collections.ArrayCollection;
private var ef:ExcelFile;
private var ba:ByteArray;
private var fr:FileReference;
private var frSave:FileReference;
private var coulunmLength:int = 60;
[Bindable]
private var statCollection:ArrayCollection;
private function onCreation():void
{
ba= new ByteArray();
fr= new FileReference();
frSave= new FileReference();
}
private function importData(event:MouseEvent):void
{
fr= new FileReference();
var fileFilter:FileFilter = new FileFilter("Import File(*.csv;*.CSV)","*.csv;*.CSV");
fr.addEventListener(Event.SELECT,selectXLS);
fr.browse([fileFilter]);
}
private function selectXLS(event:Event):void
{
fr = FileReference(event.target);
fr.addEventListener(Event.COMPLETE,fileIn);
filenameLab.text="File Name :"+fr.name;
fileSizeLab.text="File Size :"+fr.size;
fr.load();
}
private function fileIn(event:Event):void
{
ba = new ByteArray();
ba = fr.data;
var content:String = ba.readUTFBytes(ba.length);
var contentArr:Array = content.split('\n');
contentArr = contentArr.filter(filterNullFunc);
trace(contentArr);
fillinDataGrid(contentArr);
}
private function filterNullFunc(item:*, index:int, array:Array):Boolean
{
return (item!=null && item!='' && item.length>0);
}
private function replaceAll( source:String, find:String, replacement:String ):String{
return source.split( find ).join( replacement );
}
private function fillinDataGrid(contentArr:Array):void
{
statCollection=new ArrayCollection();
var headers:Array = (contentArr.shift() as String).split(",");
for(var n:int = 0; n<headers.length;n++)
{
headers[n] = replaceAll(headers[n],"\"","");
}
var data:Array = contentArr;
var columns:Array = new Array();
if(data.length>0)
{
for(var m:uint = 0;m<data.length;m++){
var recordArr:Array = (data[m] as String).split(",");
// validate and restore is there is a , in String
if(recordArr.length>coulunmLength)
{
var correctRecord:Array = new Array()
for(var k:int = 0; k<recordArr.length;k++)
{
if(recordArr[0].toString().lastIndexOf("\"") == recordArr[0].toString().length-1&&recordArr[0].toString().indexOf("\"") == 0)
{
if(recordArr[k]!=null&&recordArr[k].toString().length>0&&recordArr[k].toString().lastIndexOf("\"")!=recordArr[k].toString().length-1)
{
correctRecord.push(recordArr[k]+","+recordArr[k+1]);
}
else if(recordArr[k]!=null&&recordArr[k].toString().length>0&&recordArr[k].toString().indexOf("\"")!=0)
{
continue;
}
else
{
correctRecord.push(recordArr[k]);
}
}
else if(recordArr[0].toString().lastIndexOf("\"") == -1 && recordArr[0].toString().length-1&&recordArr[0].toString().indexOf("\"") == -1 )
{
for(var p:int = 0; p<recordArr.length;p++)
{
if(recordArr[p]!=null&&recordArr[p].toString().length>0&&recordArr[p].toString().indexOf("\"")==0)
{
correctRecord.push(recordArr[p]+","+recordArr[p+1]);
}
else if(recordArr[p]!=null&&recordArr[p].toString().length>0&&recordArr[p].toString().lastIndexOf("\"")==recordArr[p].toString().length-1)
{
continue;
}
else
{
correctRecord.push(recordArr[p]);
}
}
}
}
recordArr = correctRecord;
}
var recordObj:Object = new Object();
for(var j:uint = 0;j<recordArr.length;j++){
recordObj['col'+j] = replaceAll(recordArr[j],"\"","");
}
statCollection.addItem(recordObj);
}
dg.dataProvider=statCollection;
}
}
]]>
</fx:Script>
<s:Label id="filenameLab" />
<s:Label id="fileSizeLab"/>
<s:Button label="Import CSV File" click="importData(event)" />
<mx:DataGrid x="20" y="60" id="dg" width="100%" height="100%" dataProvider="{statCollection}">
<mx:columns>
<mx:DataGridColumn width="20" dataField="col0" headerText="R/C"/>
<mx:DataGridColumn width="20" dataField="col1" headerText="Opt-In/Out"/>
<mx:DataGridColumn width="20" dataField="col2" headerText="Geo"/>
<mx:DataGridColumn width="20" dataField="col3" headerText="PL"/>
<mx:DataGridColumn width="20" dataField="col4" headerText="PL DESCRIPTION"/>
<mx:DataGridColumn width="20" dataField="col5" headerText="SKU"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
SCREEN SHOT
DEMO
SOURCE
XL FILE
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" creationComplete="onCreation()"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" >
<s:layout>
<s:VerticalLayout/>
</s:layout>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import com.as3xls.xls.ExcelFile;
import flash.net.FileReference;
import flash.utils.ByteArray;
import mx.collections.ArrayCollection;
private var ef:ExcelFile;
private var ba:ByteArray;
private var fr:FileReference;
private var frSave:FileReference;
private var coulunmLength:int = 60;
[Bindable]
private var statCollection:ArrayCollection;
private function onCreation():void
{
ba= new ByteArray();
fr= new FileReference();
frSave= new FileReference();
}
private function importData(event:MouseEvent):void
{
fr= new FileReference();
var fileFilter:FileFilter = new FileFilter("Import File(*.csv;*.CSV)","*.csv;*.CSV");
fr.addEventListener(Event.SELECT,selectXLS);
fr.browse([fileFilter]);
}
private function selectXLS(event:Event):void
{
fr = FileReference(event.target);
fr.addEventListener(Event.COMPLETE,fileIn);
filenameLab.text="File Name :"+fr.name;
fileSizeLab.text="File Size :"+fr.size;
fr.load();
}
private function fileIn(event:Event):void
{
ba = new ByteArray();
ba = fr.data;
var content:String = ba.readUTFBytes(ba.length);
var contentArr:Array = content.split('\n');
contentArr = contentArr.filter(filterNullFunc);
trace(contentArr);
fillinDataGrid(contentArr);
}
private function filterNullFunc(item:*, index:int, array:Array):Boolean
{
return (item!=null && item!='' && item.length>0);
}
private function replaceAll( source:String, find:String, replacement:String ):String{
return source.split( find ).join( replacement );
}
private function fillinDataGrid(contentArr:Array):void
{
statCollection=new ArrayCollection();
var headers:Array = (contentArr.shift() as String).split(",");
for(var n:int = 0; n<headers.length;n++)
{
headers[n] = replaceAll(headers[n],"\"","");
}
var data:Array = contentArr;
var columns:Array = new Array();
if(data.length>0)
{
for(var m:uint = 0;m<data.length;m++){
var recordArr:Array = (data[m] as String).split(",");
// validate and restore is there is a , in String
if(recordArr.length>coulunmLength)
{
var correctRecord:Array = new Array()
for(var k:int = 0; k<recordArr.length;k++)
{
if(recordArr[0].toString().lastIndexOf("\"") == recordArr[0].toString().length-1&&recordArr[0].toString().indexOf("\"") == 0)
{
if(recordArr[k]!=null&&recordArr[k].toString().length>0&&recordArr[k].toString().lastIndexOf("\"")!=recordArr[k].toString().length-1)
{
correctRecord.push(recordArr[k]+","+recordArr[k+1]);
}
else if(recordArr[k]!=null&&recordArr[k].toString().length>0&&recordArr[k].toString().indexOf("\"")!=0)
{
continue;
}
else
{
correctRecord.push(recordArr[k]);
}
}
else if(recordArr[0].toString().lastIndexOf("\"") == -1 && recordArr[0].toString().length-1&&recordArr[0].toString().indexOf("\"") == -1 )
{
for(var p:int = 0; p<recordArr.length;p++)
{
if(recordArr[p]!=null&&recordArr[p].toString().length>0&&recordArr[p].toString().indexOf("\"")==0)
{
correctRecord.push(recordArr[p]+","+recordArr[p+1]);
}
else if(recordArr[p]!=null&&recordArr[p].toString().length>0&&recordArr[p].toString().lastIndexOf("\"")==recordArr[p].toString().length-1)
{
continue;
}
else
{
correctRecord.push(recordArr[p]);
}
}
}
}
recordArr = correctRecord;
}
var recordObj:Object = new Object();
for(var j:uint = 0;j<recordArr.length;j++){
recordObj['col'+j] = replaceAll(recordArr[j],"\"","");
}
statCollection.addItem(recordObj);
}
dg.dataProvider=statCollection;
}
}
]]>
</fx:Script>
<s:Label id="filenameLab" />
<s:Label id="fileSizeLab"/>
<s:Button label="Import CSV File" click="importData(event)" />
<mx:DataGrid x="20" y="60" id="dg" width="100%" height="100%" dataProvider="{statCollection}">
<mx:columns>
<mx:DataGridColumn width="20" dataField="col0" headerText="R/C"/>
<mx:DataGridColumn width="20" dataField="col1" headerText="Opt-In/Out"/>
<mx:DataGridColumn width="20" dataField="col2" headerText="Geo"/>
<mx:DataGridColumn width="20" dataField="col3" headerText="PL"/>
<mx:DataGridColumn width="20" dataField="col4" headerText="PL DESCRIPTION"/>
<mx:DataGridColumn width="20" dataField="col5" headerText="SKU"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
SCREEN SHOT
DEMO
SOURCE
XL FILE