【问题标题】:Getting HRESULT error when assigning Excel range?分配 Excel 范围时出现 HRESULT 错误?
【发布时间】:2014-10-08 01:05:47
【问题描述】:

我正在编写一个使用 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

【问题讨论】:

  • 尝试用谷歌搜索该异常这里是一个 SO 以前的帖子,您也可以查看 stackoverflow.com/questions/891394/…\ Basically you are expecting Excel to find something that does not Exist 与 Index out of Bounds 或多或少相同...

标签: c# excel office-interop


【解决方案1】:

Cells 的第一个索引将在 XL 不喜欢的外观开始时为 0。从 1 开始。

【讨论】:

    【解决方案2】:

    解决了;问题是 Excel 使用基于 1 的索引,而我的 For 循环从 0 开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 1970-01-01
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      相关资源
      最近更新 更多