Skip to main content
Kieran Briggs
Participant
September 30, 2008
Question

Read a CSV file into Flex

  • September 30, 2008
  • 2 replies
  • 17092 views
Hi all,

I'm new to flex and have been asked to provide a small widget that will display the contents of a csv in a list. Can I use the HTTPService to open a csv file and then create an actionscript 3 function to parse it? If so can anyone point me to a tutorial on how to parse a csv file please.

Thanks in advance
    This topic has been closed for replies.

    2 replies

    June 1, 2009

    Please find attachment.

    Participant
    June 1, 2009

    Thanks for your help.I have browse button to open the file and use will select the file location. That's why i am using current approach. can you please send me sample code to read the attached xls file please.

    Participating Frequently
    August 9, 2010

    I am using following code to read xlsx file in Flex AIR 4 code. It not reading anything I am getting "PK   "  as output.

    Please help me to read correctrly xlsx file in Flex.

    private function btnCSV_click():void

    {

    var strFilePath :String = File.applicationDirectory.nativePath + "\\Test.xlsx";

    var loader:URLLoader = new URLLoader();

    configureListeners(loader);

    var request:URLRequest = new URLRequest(strFilePath);

    request.contentType =

    "text/csv";

    try {

    loader.load(request);

    }

    catch (error:Error) {

    trace ("Unable to load requested document.");

    }

    }

    private function configureListeners(dispatcher:IEventDispatcher):void

    {

    dispatcher.addEventListener(Event.COMPLETE, completeHandlerCSV);

    }

    private function completeHandlerCSV(event:Event) :void

    {

    trace ( event.target.data );

    }

    September 30, 2008
    I've not done this in AS, but it shouldn't be too difficult.

    You will just need to use split() to first put the lines in an array then again to put the fields in an array for each line.

    Split the fields on comma, obviously. I'd have to experiment some to see how to split the lines on CR.

    You might google this, someone has probably already done it.

    Tracy
    Participant
    June 1, 2009

    Hello Sir,

      I am new to flex and want to read a csv file and using below code but does not seems to be working. Can you please help?

      i use below code but did not work

    var file:File = evt.currentTarget as File;
             file = file.resolvePath(file.nativePath);
             var fileStream:FileStream = new FileStream();
             fileStream.open(file, FileMode.READ);     
             var fileData:String =  fileStream.readUTFBytes(fileStream.bytesAvailable);       
             var endings:Array = [File.lineEnding, "\n", "\r"];

    but for some reason it return "ÐÏ ࡱá" funny value. Any idea why don't i get the correct data from file.

    belwo is the csv file i am trying to open.

    Title

    Given_Name

    Surname

    Gong

    Salutation

    Position

    Organisation

    Address_Line_1

    Address_Line_2

    Address_Line_3

    Suburb

    State

    Postcode

    Country

    Home_Phone

    Fax

    Other_Phone

    User_Field_1

    User_Field_2

    User_Field_3

    User_Field_4

    Mobile_Phone

    Second_Address

    Second_Address_Line_1

    Second_Address_Line_2

    Second_Address_Line_3

    Second_Suburb

    Second_State

    Second_Country

    Second_Postcode

    Langcode

    Website

    Mr.

    Jeff

    Alexander

    Retention Marketing

    Monday, April 13th, 2009

    Mr.

    Anthony

    Demaso

    Retention Marketing

    Monday, April 13th, 2009

    Sally

    Swinamer

    Yield

    Monday, April 13th, 2009

    Chris

    Torbay

    Yield

    Monday, April 13th, 2009

    Annette

    Warring

    Genesis Vizeum

    Monday, April 13th, 2009

    Mr.

    Mark

    Khoury

    Genesis Vizeum

    Monday, April 13th, 2009

    Mr.

    Andy

    Thorndyke

    Thorsons

    Monday, April 13th, 2009

    Shannon

    Rutherford

    Central Reproductions

    Monday, April 13th, 2009

    Mr.

    Rob

    Greenwood

    Central Reproductions

    Monday, April 13th, 2009

    Lisa

    Marchese

    Des Rosiers

    Monday, April 13th, 2009

    Mr.

    Michael

    Whitcombe

    McMillan LLP

    Monday, April 13th, 2009

    Thanks,

    Gill