在.NET中从Excel和Word文件读取最简单的方法

这是真正的自我教育。 但是,有没有更简单的方法来从Excel表读取,而不使用COM / INTEROP DLL? 任何其他方式微软可以更容易地从单元格读取Excel表单元格中的值(以及Word中的Word )?

对于Word和Excel,一个好的第三方库是Aspose http://www.aspose.com/.net/total-component.aspx Aspose提供了一个非常易于使用的API,其中包含大量的选项来操纵word和excel文档。 文档也很好。 这就是说许可费用可能是过高的。

如果你想要Word的开源替代品,那就是DocX http://docx.codeplex.com/ DocX提供了一个比OpenXML更好的API。

我们已经在非官方的分支中对DocX进行了一些主要的扩展,您可能会觉得有用: https : //github.com/organizations/Word-DocX

扩展名列表包括对单层和多层列表的支持,支持读取字体格式,分区/分区等。

我知道这个问题问是否有替代Excel和Word互操作API – 但是当我读它,这是因为OP想要一个更简单的方法,例如,从Excel工作表读取单元格或读取单词一个Word文档。

我只是想补充说,对于Word来说,阅读文档中的每个单词可以用很less的几行完成:

 foreach (var word in myDocument.Words) { Console.WriteLine(word); } 

…我不相信这可以做得更简单,所以我想知道它是否真的是OP所需要的替代API,或者可能是一些Word互操作示例呢? (至less在使用Word interop API的情况下)

如果你是关于新格式(xlmx,docx),我想你可以使用OpenXML 。

如果您想使用ODBC并读取WorkSheet作为示例,请遵循以下简单示例,这只是从Excel读取多种不同方式之一

  OleDbCommand myCommand = new OleDbCommand("Select * from [Sheet1$];"); OleDbConnection myConnection = new OleDbConnection(connectionString); myConnection.Open(); myCommand.Connection = myConnection; OleDbDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { // it can read upto 5 columns means A to E. In your case if the requirement is different then change the loop limits a/c to it. //Change the Response.Write blow to Console.WriteLine() if you are wanting to test this in a Console application vs a web app. for (int i = 0; i < 5; i++) { Response.Write(myReaderIdea.ToString() + " "); } Response.Write("<br>"); } myConnection.Close(); //make sure to Dispose of the Reader object as well ((IDisposable)myReaderIdea).Dispose() 

有一个ODBC数据源的Excel驱动程序。 它可以通过控制面板访问。 如果您在那里configuration,或者使用连接string与excel驱动程序,您可以通过ADO API访问它。