Thread: How to read an Excel file

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question How to read an Excel file

    I have a work: that how to read an microsoft excel file.
    For example: I have an excel file with multi sheets. (that I don't know what does these sheets' name). And my work is:
    1) how to scan this file to find what sheets does it have and what's name.
    2) after that, I want to read an exact cell that I want. for example: I want to read <programming sheet><cell A93>.

    I think this work need a library to work, and I don't know it. I play with algorithm very much, but I never do something like that before. So, who can help me, please

    thanks for all

  2. #2
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Not sure how you would do this directly. However you can save excel files with a single sheet to .csv, which would convert a sheet that looks like this:

    Code:
    +---+---+
    | 1 | 2 |
    +---+---+
    | 3 | 4 |
    +---+---+
    to a .csv file with these contents

    Code:
    1,2
    3,4
    For multiple sheets, you could write something in excel vba that would save each sheet to a separate .csv file, and then use your C++ program on those files. It's been a while since I used vba though, but I do know it's possible and fairly simple.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    Question

    I have googled and see that there're some ways, but they have a same method: convert excel file to cvs file.
    So, we cannot use excel file directly, huh :-?

  4. #4
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Well, it must be possible, but I'm guessing it'd be pretty complex unless there's some library out there. Why do you want to use the excel file directly?

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by hqt View Post
    I have googled and see that there're some ways, but they have a same method: convert excel file to cvs file.
    So, we cannot use excel file directly, huh :-?
    Yes you can, but you will probably need to use COM.
    Here's an example c++ for excel worksheets

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    I have convert an excel file to cvs file, After open by notepad and see that, to express each cell, excel use "," this very hard to use algorithm to process these cells.
    And, I google and no answer tell me how to convert excel file with multi sheets to multi cvs file by C++. So, who can help me, please

    thanks

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    how to convert excel file with multi sheets to multi cvs file by C++.
    You would probably be better off using Excel to export each sheet to a separate file. Trying to read an Excel file with C/C++ is difficult for several reasons. The first reason is that there are different versions of the file format depending on which version of Excel is being used.
    I have convert an excel file to cvs file, After open by notepad and see that, to express each cell, excel use "," this very hard to use algorithm to process these cells.
    Actually processing a Comma Separated Values file is fairly straight forward, as long as the actual data does not contain commas. Read the line with getline() to get the entire line into a std::string. Then using stringstreams use getline(), with the third parameter, or use the extraction operator<< to extract the data, using a throw away character to dispose of the comma.

    If you feel you must convert the actual Excel file with C/C++ you may want to start by studying this: Excel File Format.

    Jim

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    You would probably be better off using Excel to export each sheet to a separate file. Trying to read an Excel file with C/C++ is difficult for several reasons. The first reason is that there are different versions of the file format depending on which version of Excel is being used.
    Ohh, this make me sad and disappointed, because I always think C/C++ can do everything
    I have convert an excel file to cvs file, After open by notepad and see that, to express each cell, excel use "," this very hard to use algorithm to process these cells.
    Oh, I have use this way: convert the cvs file to two demension vector of string. And, we suppose excel table like two demension array. But, I see some problems with my solution. The most evident is very slow !!! Copy a table to array take a lot of time, huh
    @: I still like use directly excel file. Like above suggest and some source I have googled that use Visual C++. But, they say you must know COM application... and their code make me dizzy @@. My deadline is going to end up, so I hardly to learn a very new thing now. So, how to use this method in an esier way ?

    So, who has a new idea, please teach me.
    thanks
    Last edited by hqt; 11-09-2011 at 10:18 AM.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jimblumberg View Post
    Actually processing a Comma Separated Values file is fairly straight forward, as long as the actual data does not contain commas. Read the line with getline() to get the entire line into a std::string. Then using stringstreams use getline(), with the third parameter, or use the extraction operator<< to extract the data, using a throw away character to dispose of the comma.
    Wouldn't using getline directly with ',' specified as the delimiter be simpler ?

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Wouldn't using getline directly with ',' specified as the delimiter be simpler ?
    If you are trying to get numeric values you must still convert the string to the numeric values. Yes you can do all the processing on the file stream but I find string stream errors easier to handle than file stream errors, so I prefer to use getline() to get an entire line from the file then process that line using string streams.


    Jim

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by jimblumberg View Post
    If you are trying to get numeric values you must still convert the string to the numeric values.
    If different data types are there.... a much better approach could be use some database library...which would surely have ways to read in csv `s into relations.
    string stream errors easier to handle than file stream errors
    Agreed...
    Wondering how viable it is to pull the whole thing into a string stream..and using >>s for getting the data..(and making a custom manipulator to ignore the comma)
    Last edited by manasij7479; 11-09-2011 at 01:36 PM.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    a much better approach could be use some database library
    Possibly, but in my opinion at the cost of much higher complexity. And no matter what the programmer must know what each data element represents.

    Wonder how viable is pulling the whole thing into a string stream..and using >>s for getting the data..(and making a custom manipulator to ignore the comma)
    Again, possible as long as the file size does not exceed the string stream maximum size. In my opinion using this method only becomes reasonable if profiling determines a need for a different approach, and then only if you are going to be parsing this data source quite often.

    Jim

  13. #13
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    Quote Originally Posted by hqt View Post
    Ohh, this make me sad and disappointed, because I always think C/C++ can do everything
    And with enough time, perseverance, and budget you probably can

    After all, Excel itself was written (largely) in C and C++, so no reason you can't do the same, in theory.
    Whether it is practical is of course another matter entirely.

    A quick search of the internet found several libraries that claim to be able to handle XLS documents .
    C++ Excel Library to read/write xls/xlsx files - LibXL
    C++ Library For Direct Writing Excel Spreadsheets | PRLog
    C++ Library for Excel

    These are commercial products. The first several pages of search results didn't yield an open source or otherwise free library (except one announcement from 2 years ago stating someone's starting one, but that one died already).

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suppose with some digging (OK, make that a lot of digging), you might find something in the source code for Home » LibreOffice
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Write/Read Excel Cells
    By rogster001 in forum C Programming
    Replies: 15
    Last Post: 02-26-2008, 04:54 AM
  2. Read Excel File from Windows Program
    By baburajv in forum Windows Programming
    Replies: 2
    Last Post: 11-07-2006, 01:42 PM
  3. How to access excel sheet with C program(open,read,write)
    By jaininaveen in forum C Programming
    Replies: 4
    Last Post: 01-29-2006, 11:17 AM
  4. Read/Write a excel file using C program & OS is DOS
    By akhitesh in forum C Programming
    Replies: 2
    Last Post: 05-13-2003, 11:07 PM
  5. how do access and read data in from an excel file
    By hamilton73 in forum C++ Programming
    Replies: 2
    Last Post: 09-26-2001, 10:00 PM