Excel 互操作替代方案

发布于 2024-11-29 00:26:04 字数 162 浏览 0 评论 0原文

我已开始使用 Microsoft 为我的 C# 应用程序之一提供的 Excel 互操作程序集。一切都很顺利,但似乎缺乏强类型,老实说感觉就像我正在编写 VBA 代码。是否有其他方法可以通过 C# 与 Excel 进行交互,从而提供更好的 OO 体验?顺便说一句,我正在使用 VS2010 和 .Net 4.0。

I have started using the Excel interop assemblies that are provided by Microsoft for one of my C# applications. Everything is going fine, but there seems to be a lack of strong typing, and honestly it feels like I am writing VBA code. Are there any alternative ways to interact with Excel from C# that would give a better OO experience? I am using VS2010 and .Net 4.0 by the way.

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

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

发布评论

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

评论(7

合约呢 2024-12-06 00:26:04

看看 Codeplex 上的 EPPlus 项目:

http://epplus.codeplex.com/

我最近使用了这个,它非常有效出色地。 (VS2010 和 .Net4)

EPPlus 是一个 .net 库,它使用 Open Office Xml 格式 (xlsx) 读取和写入 Excel 2007/2010 文件。

不错的示例页面:http://epplus.codeplex.com/wikipage?title=ContentSheetExample

Take a look at the EPPlus project over at Codeplex:

http://epplus.codeplex.com/

I recently used this and it worked very well. (VS2010 and .Net4)

EPPlus is a .net library that reads and writes Excel 2007/2010 files using the Open Office Xml format (xlsx).

Nice example page: http://epplus.codeplex.com/wikipage?title=ContentSheetExample

ペ泪落弦音 2024-12-06 00:26:04

根据您想要执行的操作(例如生成 XLSX 文档),您可以使用 打开XML SDK

这对于生成办公文档很有用;特别是因为它不需要安装 Office 即可使用。

它是免费的;一个漂亮干净的 API,并且受到 Microsoft 的支持。例如:

public static void CreateNewWordDocument(string document)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
    {
        // Set the content of the document so that Word can open it.
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

        SetMainDocumentContent(mainPart);
    }
}

示例来自: http://msdn.microsoft.com/en-us /library/bb497758.aspx此处提供了使用电子表格的示例。

如果您尝试直接与 Excel 交互(如 UI 自动化);不生成文档;那么 KeithS 的答案就是我会怎么走。

Depending on what you are trying to do, like generate an XLSX document, you can use the Open XML SDK.

This is a nice for generating office documents; especially because it doesn't require office to be installed to use.

It's free; a nice clean API, and is supported by Microsoft. For example:

public static void CreateNewWordDocument(string document)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(document, WordprocessingDocumentType.Document))
    {
        // Set the content of the document so that Word can open it.
        MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();

        SetMainDocumentContent(mainPart);
    }
}

Example from: http://msdn.microsoft.com/en-us/library/bb497758.aspx. There are examples for working with Spreadsheets here.

If you are trying to directly interact with Excel (Like UI automation); not generate a document; then KeithS's answer is how I'd go.

感受沵的脚步 2024-12-06 00:26:04

NetOffice 库是 Office 主互操作程序集 (PIA) 的替代方案。 NetOffice 提供了一组独立于版本的互操作库,因此您可以使用应用程序支持所有 Office 版本,并且包含 IntelliSense 来向您显示哪些方法在哪些 Office 版本下可用。

NetOffice 还有助于跟踪 COM 引用,这有时会在 .NET / COM 互操作中带来麻烦。

The NetOffice libraries are an alternative to the Office Primary Interop Assemblies (PIAs). NetOffice provides a version-independent set of interop libraries, so you can support all Office versions with your app, and includes IntelliSense to show you what methods are available under which Office versions.

NetOffice also helps a bit in keeping track of the COM references, which can sometimes be a pain in .NET / COM interop.

汹涌人海 2024-12-06 00:26:04

您正在寻找什么类型的互动?只是写作?阅读?全面掌控各个方面?

有很多第三方库可用于操作 Excel 文档,特别是如果您使用 xlsx 格式(2007 或更高版本)。如果您只是将内容写出来,甚至可以查看开源实现(Sourceforge 有 Excel Writer< /a>,例如)。

What type of interaction are you looking for? Just writing? reading? Full control of all aspects?

There are plenty of third party libraries for manipulating Excel documents, especially if you stay in the xlsx format (2007 or newer). If you are merely writing things out, there are even open source implementations you can look at (Sourceforge has Excel Writer, for example).

森罗 2024-12-06 00:26:04

ExcelLibrary 非常棒。 http://code.google.com/p/excellibrary/

ExcelLibrary is excellent. http://code.google.com/p/excellibrary/

别把无礼当个性 2024-12-06 00:26:04

2020 年,https://github.com/ClosedXML/ClosedXML 上的 ClosedXML 库似乎非常有能力生成 Excel 文件。它基于 Microsoft 的 Open XML SDK (https://github.com/OfficeDev/Open -XML-SDK)。它不需要在计算机/服务器上安装 Excel 即可工作。显然,您可以配置它在 Web 服务器上生成 Excel 文件。它有一组非常好的对象,提供了相对简单的操作 Excel 文件的方法。

In 2020, the ClosedXML library at https://github.com/ClosedXML/ClosedXML seems pretty capable of generating Excel files. It is based on the Open XML SDK by Microsoft (https://github.com/OfficeDev/Open-XML-SDK). It does not require Excel to be installed on a computer/server to work. So apparently you can rig it to generate Excel files on a web server. It has a very nice set of objects that expose relatively easy means of manipulating Excel files.

短叹 2024-12-06 00:26:04

OLE 自动化是除剪贴板之外的应用程序通信的 Windows 标准方式。因为并非每种需要 OLE 的语言都有泛型(甚至是强静态类型),所以接口必然非常通用。

如果您只是拒绝使用 OLE,则可以使用 Windows 脚本主机来移动 GUI 本身并以这种方式获取数据,或者使用 WinAPI DLL 进行屏幕抓取。您还可以根据 XLS 和 XLSX 文件的文档标准编写或扫描 Excel 文件的 ETL。无论如何,我认为使用 OLE 的缺点可以通过以下极端优势得到缓解:您不必尝试以任何其他方式与 Excel 进行交互。

OLE automation is the Windows-standard way for apps to communicate, other than the Clipboard. Because not every language that would need OLE has generics (or even strong static typing), the interface is necessarily very general.

If you simply refuse to use OLE, you could use a Windows scripting host to move around the GUI itself and get your data that way, or screen-scrape with the WinAPI DLLs. You could also write or swipe an ETL for Excel files based on the document standards for XLS and XLSX files. Any way you slice it, I think the disadvantages of working with OLE are mitigated by the extreme advantage that you don't have to try to interact with Excel any other way.

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