开发者

Writing an Excel spread sheet in c++

开发者 https://www.devze.com 2023-03-24 08:12 出处:网络
I want to write some data to an xlsx spreadshhet (ultimately into an existing one with multiple shhets, but for now would be happy with creating a blank single sheet file).

I want to write some data to an xlsx spreadshhet (ultimately into an existing one with multiple shhets, but for now would be happy with creating a blank single sheet file).

Looking around it seems I need to use the excel ODBC driver (Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)) to write data into the spread sheet.

However this fails with: "Driver not installed: Micros开发者_高级运维oft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)"

I have the 64bit version of Office 2010 Pro Plus (I think thats the version students get?) installed, on Windows 7 Home 64bit. And am trying to create a 32bit application.

Is there some fundamental issue using ODBC here with office and windows editions, or the bitness of the application? Is ODBC the best choice here for simply writing some data into a file?

A requirment is also that I can write any file, and excel doesnt need to be open (but excel 2007 or later will be installed on the system).

CString sDriver = L"Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)";
CString sExcelFile = L"c:\\test.xlsx";
CString connect;
connect.Format(L"DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\"%s\";DBQ=%s",
    sDriver, sExcelFile, sExcelFile);


I would recommend you to use a CSV file (Comma Separated Values). That is very easily created and can be read by Excel (as well as OpenOffice's and iWork's products).


try http://www.libxl.com/ - not affiliated, not even a customer (since I use mostly C#)...

EDIT - as per comment:

the above lib would allow you to not only create but also read+modify existing excel files as you hint as a future goal in your first sentence...

For creating Excel files there some free options:
You can create CSV or (if some formatting and/or grouping and/or formulas etc. needed) HTML - Excel can open HTML and use this just fine if you take care to use the tags it understands - see http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx


There is another option.

ExcelFormat: A header file only C++ library that can write, read and format xls files. I know you asked for xlsx, but as Excel 2007 still can open these files it might be worth a try.

On top of that ExcelFormat is free (CPOL), easy to use and doesn't need an installed Excel at all. If you are interessted, a slightly updated version can be downloaded here.

0

精彩评论

暂无评论...
验证码 换一张
取 消