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?
Openpyxl is a Python library that is used to read from an Excel file or write to an Excel file. Data scientists use Openpyxl for data analysis, data copying, data mining, drawing charts, styling sheets, adding formulas, and more.
The Excel C API is the ideal choice when you want to create high-performance worksheet functions by creating XLL add-ins. The C API provides you with the most direct access to worksheet data. XLLs provide Excel with the most direct access to the DLL resources.
Extracting data from a closed file in another workbook is a common request by most of the excel user. They would like to pull or consolidate data from closed files; however, this is not possible.
Then go to the File tab on your Ribbon and click Open. Click Recent and then scroll to the bottom of the page and click Recover Unsaved Workbooks (see the screenshot below). The Open window will appear (see the screenshot below). Select the unsaved file you want to recover.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With