views:

967

answers:

12

How to read/write data into excel 2007 in c++?

+1  A: 

Start here with the OpenXml Sdk. Download the SDK from here. The SDK uses .NET, so you might need to use C++.NET

Andrew Keith
I am skeptic about using .NET just for this purpose, since my app does not use .Net as of now
yesraaj
-1, doesn't work, question is tagged VC6.
MSalters
+2  A: 

A low tech way I have used on a couple of projects is to make a simple script/macro/whatever that runs an external program. Write that external program in C++. Get that external program to read its input from and write its output to a .csv file (simple comma separated value text file). Excel can easily read and write .csv files, so this setup gives you everything you need to craft a viable solution.

Bill Forster
A: 

It has been a very long time but I have used the Jet OLEDB to get at Excel files. You might start searching in that direction.

Jeff Alexander
+4  A: 

Excel provides COM interface which you can use from your C++ application.

I have experience with Excel 2003 only but I think for excel 2007 it will also work.

This can be done e.g. with #import or in the way described in this article: http://support.microsoft.com/kb/216686

Oleg
+3  A: 

There is a python solution (using COM dispatch) here: http://snippets.dzone.com/posts/show/2036

It's not C++, but the COM interface should be the same no matter which language you use, right?

You wouldn't need to port everything. Just __init__, set_range, get_value, set_value, save (or save_as), close, and quit. You might also need to dispose of garbage (as python has automatic gc).

Or you could just port (and modify) the following code (which I haven't tested, as I don't have excel anymore - you should probably check it by downloading python and pythonwin):

from win32com.client import Dispatch
app = Dispatch("Excel.Application")
app.Visible = True # spooky - watch the app run on your desktop!
app.Workbooks.Open("C:\\book.xls")

range = app.ActiveWorkbook.Sheets(1).Range("a1") 
print 'The range was:'
print range.Value

range.Value = 42

print 'The value is now 42'  

app.ActiveWorkbook.Save()
app.ActiveWorkbook.SaveAs("C:\\excel2.xls")

app.ActiveWorkbook.Close()
app.Quit()

# any gc to do?
wisty
+1  A: 

This can all be done via the IDispatch interface. It can get really bloody complicated quickly (Cheers Microsoft) but at least once you've got your head round Excel you'll find integrating with any other MS application easy too :)

Fortunately there is someone over on codeguru who has made the process nice and easy. Once I'd read through that code I started to get my head round what excel was doing and, furthermore, i found it became REALLY easy to extend it to do other things that I wanted. Just remember that you are sending commands to Excel via the IDispatch interface. This means you need to think about how YOU would do something to figure out how to do it programatically.

Edit: the code guru example is for Excel 2003 but it should be fairly easy to extend it to 2007 :)

Goz
A: 

I have used a 3rd Party component for this in the past: OLE XlsFile from SM Software (not free, but inexpensive). The advantage of this component over the Microsoft COM components is that you can use it to write XLS files even if Excel is not installed.

It also allows you to create speadsheets or workbooks with embedded formulas and formatting, something not possible if you use CSV files as an interchange format.

Bruce Ikin
A: 

If you need the best performance, writing binary Excel files is the way to go and writing them is not as difficult as reading them. The binary file format is relatively well documented by the OpenOffice.org project:

http://sc.openoffice.org/excelfileformat.pdf

and Microsoft has also released the documentation:

http://download.microsoft.com/download/0/B/E/0BE8BDD7-E5E8-422A-ABFD-4342ED7AD886/Excel97-2007BinaryFileFormat(xls)Specification.xps

This solution will work best if you have to write many Excel files, mostly with data and little formatting, and if you don't want to open the files at the same time. If you write a single Excel file to open it afterwards, you can probably use the propsed C++ COM Interface as the other posters have explained.

Using the COM interface will necessitate expensive out-of-process calls unless you write an Excel COM Addin. If you write an Addin that imports your data into the current Excel sheet, filling the sheet will still be much slower than dumping a file, but for a single file at a time this can be acceptable depending on your user scenario. If you do decide to use the COM interface, minimize the number of calls to Excel. Use the methods that allow to insert an array of values if they exist, set style properties by row and column is possible and not per cell.

Sebastian
+3  A: 

Excel 2007 files are simply zip files. Try to rename .xlsx to .zip: you can extract files and folders. With a text editor you can view that they are all XML files.

So the solution:

  1. use a common class to unzip your xlsx
  2. use an xml parser to grab you data
  3. if you have modified somethig, re-zip all

No COM object required. Depending on your c++ compiler you can easyly find the required sources.

Redax
A: 

You can use ODBC to read and write data from an excel file easily without Excel. The link:Here

The link for how to write data using odbc: Here

jle
A: 

I've used a set of C++ classes that is available from CodeProject before to write to XLS files.

It's really easy to use, and free! You can find it here.

Took me no time to set up.

Chaoz