在c#中读写Excel文件

时间:2012-10-29 06:29:35

标签: c# user-interface

在使用C#读取和编写Excel文件时,我有点困惑。我在我的应用程序中使用Interop.Excel.dll。

现在这在我的本地机器上工作正常。我已经安装了MS Excel。 但是,当我在未安装MS Excel的服务器上运行此应用程序时,它正在生成异常。

我是否需要安装MS Excel,虽然我引用了dll。

请指导我。

这是个例外。

===================START=====================
    10/27/2012 7:48:52 PM
    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 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
       at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)
       at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache)
       at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache)
       at System.Activator.CreateInstance(Type type, Boolean nonPublic)
       at ImpExpExcelFile.Form1.generateExcel(String loginId, String fileName, DataSet newValues)
    ====================END======================

谢谢:)

3 个答案:

答案 0 :(得分:0)

安装Excel是必需的。安装,然后重试。不用没有Excel的工作

答案 1 :(得分:0)

你需要安装MS Excel。 Interop.Excel.dll只是接口包装器。

答案 2 :(得分:0)

我在Expected Behavior工作,我们有一个名为DocRaptor的HTML to Excel API,它可以将HTML转换为Excel,而无需在服务器端安装Excel。

DocRaptor是一个Ruby on Rails应用程序,但您可以使用C#通过HTTP POST请求生成Excel文档。

这是DocRaptor主页的链接: DocRaptor

以及我们的C#示例代码的链接: DocRaptor C# Example