知道为什么我会收到错误 "Column number must be between 1 and 16384" 吗?

Any idea why I'm getting the error "Column number must be between 1 and 16384"?

我的代码试图完成的是将所有单元格从一个工作表复制到另一个工作表。这是简单的块

// find the index of the first empty row and column in the dumped file
int m = 1; while ( !wb.Worksheet(3).Cell(m,1).IsEmpty() ) ++m; 
int n = 1; while ( !wb.Worksheet(3).Cell(1,n).IsEmpty() ) ++n;
// copy from dumped file into raw data file
for (int i = 1; i < m; ++i )
{
    for (int j = 1; i < n; ++j)
    {
        wb.Worksheet(1).Cell(i,j).Value = wb.Worksheet(3).Cell(i,j).Value;
    }
}

不知何故这是抛出错误

Column number must be between 1 and 16384

知道为什么会这样吗?我没有看到任何无限循环或类似的东西。

这行代码发生了两件事。

for (int j = 1; i < n; ++j)
  1. 没有限制j;它只会在 i < n 期间不断增加。大概应该是:
    for (int j = 1; j < n; ++j)
  2. 这很可能是不相关的,但 ++j 使用 j 之前递增。在使用 j 之后,使用j++ 递增。您的代码可以写成
    for (int j = 2; j < n; j++)
    我不确定您是否希望第一次迭代以 j 作为 1 或 2 开始。

您似乎在循环直到找到空行或空列。如果它没有找到一个(似乎不太可能)那么它肯定会越过边界。

你在用什么?是互操作吗?您可以检查 Worksheet.UsedRange.Rows.CountWorksheet.UsedRange.Columns.Count。根据您要执行的操作,您可以只复制并粘贴 Worksheet.UsedRange 而不是一次复制一个单元格的值。