2

In a project I'm working the client has asked at the last minute that I add functionality to import data from an Excel spreadsheet. The examples he's sent me have an .xlsx extension so I'm assuming they are from Excel 2010, but I'd like to support all versions if possible. Is there a quick and easy way to open and read data from an Excel spreadsheet from a C program?

The only idea I can think of is to connect to the spreadsheet as if it were a database and SELECT the information I need from it. Is this a good approach? How do I connect to an Excel spreadsheet through C? I'd like to use the Windows API as much as possible.

Sparafusile
  • 4,696
  • 7
  • 34
  • 57
  • AFAIK, excel sheet cannot be read as it is. It needs to be converted to `.csv` format and then can be read. – Mahesh Mar 23 '11 at 17:47
  • It would be a trivial problem if I required them to convert to .csv format. I'd rather not make that a requirement if I can help it though, sometimes it introduces too much of a learning curve. – Sparafusile Mar 23 '11 at 17:58

2 Answers2

2

You can connect to an Excel workbook through an ODBC driver. Control panel | Administrative tools | Data sources (ODBC)

Mike Sherrill 'Cat Recall'
  • 91,602
  • 17
  • 122
  • 185
  • No, but there is a Control Panel in Windows. That's where you manually configure a DSN, which lets you connect to an Excel workbook through ODBC as if it were a database. (More or less.) You can create a DSN using the Windows API, too. – Mike Sherrill 'Cat Recall' Mar 23 '11 at 18:21
  • I'm not going to train every user of my program to create a ODBC, sorry. What is the Windows API that creates one programmatically? – Sparafusile Mar 23 '11 at 18:26
  • 2
    I don't know off the top of my head. If I were trying to code it, I'd probably Google "windows api dsn". But I'm pretty sure there's no "Google" in C. – Mike Sherrill 'Cat Recall' Mar 23 '11 at 20:30
2

You can use the OLE Automation feature to start Excel and let it read the file four you.

See examples here:

http://en.wikipedia.org/wiki/OLE_Automation

Bo Persson
  • 90,663
  • 31
  • 146
  • 203