tags:

views:

3488

answers:

5

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?

A: 

SpreadsheetGear for .NET will do it. You can see what people say and download the free trial to run this code:

namespace HelloCell
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a new empty workbook.
            SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
            // Set the worksheet name.
            SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets[0];
            worksheet.Name = "HelloSheet";
            // Add a text cell.
            worksheet.Cells["A1"].Value = "Hello Cell";
            // Save as Excel 97-2003 xls workbook.
            workbook.SaveAs("HelloWorkbook.xls", SpreadsheetGear.FileFormat.Excel8);
            // Save as Excel 2007 xlsx (Open XML) workbook.
            workbook.SaveAs("HelloWorkbook.xlsx", SpreadsheetGear.FileFormat.OpenXMLWorkbook);
            // Now load it back in.
            workbook = SpreadsheetGear.Factory.GetWorkbook("HelloWorkbook.xlsx");
            // Write cell A1 to the console.
            System.Console.WriteLine("A1='{0}'", workbook.Worksheets["HelloSheet"].Cells["A1"].Value);
        }
    }
}
Joe Erickson
This looks good except it is for C++, not C and "Developer Subscription" is 999$ :(
kliketa
It's actually for C#, not C++.
ephemient
Sorry, my mistake. Still, not C and is expensive
kliketa
My Bad - I read the question too fast and saw what I wanted to see (C#) instead of what the original poster actually asked for (C). Not sure what the proper etiquette is at this point. SpreadsheetGear can and is used from C (and VB6 and Java, etc...) but it is not as "simple" as the code I gave.
Joe Erickson
+2  A: 

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
Thanks! You have been very helpful regarding new Open XML format (I didn't know it existed)
kliketa
+4  A: 

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
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
No wonder C++ is going the way of the Dodo.
Rui Pacheco
+2  A: 

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
looks very interesting, but a little old (2004) project
kliketa
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
Well it may be "old", but the COM stuff is older ;-) and changes are not to be expected. The helper will just work...
Friedrich
A: 

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

dtw