分配Excel范围时出现HRESULT错误?
我正在写一个基本的程序来使用C#来操纵Excel,而且我在分配范围时遇到了困难。 这是我的代码:
using Excel = Microsoft.Office.Interop.Excel; //...various other using statements static void Main(string[] args) { Excel.Application xlApp = new Excel.Application(); xlApp.Visible = true; Excel.Workbook bk=xlApp.Workbooks.Add(); Excel.Worksheet sht = bk.Sheets["Sheet1"]; Excel.Range rng; for (int c = 0; c < 10;c++ ) { rng=sht.Cells[c,1]; rng.Value= "aaa"; } }
我不断收到以下错误rng=sht.Cells[c,1];
线:
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll Additional information: Exception from HRESULT: 0x800A03EC
XL的第一个索引将在XL不喜欢的外观开始时为0。 从1开始。
解决了; 问题是Excel使用基于1的索引,并且我的For循环从0开始。