如何读取未安装office的excel文件(C#3.0,dotnet 3.5)

嗨,我正面临一个问题。

在我的服务器上,没有安装办公室。 但是,我需要从Excel文件中访问数据。

我使用了Microsoft.Office.Interop.Excel dll文件。 我的印象是,这将工作

因为dll的位置是

C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\Microsoft.Office.Interop.Excel.dll 

服务器机器也是一样的。 但是它没有安装办公室

但是当我运行程序时,我得到了exception

 System Exception:System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80040154. 

谷歌search没有提供太多的支持。

此外,这是非常紧急的。

请帮助。

Office PI程序集仅包装Office COM组件,以提供可从.NET托pipe代码调用的接口。 您仍然需要安装Office,但是,您有其他选项…

如果您使用的是Office 2007文件,则可以尝试Open Office SDK 2.0 for Microsoft Office 。

或者,如果您正在使用较早版本的Office的文件,则可以使用第三方库,例如SpreadsheetGear 。

如果您使用的是Excel&C#,请尝试http://epplus.codeplex.com/

免费的图书馆,可以读/写电子表格,非常容易使用Excel。

按照OP要求在Excel文件中读取的示例:

 FileInfo existingFile = new FileInfo(FilePath); using (ExcelPackage package = new ExcelPackage(existingFile)) { // get the first worksheet in the workbook ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int col = 2; //The item description // output the data in column 2 for (int row = 2; row < 5; row++) Console.WriteLine("\tCell({0},{1}).Value={2}", row, col, worksheet.Cells[row, col].Value); // output the formula in row 5 Console.WriteLine("\tCell({0},{1}).Formula={2}", 3, 5, worksheet.Cells[3, 5].Formula); Console.WriteLine("\tCell({0},{1}).FormulaR1C1={2}", 3, 5, worksheet.Cells[3, 5].FormulaR1C1); } // the using statement automatically calls Dispose() which closes the package. 

PS:请求/谢谢可能会有更多的人来帮忙;)

你基本上是塞满的。 互操作文件只是指向安装时由Excel注册的COM对象。

由于您没有Excel,因此它的COM注册将不在registry中,因此互操作文件有效地指向断开的链接,因此您将得到COMException。

您需要安装Office才能使其工作。

根据您的需要, NPOI可能会做这项工作(不需要安装办公室)。