我正在尝试从 c# 将数据写入 Excel,但在调试时出现异常:
无法将“System.__ComObject”类型的 COM 对象转换为接口类型“Microsoft.Office.Interop.Excel.Range”。此操作失败,因为 IID 为“{00020846-0000-0000-C000-000000000046}”的接口的 COM 组件上的 QueryInterface 调用因以下错误而失败:Интерфейс не поддерживается(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;
}
}
}
}