Thread: Open an Excel file from C

  1. #1
    Banned
    Join Date
    Jul 2011
    Posts
    12

    Open an Excel file from C

    How do I open an excel file from C?

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Sjvlsdsd View Post
    How do I open an excel file from C?
    See the stdio library, available on *most* "end-user-programmer" compilers (ie. CL in windows, GCC on Linux, Solaris, or OS X, Cygwin, etc.).

    You'll need to read in the file, then parse it in according with the format MS has already determined (XLS or XLSX, depending on the version). Which isn't followed to spec, 100%, if I remember correctly.

    You can just crack open an excel file in Notepad++ or Gedit or Kate (most text editors less notepad, really), and see the textual data inside - you'll need to parse that to get any "good" info out of it. Though if you search the web, I'm sure somebody has already made a parser for XLS and XLSX files, possibly look at the code for LibreOffice, as that will open XLS and XLSX files quite well.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You can save the parse worries, if you export the file from excel, as plain text. If the plain text is the data you want, of course, rather than the rest of the Excel file format.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Rochester, NY
    Posts
    196
    Quote Originally Posted by Adak View Post
    You can save the parse worries, if you export the file from excel, as plain text. If the plain text is the data you want, of course, rather than the rest of the Excel file format.
    Very true, also, you can export an excel document in CSV format (comma separated values) - which makes parsing using a string tokenizer a complete breeze! That works for data extraction in plain text.

    Though if you want the other stuff, the formatting, the colors, the fonts, all the other "pretty" stuff you see in an excel document, you'll need to dig a little deeper into the file, as not all of that can be exported in easy to parse formats.

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    If you really want to work with Excel documents you're going to have to support various different formats of them....for example Excel 97 and older version do not use the new xlsx format. This is actually a ZIP file which contains various other files, the content, the images that might be included (thats right there can be images and graphs and all sorts of things) almost ANYTHING can go into an Excel document, we're talking about Micros*** here...

    So, whats the best answer? Find an existing library (or use windows existing api for this) to access the files, I HIGHLY advise you do NOT attempt to "parse" it yourself...

    The best phrase you should always remember in coding: Do not re-invent the wheel.
    REUSE existing code, if someone else solved your problem already, they probably solved it a lot better then you can in 20 minutes hacking something out.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    If all you need is to analyze/use/copy/whatever the raw data and headers, I second the CSV approach. Between csv format and the std::string, you can do quite a lot.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Ocifer View Post
    Between csv format and the std::string, you can do quite a lot.
    std::string is C++, this question is about C.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Microsoft Excel, along with all the other office programs are exposed through COM interfaces. If you actually want to work with Excel files, vice a shell command to open them or the suggestions given above, you are going to have to use this interface. Note the .NET framework does a lot of this for you automatically. Can you create an MFC program with your compiler?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    The better way to do this is to use an existing library (unless you want to write off a long time writing one from scratch):
    * Use COM Automation - Probably the preferred way, but its ugly in C++ and will be hideous in straight C
    * Use an existing open source library. Theres almost certainly one out there for excel as Open Office and Gnumeric (etc) can handle excel file formats. The one for (older version) word is called "wv" and I don't know what the equivalent is for excel. You would also need a version for the 2007 format as well as one for the earlier ones

    The easiest workaround is to do as already suggested and force users to save the data in csv first.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    88
    Quote Originally Posted by quzah View Post
    std::string is C++, this question is about C.


    Quzah.
    Right you are, I go between the two boards, forgot where I was. Replace std::string in my original post with string.h, ctype.h and some strtok().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPEN an Excel file with MACROS DISABLED
    By highlowjack in forum Windows Programming
    Replies: 6
    Last Post: 11-02-2007, 06:10 PM
  2. Open an excel file from a windows service under Windows Vista
    By AdrianaLuby in forum C# Programming
    Replies: 1
    Last Post: 06-05-2007, 03:55 AM
  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. open excel-file
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 01-31-2002, 02:55 AM
  5. How to convert access file to excel file????
    By husna in forum Windows Programming
    Replies: 2
    Last Post: 09-03-2001, 09:59 PM