30

Well... that's it.

I need something simple and reliable (doesn't have to have fancy features - I need to write and read text and numbers from Excel cells)

And yes, I would like a sample "Hello Cell" code...

What do you recommend?

pnuts
  • 58,317
  • 11
  • 87
  • 139
kliketa
  • 1,276
  • 3
  • 17
  • 23
  • If possible Windows and Linux, but if not then just Windows since I want to interact with Excel which is on Windows. – kliketa Jan 30 '09 at 13:19
  • Excel on Linux, I'd be very suprised to find it. OpenOffice is there and has a usable interface for C++.... Maybe that's what you'd like to try – Friedrich Feb 01 '09 at 13:47
  • 1
    "Cello Hell", that's where all the bad Cellists go and make terrible noises together... :/ I'd have a look at the gnumeric source code, it's probably modular. If you raise money to pay me $1000 I will fix it up into a library ;) – Sam Watkins Jan 18 '13 at 06:44
  • I cannot understand why this 'recommend a library' question got 26 upvotes, instead of being closed as off-topic:( SO is terribly inconsistent.. – Martin James Jul 07 '15 at 20:34
  • @MartinJames It was missed. I clicked on the link from your meta post specifically to close whatever was on the other side. – durron597 Jul 07 '15 at 20:49
  • @MartinJames This questions is from 2009. It's ancient in Stack Overflow terms, and our criteria for on-topic questions has evolved over time. – user229044 Jul 08 '15 at 15:51

5 Answers5

12

Strongly discouraged. I'd recommend using a C-friendly format (e.g. CSVs) instead of XLS, or using the new XML formats (take your pick on XML and ZIP libraries).

Still, for a quick fix, you could export to quoted CSV and then import using VBScript. Something like this, although I'd try to get it to work in VBA first.

Note that this will require a copy of Office, and will not scale well (but you can hide the Excel window).


I've just found xlsLib, so if you really need to write directly in C, give it a go! Be careful though, because it's very hard to get right, especially if you're writing to already-existing files.

There also exists LibExcel, but that's C++, so you'd need to compile a wrapper around, or rewrite for C.


One final caveat: the reason I didn't search for these at the start is that it's extremely difficult to get right. I have not used the libraries above, but I suspect they'll break in strange and unusual ways. I trust you've read Joel's take on the Office formats.

Mark
  • 6,269
  • 2
  • 35
  • 34
12

Excel, like other Office products, exports its guts over COM. This is available for use in C++, VB, C#, and whatever other languages have COM interop -- provided that you're running in Windows and have Excel installed. (You can't get to COM from plain C. Whether this is fortunate or unfortunate is up to you.)

COM is a bloody messy pain for unmanaged languages, though. The following VB:

Set objExcel = CreateObject("Excel.Application")  ' start or use existing Excel
objExcel.Visible = True                           ' show the window
objExcel.Workbooks.Add                            ' create an empty workbook

roughly translates into the following C++:

#include <assert.h>
#include <ole2.h>
#include <tchar.h>

int main() {
    HRESULT hr;
    IDispatch *objExcel, *objWorkbooks;
    CLSID clsid;
    DISPID id, id2;
    DISPPARAMS p;
    VARIANT v;
    TCHAR *name;

    CoInitialize(NULL);

    hr = CLSIDFromProgID(_T("Excel.Application"), &clsid);
    assert(SUCCEEDED(hr));
    hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER,
            IID_IDispatch, (LPVOID *)&objExcel);
    assert(SUCCEEDED(hr));

    id2 = DISPID_PROPERTYPUT;
    name = _T("Visible");
    hr = objExcel->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    VariantInit(&v);
    v.vt = VT_I4;
    v.lVal = 1;
    p.cArgs = 1;
    p.rgvarg = &v;
    p.cNamedArgs = 1;
    p.rgdispidNamedArgs = &id2;
    hr = objExcel->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYPUT, &p, NULL, NULL, NULL);
    assert(SUCCEEDED(hr));

    name = _T("Workbooks");
    hr = objExcel->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    p.cArgs = 0;
    p.rgvarg = NULL;
    p.cNamedArgs = 0;
    p.rgdispidNamedArgs = NULL;
    hr = objExcel->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET, &p, &v, NULL, NULL);
    assert(SUCCEEDED(hr));
    objWorkbooks = v.pdispVal;

    name = _T("Add");
    hr = objWorkbooks->GetIDsOfNames(IID_NULL, &name, 1, LOCALE_USER_DEFAULT, &id);
    assert(SUCCEEDED(hr));
    p.cArgs = 0;
    p.rgvarg = NULL;
    p.cNamedArgs = 0;
    p.rgdispidNamedArgs = NULL;
    hr = objWorkbooks->Invoke(id, IID_NULL, LOCALE_SYSTEM_DEFAULT,
            DISPATCH_PROPERTYGET, &p, NULL, NULL, NULL);
    assert(SUCCEEDED(hr));

    return 0;
}
ephemient
  • 198,619
  • 38
  • 280
  • 391
  • I admit I didn't google much for this question, but that is what stackoverflow is for :) Seriously, I knew that there will be problems finding this library, but I didn't knew that **no one** has managed to write or read a plain Excel cell with no functions,formatting,just text and numbers.you go UP! – kliketa Jan 30 '09 at 09:15
  • 1
    No wonder C++ is going the way of the Dodo. – ruipacheco Oct 29 '09 at 23:33
  • There's a lot you can do with COM, but there's not a lot you *want* to do with COM. – dreamlax Sep 05 '14 at 00:38
  • @kliketa - no, it's not:( – Martin James Jul 07 '15 at 20:16
  • 1
    @dreamlax whatever do you mean? COM is really simple and the easily-comprehended TLB's are a joy to... . hehe, OK not really:) – Martin James Jul 07 '15 at 20:20
3

A free library, but again this is C++, not C: ExcelFormat

dtw
  • 1,785
  • 1
  • 13
  • 13
2

Another option is to use libxlsxwriter, a C library for writing Excel XLSX files. However, it can't read Excel files and it doesn't support XLS files.

Zenadix
  • 15,291
  • 4
  • 26
  • 41
2

I've a small extension to the stuff ephemient has posted. There's very handy library available with eases the paind with the IDispatch interface. http://disphelper.sourceforge.net/

It's just one file you add to your projects but it makes IDispatch programming with C nearly fun ;-)

Friedrich
  • 5,916
  • 25
  • 45
  • looks very interesting, but a little old (2004) project – kliketa Feb 01 '09 at 16:05
  • Wow, that's really quite useful! kliketa: doesn't matter how old it is, the underlying COM protocol is still the same. http://disphelper.cvs.sourceforge.net/viewvc/disphelper/disphelper/samples_c/excel.c?view=markup will still work just fine on the latest Windows and Office versions. – ephemient Feb 01 '09 at 16:24
  • Well it may be "old", but the COM stuff is older ;-) and changes are not to be expected. The helper will just work... – Friedrich Feb 02 '09 at 05:32
  • Still works in 2017... – Prof. Falken Mar 14 '17 at 10:39