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

时间:2010-06-25 07:30:25

标签: excel c#-3.0

嗨,我正面临一个问题。

在我的服务器中,没有安装办公室。但是,我需要从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

服务器机器也可以使用相同的功能。但它没有安装办公室

但是当我运行该程序时,我得到了异常

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.

谷歌搜索没有提供太多支持。

此外,这是非常紧急的。

请帮助。

4 个答案:

答案 0 :(得分:2)

Office PI程序集简单地包装Office COM组件,以提供可从.NET托管代码调用的接口。您仍然需要安装Office,但是,您还有其他选择......

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

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

答案 1 :(得分:2)

如果你正在使用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:请求/感谢可能会有更多人提供帮助;)

答案 2 :(得分:1)

你基本上被塞满了。 interop文件只指向一个COM对象,它在安装时由Excel注册。

由于您没有Excel,因此其COM注册不会在注册表中,因此interop文件实际上指向一个断开的链接,因此您获得了COMException。

您需要安装Office才能使其正常运行。

答案 3 :(得分:0)

根据您的需要,NPOI可以完成工作(不需要安装办公室)。