1

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;
       }
    }
  }
 }
3
  • 2
    Can you please mention on which line are you getting the exception. Commented May 12, 2011 at 10:07
  • excelcells = excelworksheet.get_Range("A1", "A1"); Commented May 12, 2011 at 11:46
  • I am facing the exact same problem when I call WorkSheet.UsedRange Commented May 12, 2011 at 13:42

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

There are also free libraries for excel reading/writing: npoi.codeplex.com or epplus.codeplex.com
0

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 .

1 Comment

No it no matter how to use "a1" or index like"1,1"
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.