用 c++ 编写 Excel 电子表格

发布于 2024-11-27 06:01:26 字数 792 浏览 1 评论 0 原文

我想将一些数据写入 xlsx spreadshhet(最终写入具有多个 shhet 的现有数据,但现在很乐意创建一个空白的单页文件)。

环顾四周,我似乎需要使用 Excel ODBC 驱动程序(Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb))将数据写入电子表格。

然而这失败了: “未安装驱动程序:Microsoft Excel 驱动程序(*.xls、*.xlsx、*.xlsm、*.xlsb)”

我在 Windows 上安装了 64 位版本的 Office 2010 Pro Plus(我认为这是学生获得的版本?) 7 家庭 64 位。我正在尝试创建一个 32 位应用程序。

在 Office 和 Windows 版本中使用 ODBC 是否存在一些基本问题,或者应用程序的位数? ODBC 是简单地将一些数据写入文件的最佳选择吗?

还有一个要求是我可以写入任何文件,并且不需要打开excel(但系统上会安装excel 2007或更高版本)。

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 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: Microsoft 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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风疏影 2024-12-04 06:01:26

我建议您使用 CSV 文件(逗号分隔值)。它非常容易创建,并且可以通过 Excel(以及 OpenOffice 和 iWork 的产品)读取。

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).

つ可否回来 2024-12-04 06:01:26

尝试 http://www.libxl.com/ - 不附属,甚至不是客户(因为我主要使用C#)...

编辑 - 根据评论:

上面的库不仅允许您创建,还允许您读取+修改现有的 excel 文件,正如您在第一句话中暗示的那样,作为未来的目标

...创建 Excel 文件有一些免费选项:
您可以创建 CSV 或(如果需要某些格式和/或分组和/或公式等)HTML - Excel 可以打开 HTML 并使用它,如果您小心使用它理解的标签 - 请参阅 http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx

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

初心 2024-12-04 06:01:26

还有另一种选择。

ExcelFormat:一个仅包含头文件的 C++ 库,可以写入、读取和格式化 xls 文件。我知道您要求 xlsx,但由于 Excel 2007 仍然可以打开这些文件,因此可能值得一试。

最重要的是,ExcelFormat 是免费的 (CPOL)、易于使用并且根本不需要安装 Excel。
如果您有兴趣,可以在此处下载稍微更新的版本。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文