Thread: Q: Simpliest way to use Excel file?

  1. #1
    cDir
    Guest

    Q: Simpliest way to use Excel file?

    I would like to take a set of records each defined as simple structs of strings (the fields), and put them into an Excel spreadsheet, where each one row in the Excel spreadsheet will correspond to one record.

    I am using Visual C++ 5.0 (standard ed.). I know C/C++, and have limited experience with the Win32 API.

    The simpliest description of what I need to figure out how to do is (I hope):
    1. create a file containing a simple spreadsheet
    2. add a row containing a set amount of cells (the # of fields)
    3. populate the cells with data
    4. repeat steps 2 & 3 an indeterminate number of times

    Can anyone suggest a resource detailing the simpliest way to do this and (not necessarily) anything beyond this?

    As recommended I have tried reading Microsoft's "OLE DB programmer's reference", but was completely lost. I do not understand what COM or OLE is, and have no real experience with MFC (though I do have a large book explaining the latter). My second question then is, can I accomplish my task without investing an incredible amount of time learning about COM or OLE? Perhaps there is a simple set of C functions in an SDK somewhere to do what I need?

    Thanks in advance for any advice,
    cDir

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Simplest way I know is to create a type of Excel sheet called a '.csv' (comma seperated volume)

    I use CreateFile() and WriteFile() with a char buffer. Set the file name to *.csv and then use sprintf() to enter the data to a string, place a comma where the cell should be.

    ie
    char sBuffer[256]="\0";
    sprintf(sBuffer,"%s,%s,%s,%s",Struct.sCell_1,Struc t.sCell_2,....);
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    >(comma seperated volume)

    I think it's seperated by ';', so use this instead of commas in the fprintf statement
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Ahh, No.

    Its commas. 100% sure.

    //EDIT
    Not that I know that ';' will or won't.
    //END EDIT

    After my boss made me transfer all 240+ components (had to leave some out to fit it into Excel) from a binary to .csv so he could 'look' at the results. I learnt its a comma.

    Try writing that sprintf() line! The sprintf for teh title strings alone takes 17 lines across my screen.
    Last edited by novacain; 01-31-2002 at 03:20 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    cDir
    Guest
    Ah, wonderful!

    As you may have guessed, I do not have much experience using Excel either.

    I was preparing for the worst when I posted, but it appears as though this will do the trick.

    Thank you, and here's to hoping that this will be sufficient.

    ,cDir

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    hm...

    Well, on opening a csv file, excel expects commas. If I select 'Save As...' and save my xls file as csv, it puts ; in my file as seperators...

    Could you check this with a non-german version ?
    I think that's weird.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I think Excel will except more than 1 type of delimiter if the file has a .csv extention

  8. #8
    cDir
    Guest
    >Could you check this with a non-german version ?

    I am using Excel97 - U.S. version.

    When saving as *.csv, Excel puts commas in. I replaced the commas with semicolons and attempted to open the file. It placed each semicolon delimited line (in the file) into the first cell of a row (in the sheet).

  9. #9
    cDir
    Guest
    (argh, it would appear that I can not edit my prev. post without registering)

    I also created a *.xls file with Excel, and saved it as a *.csv. It correctly put in the commas.

    Sorry, I had read your question incorrectly.

  10. #10
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Mines commas as well. (if I create a sheet and save as .csv, open in notepad has ',')

    Very wierd you are getting ';'.

    I am using Office2K Pro with Australian English settings.
    If I use ';' it wont recognise the cells.

    M$ bug?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  11. #11
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Actually, my versions only accept ',' as seperator as well. If I have ; I only get all in one cell per line. But Excel saves it that way. Maybe it's because a comma is the decimal sign in German. That would make sense in a way. However, saving anything but commas in a comma separated value file is just crazy.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM