导出到 Excel 时出现索引超出范围错误
Index was out of range error while exporting to Excel
我正在尝试使用以下代码将数据从 c# 导出到 excel:
enter worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView1.Rows.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count - 1; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers,
// adding a condition check.
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
我收到以下错误:
Index was out of range.Must be non negative and less than the size of the collection. Parameter name: index.
UPDATE 我通过更改 for 语句:[=14= 解决了这个问题]
for ( int i = -1; i < DataGridView1.Columns.Count; i++)
我认为问题在于许多在线指南和教程都表示当您数完 Lists<>
、Arrays
和 rows/columns
的 Table
时,您需要添加+1 因为所有这些对象容器的起始索引都是 0。
作为新手,一开始可能很难弄清楚必须在何处放置 +1,尤其是当您必须这样做时。也许您感到困惑,因为您希望将总行数作为 i
的最大定义。但是当你用 int i = 0
开始你的循环时(这是正确的,因为你不想跳过索引为 0 的行)你也从点 0 而不是 1 开始。所以没有必要添加 + 1 到最大断点,因为您仍然 dataGridView1.Rows.Count
次(<-- 循环执行的频率)通过行。
此异常 Index was out of range
告诉您您想要对超出范围的行执行某些操作。它超出范围,因为该行的项目不存在。假设你有 10 行,索引为 0 - 9。现在你从 0 开始遍历它们。所以在执行 10 次之后,你遍历了 0 - 9 行。因为 dataGridView1.Rows.Count
给你总数行数,在此示例中为 10。但是您将断点设置为 dataGridView1.Rows.Count + 1
,因此循环想要第 11 次使用索引为 10 的行执行您的任务,但最后一行的索引为 9。所以它找不到这一行,这就是它为您提供 Index out of range
执行时的情况。现在我希望你明白出了什么问题以及为什么。
尝试:
sheet.GetRow(rowNumber).CreateCell(columnNumber);
然后填写单元格值。
我正在尝试使用以下代码将数据从 c# 导出到 excel:
enter worksheet = workbook.ActiveSheet;
worksheet.Name = "ExportedFromDatGrid";
//Loop through each row and read value from each column.
for (int i = 0; i < dataGridView1.Rows.Count + 1; i++)
{
worksheet.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
}
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count - 1; j++)
{
// Excel index starts from 1,1. As first Row would have the Column headers,
// adding a condition check.
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
我收到以下错误:
Index was out of range.Must be non negative and less than the size of the collection. Parameter name: index.
UPDATE 我通过更改 for 语句:[=14= 解决了这个问题]
for ( int i = -1; i < DataGridView1.Columns.Count; i++)
我认为问题在于许多在线指南和教程都表示当您数完 Lists<>
、Arrays
和 rows/columns
的 Table
时,您需要添加+1 因为所有这些对象容器的起始索引都是 0。
作为新手,一开始可能很难弄清楚必须在何处放置 +1,尤其是当您必须这样做时。也许您感到困惑,因为您希望将总行数作为 i
的最大定义。但是当你用 int i = 0
开始你的循环时(这是正确的,因为你不想跳过索引为 0 的行)你也从点 0 而不是 1 开始。所以没有必要添加 + 1 到最大断点,因为您仍然 dataGridView1.Rows.Count
次(<-- 循环执行的频率)通过行。
此异常 Index was out of range
告诉您您想要对超出范围的行执行某些操作。它超出范围,因为该行的项目不存在。假设你有 10 行,索引为 0 - 9。现在你从 0 开始遍历它们。所以在执行 10 次之后,你遍历了 0 - 9 行。因为 dataGridView1.Rows.Count
给你总数行数,在此示例中为 10。但是您将断点设置为 dataGridView1.Rows.Count + 1
,因此循环想要第 11 次使用索引为 10 的行执行您的任务,但最后一行的索引为 9。所以它找不到这一行,这就是它为您提供 Index out of range
执行时的情况。现在我希望你明白出了什么问题以及为什么。
尝试:
sheet.GetRow(rowNumber).CreateCell(columnNumber);
然后填写单元格值。