在 C++ 中以编程方式创建 Excel 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8266609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Programmatically creating Excel file in C++
提问by links77
I have seen programs exporting to Excel in two different ways.
我见过以两种不同方式导出到 Excel 的程序。
- Opening Excel and entering data cell by cell (while it is running it looks like a macro at work)
- Creating an Excel file on disk and writing the data to the file (like the Export feature in MS Access)
- 打开 Excel 并逐个单元格输入数据(在运行时它看起来像一个工作中的宏)
- 在磁盘上创建 Excel 文件并将数据写入文件(如 MS Access 中的导出功能)
Number 1 is terribly slow and to me it is just plain aweful.
1号非常慢,对我来说简直太棒了。
Number 2 is what I need to do. I'm guessing I need some sort of SDK so that I can create Excel files in C++.
2号是我需要做的。我猜我需要某种 SDK 以便我可以在 C++ 中创建 Excel 文件。
- Do I need different SDKs for .xls and .xlsx?
- Where do I obtain these? (I've tried Googling it but the SDKs I've found looks like they do other things than providing an interface to create Excel files).
- When it comes to the runtime, is MS Office a requirement on the PC that needs to create Excel files or do you get a redistributable DLL that you can deploy with your executable?
- .xls 和 .xlsx 是否需要不同的 SDK?
- 我从哪里获得这些?(我已经尝试使用谷歌搜索它,但我发现的 SDK 看起来除了提供创建 Excel 文件的界面之外,还可以做其他事情)。
- 说到运行时,PC 上是否需要 MS Office 来创建 Excel 文件,或者您是否获得了可以与可执行文件一起部署的可再发行 DLL?
回答by Baltasarq
You can easily do that by means of the XML Excel format. Check the wikipedia about that:
您可以通过 XML Excel 格式轻松地做到这一点。检查维基百科:
http://en.wikipedia.org/wiki/Microsoft_Excel#XML_Spreadsheet
http://en.wikipedia.org/wiki/Microsoft_Excel#XML_Spreadsheet
This format was introduced in Excel 2002, and it is an easy way to generate a XLS file.
这种格式是在 Excel 2002 中引入的,它是一种生成 XLS 文件的简单方法。
回答by Emir Akayd?n
You can also try working with XLS
/XLSX
files over ODBC
or ADO
drivers just like databases with a limited usage. You can use some templates if you need formatting or create the files from stratch. Of course you are limited by playing with the field values that way. For styling etc. you will need to use an Excel
API like Microsoft
's.
您还可以尝试使用XLS
/XLSX
文件ODBC
或ADO
驱动程序,就像使用有限的数据库一样。如果您需要格式化或从 stratch 创建文件,您可以使用一些模板。当然,您会受到以这种方式处理字段值的限制。对于样式等,您将需要使用's 之Excel
类的API Microsoft
。
回答by matiu
I'm doing this via Wt library's WTemplate
In short, I created the excel document I wanted in open office, and save-as
excel 2003 (.xml)
format.
简而言之,我在开放式办公室中创建了我想要的 excel 文档,并进行了save-as
excel 2003 (.xml)
格式化。
I then loaded that in google-chrome to make it look pretty and copied it to the clipboard.
然后我将其加载到 google-chrome 中以使其看起来很漂亮并将其复制到剪贴板。
Now I'm painstakingly breaking it out into templates so that Wt can render a new file each time.
现在我煞费苦心地将它分解成模板,以便 Wt 每次都可以渲染一个新文件。