使用 C# 将数据导出到 Excel(包括格式设置)

发布于 2024-10-10 12:18:32 字数 387 浏览 0 评论 0原文

我的程序中有一个数据表,我之前已使用 CSV 文件将其导出到 Excel。但是,我想有条件地设置单元格背景颜色的格式 - 这显然需要一种新方法。

问题是 - 运行该软件的计算机没有 Excel。所以我无法继续调用 Excel api 的路线。

有没有一种我错过的简单方法?如果没有,Excel文件的格式是否公开或者我怎样才能实现这个目标?

谢谢。


编辑:

对于其他感兴趣的人,我最终找到了 这篇文章 它提供了我发现的最简单的方法。适用于 Excel 2003。

I have a DataTable in my program which I have previously exported to excel using a CSV file. However, I want to conditionally format the backgroun colour of cells - which obviously needs a new approach.

The issue is this - the computer where the software will run does not have Excel. So I cannot go down the route of calling the Excel api.

Is there an easy way of doing this that I have missed? If not, is the format of Excel files published or how can I achieve this goal?

Thanks.


EDIT:

For anyone else that is interested, I eventually found this post which provides the simplest method I have found. Works with Excel 2003.

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

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

发布评论

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

评论(2

剩一世无双 2024-10-17 12:18:32

哪个版本的Excel?
如果您需要 excel 2003,请查看:
http://www.carlosag.net/Tools/ExcelXmlWriter/

如果您同意excel 2007,则有:
http://excelpackage.codeplex.com/(开源)
或者
http://msdn.microsoft.com/en-us/library/bb448854.aspx (微软官方产品)

Which version of excel?
If you need excel 2003, then take a look at :
http://www.carlosag.net/Tools/ExcelXmlWriter/

If you're ok with excel 2007, then there's:
http://excelpackage.codeplex.com/ (open source)
or
http://msdn.microsoft.com/en-us/library/bb448854.aspx (official MS product)

故事和酒 2024-10-17 12:18:32

查看 NPOI,它是 POI(免费的 MS Office 互操作库)的 .NET 端口。它功能强大且学习起来相当简单(尽管文档不明确)。我会阅读 NPOI 上过去的一些帖子(数量并不多),以熟悉一些令人沮丧的问题,这些问题与其说是问题,不如说是违反直觉的,并且从文档中并不总是显而易见的。

但一旦你克服了缺乏好的入门知识的困难,它就是一个很棒的库。

我猜你说的是 xls 格式。如果您可以使用较新的基于 XML 的 xlsx 格式,那么问题就完全不同了。您只需要编写 XML,该格式可以在 Microsoft 网站上找到。 可能 (请参阅其他答案)工具可以帮助解决此问题,但就我个人而言,我总是只使用 NPOI 并编写 XLS 文件以确保任何人都可以打开它们。

Check out NPOI, which is a .NET port of POI, a free MS office interop library. It's powerful and fairly straightforward to learn (though the documentation is iffy). I would read through some of the past posts here on NPOI (there aren't all that many) to become familiar with a few frustrating gotchas that are not so much problems as things that are counterintuitive and not always obvious from the documentation.

But once you get past the lack of a good primer it's an awesome library.

I assume you're talking about xls format. If you can use the newer XML based xlsx format, it's a totally different problem. You just need to write XML, the format is available on Microsoft's web site. There may be are (see other answer) tools that can help with this, though personally, I always just use NPOI and write XLS files to be sure anyone can open them.

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