Thread: How read from spreadsheet?

  1. #1
    Registered User
    Join Date
    Sep 2011

    How read from spreadsheet?

    Hello all:

    I am accustomed to read/write txt/ASCII file with ifstream/ofstream.

    If the data file is in the EXCEL spreadsheet, *.csv, *.xls, *.xlsx, then what is the way to read them?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    AFAIK .xls is a pretty complex format.

    .csv (Comma Separated Values) on the other hand, is exactly what's on the tin: a list of values separated by commas. You can open a csv file in Notepad to see the exact formatting Excel uses. Other than interpreting the text (ie, finding the commas, reading floats vs strings) it's not any different than a standard text file.
    Consider this post signed

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Liverpool UK
    Dont bother with .xls - at all. save your data as .csv or similar.
    if it is presently in multiple worksheets as an xls workbook, then you will have to save each sheet as a seperate .csv file.
    Depending on where your data is imported / exported from or where it is saved or created will define the exact format your .csv file will actually have. for example some settings in excel will mean that strings in the fields are surrounded by " " to demarcate them as strings. With this in mind your program should be able to parse each row of the file and cope with " " wrappers or lack thereof, this is beyond the actual parsing for the delimiting token itself.
    You can use std::string objects with getline() as a simple way to practice with .csv files.
    The string objects give you lots of easy to use functions built in that you can take advantage of to extract what you want from your source data.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Hi, Thanks so much for instructions.

    Suppose my spreadsheet file is aaa.csv, the data are:

    01/10/12 100.01
    01/11/12 101.02
    01/12/12 102.03
    01/13/12 105.03
    01/14/12 107.07

    Now I need to read them into my program variables. the 1st column into "string date[5];"
    and the 2nd column into "float price[5];"

    What is the way/codes for me to achieve that?


  5. #5
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Generally, you would do something like
    struct Row
        std::string data;
        float price;
    std::vector<Row> rows;
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spreadsheet I/O formats
    By Else in forum C++ Programming
    Replies: 5
    Last Post: 08-21-2009, 05:29 PM
  2. Help with spreadsheet and database
    By glo in forum Tech Board
    Replies: 2
    Last Post: 10-30-2007, 09:42 AM
  3. 2D Array spreadsheet question
    By cdonlan in forum C Programming
    Replies: 7
    Last Post: 01-24-2005, 08:22 AM
  4. Is it possible to write into an Excel spreadsheet?
    By rtsc17 in forum C++ Programming
    Replies: 6
    Last Post: 09-18-2003, 10:15 AM