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