2010-06-25 89 views
1

嗨,我正面临一个问题。如何读取未安装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 

同样是在服务器计算机也可提供。但它并没有安装Office有

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

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. 

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

另外,它是非常紧急的。

请帮忙。

+6

“[URGENT]”增加了_nothing_对您的问题有用。事实上,它让我阅读_slower_。缺乏你的计划并不构成我们的紧急事件:-) – paxdiablo 2010-06-25 07:47:34

+0

删除了愚蠢的紧急部分,否则你一定得不到答案。 – leppie 2010-06-25 07:50:27

+0

请发送给我的代码,是紧急的 – Meff 2010-06-25 07:51:41

回答

1

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

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

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

2

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

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

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

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:有请询问/感谢很可能会得到更多人的帮助;)

+0

读取Excel表格的示例 – 2010-06-25 08:13:48

+0

http://epplus.codeplex.com/SourceControl/changeset/view/59882#885360 查看源代码存储库中的sample2.cs。 – Meff 2010-06-25 08:40:31

0

根据您的需求,NPOI可以做该工作(它不需要安装办公室)。

相关问题