How do I open an excel file from C?
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.
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.
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.
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.
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?
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.