excel interop alternative

送分小仙女□ 提交于 2019-12-20 20:40:13

问题


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.


回答1:


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




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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




回答6:


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.



来源:https://stackoverflow.com/questions/7002256/excel-interop-alternative

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!