简体   繁体   English

从C#导出到Excel

[英]Export to Excel from C#

I am trying to write data to Excel from c#, but while debugging I have an exception: 我正在尝试从C#将数据写入Excel,但是在调试时出现异常:

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Excel.Range'. 无法将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ Microsoft.Office.Interop.Excel.Range”。 This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020846-0000-0000-C000-000000000046}' failed due to the following error: Интерфейс не поддерживается (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). 此操作失败是因为对IID为“ {00020846-0000-0000-C000-000000000046}”的COM组件上的QueryInterface调用由于以下错误而失败:Интерфейснеподдерживается(来自HRESULT的异常:0x80004002(E_NOINTERFACE))。

Code : 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication3
{
   class Program
  {
    static void Main(string[] args)
    {
        Excel.Application excelapp;
        Excel.Window excelWindow;
        Excel.Workbooks excelappworkbooks;
        Excel.Workbook excelappworkbook;
        Excel.Sheets excelsheets;
        Excel.Worksheet excelworksheet;
        Excel.Range excelcells;
        excelapp = new Excel.Application();
        excelapp.Visible = true;

        excelappworkbooks = excelapp.Workbooks;

        excelappworkbook = excelapp.Workbooks.Open(@"C:\Documents and Settings\дима\Мои документы\Visual Studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\bin\Debug\a.xls",
                           Type.Missing, Type.Missing, Type.Missing,
         "WWWWW", "WWWWW", Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing, Type.Missing, Type.Missing,
          Type.Missing, Type.Missing);

        excelsheets = excelappworkbook.Worksheets;

        excelworksheet = (Excel.Worksheet)excelsheets.get_Item(1);
        excelcells = excelworksheet.get_Range("A1", "A1");

        excelcells.Value2 = 10.5;
       }
    }
  }
 }

looks like a type library conflict. 看起来像类型库冲突。 maybe you have multiple versions installed. 也许您安装了多个版本。 please check you have a reference to the type lib you want 请检查您是否对所需的lib类型有引用

9.0 Excel 2000 9.0 Excel 2000

10.0 Excel 2002 10.0 Excel 2002中

11.0 Excel 2003 11.0 Excel 2003

12.0 Excel 2007 12.0 Excel 2007

14.0 Excel 2010 14.0 Excel 2010

after reference change, clean solution and do a complete recompile. 参考更改后,请清洁溶液并进行完全重新编译。

This is not really answering the question directly, but I have seen many people struggle with controlling Excel via the COM interface. 这并不是真正地直接回答问题,但是我已经看到很多人都在努力通过COM接口控制Excel。

I got a library called FlexCel from TMS Software which lets you create or manipulate Excel worksheets entirely in code, without even needing Excel installed on the machine. 我从TMS Software获得了一个名为FlexCel的库,它使您可以完全用代码创建或操作Excel工作表,甚至不需要在计算机上安装Excel。

It has lots of other useful features as well (writing PDF files, doing complex reports from Excel templates, etc., etc.) 它还具有许多其他有用的功能(编写PDF文件,从Excel模板执行复杂的报告等)。

I should point out that I have no connection with the company, other than being a satisfied user. 我应该指出,除了成为满意的用户外,我与公司没有任何联系。

Can you please try the following and let me know. 您可以尝试以下方法,让我知道。 The following code should work in C# 3.0 以下代码应在C#3.0中运行

excelcells = excelworksheet.get_Range("A1", Type.Missing);

If you are using C# 4.0 , use the following 如果您使用的是C#4.0,请使用以下命令

excelcells = excelworksheet.get_Range("A1");

Also always write code with a try / catch block . 也要始终使用try / catch块编写代码。

Try this: Use Add Remove programs> Go to the tab Change or remove programs Go to Microsoft Office Professional 2003. Click the link "Click here for support information". 尝试以下操作:使用添加删除程序>转到选项卡更改或删除程序转到Microsoft Office Professional2003。单击链接“单击此处以获取支持信息”。 Use Repair Button. 使用修复按钮。

This worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM