任何想法,为什么我得到的错误“列号必须在1和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; } }
不知何故这是抛出错误
列号必须在1到16384之间
任何想法,为什么这可能是? 我没有看到任何无限循环或类似的东西。
这行代码有两件事。
for (int j = 1; i < n; ++j)
-
j
没有限制; 当i < n
时,它会持续增加。 它可能应该是:
for (int j = 1; j < n; ++j)
- 在使用
++j
之前,这很可能是不相关的,但是++j
递增的。 使用j++
之后,使用j++
来增加。 你的代码可以写成
for (int j = 2; j < n; j++)
我不确定是否要第一次迭代以j开头,1或2。
它看起来像循环,直到你find一个空的行或列。 如果它找不到一个(似乎不太可能),那么它肯定会超越界限。
你在用什么? 它是互操作的吗? 你可以检查Worksheet.UsedRange.Rows.Count
和Worksheet.UsedRange.Columns.Count
。 根据你想要做什么,你可能只是复制和粘贴Worksheet.UsedRange
而不是一次复制一个单元格的值。