从 C# 导出到 Excel

发布于 2024-11-06 17:06:22 字数 1664 浏览 2 评论 0原文

我正在尝试从 C# 将数据写入 Excel,但在调试时出现异常:

无法将“System.__ComObject”类型的 COM 对象转换为接口类型 'Microsoft.Office.Interop.Excel.Range'。 此操作失败,因为 COM 上的 QueryInterface 调用 IID 接口组件 '{00020846-0000-0000-C000-000000000046}' 由于以下错误而失败: Интерфейс не поддерживается (例外 来自 HRESULT:0x80004002 (E_NOINTERFACE))。

代码:

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;
       }
    }
  }
 }

I am trying to write data to Excel from c#, but while debugging I have an exception:

Unable to cast COM object of type 'System.__ComObject' to interface type
'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)).

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;
       }
    }
  }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

盗琴音 2024-11-13 17:06:23

试试这个:
使用“添加删除程序”>转到“更改或删除程序”选项卡
转到 Microsoft Office Professional 2003。单击链接“单击此处获取支持信息”。使用修复按钮。

这对我有用。

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". Use Repair Button.

This worked for me.

城歌 2024-11-13 17:06:22

看起来像是类型库冲突。
也许您安装了多个版本。
请检查您是否有对所需类型库的引用

9.0 Excel 2000

10.0 Excel 2002

11.0 Excel 2003

12.0 Excel 2007

14.0 Excel 2010

引用更改后,清理解决方案并进行完整的重新编译。

looks like a type library conflict.
maybe you have multiple versions installed.
please check you have a reference to the type lib you want

9.0 Excel 2000

10.0 Excel 2002

11.0 Excel 2003

12.0 Excel 2007

14.0 Excel 2010

after reference change, clean solution and do a complete recompile.

倾城花音 2024-11-13 17:06:22

这并没有真正直接回答问题,但我看到很多人在通过 COM 接口控制 Excel 时遇到困难。

我从 TMS Software 获得了一个名为 FlexCel 的库,它可以让您完全在其中创建或操作 Excel 工作表代码,甚至不需要在机器上安装 Excel。

它还有许多其他有用的功能(编写 PDF 文件、从 Excel 模板制作复杂的报告等)。

我应该指出,除了作为一个满意的用户之外,我与该公司没有任何联系。

This is not really answering the question directly, but I have seen many people struggle with controlling Excel via the COM interface.

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.

It has lots of other useful features as well (writing PDF files, doing complex reports from Excel templates, etc., etc.)

I should point out that I have no connection with the company, other than being a satisfied user.

唐婉 2024-11-13 17:06:22

您可以尝试以下操作并告诉我吗?以下代码应在 C# 3.0 中运行

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

如果您使用的是 C# 4.0,请使用以下代码

excelcells = excelworksheet.get_Range("A1");

同时始终使用 try/catch 块编写代码。

Can you please try the following and let me know. The following code should work in C# 3.0

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

If you are using C# 4.0 , use the following

excelcells = excelworksheet.get_Range("A1");

Also always write code with a try / catch block .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文