为什么我的 Console.WriteLine(", '{0}'", String); 总是出现 System.IndexOutOfRangeException陈述?
Why do I keep getting a System.IndexOutOfRangeException at my Console.WriteLine(", '{0}'", String); statement?
我正在 Visual Studio 中使用 C# 开发一个网络爬虫,它将 Excel 电子表格中的 URL 读取到一个字符串数组中,稍后将通过这些字符串访问网站以从中抓取信息.我现在正在调试它,但出于某种原因,每当我尝试将字符串打印到控制台时,都会出现超出范围的异常。
我正在使用 IronXL Excel 接口库来读写 from/to Excel 电子表格。并且异常不断发生在“Console.WriteLine(”, '{0}'”, String);”语句位于。
以下是涉及的class个变量:
public static int NUMBER_OF_ENTRIES = 90;
public static String [] URLs = new String [NUMBER_OF_ENTRIES];
public static String PATH_OF_IO_DOC = "C:\Users\Owner\Desktop\List of Clubs.xlsx";
public static String SHEET_NAME = "Sheet1";
这里是涉及到的方法的代码:
private void buttonGetURLs_Click(object sender, EventArgs e)
{
//read the URLs from the excel doc to an array of strings
WorkBook wb = WorkBook.Load(PATH_OF_IO_DOC);
WorkSheet ws = wb.GetWorkSheet(SHEET_NAME);
int rowCount = NUMBER_OF_ENTRIES;
//start at row 2 to skip the first header
for (int i = 2; i < rowCount; i++)
{
//skip header lines
if (!((rowCount == 13) || (rowCount == 16) || (rowCount == 31) || (rowCount == 32) || (rowCount == 33)))
{
//get value by cell address
//string address_val = ws["A" + rowCount].ToString();
//get value by row and column indexing
string index_val = ws.Rows[rowCount].Columns[1].ToString();
//read each cell's value to the array of URLs
URLs[rowCount] = index_val;
//check to make sure correct values are collected
Console.WriteLine(", '{0}'", index_val);
}
}
}
这是错误的行,因为 URL 将达到 rowCount-1
基于 0:
URLs[rowCount] = index_val;
您可能想要使用 i
索引
URLs[i] = index_val;
我正在 Visual Studio 中使用 C# 开发一个网络爬虫,它将 Excel 电子表格中的 URL 读取到一个字符串数组中,稍后将通过这些字符串访问网站以从中抓取信息.我现在正在调试它,但出于某种原因,每当我尝试将字符串打印到控制台时,都会出现超出范围的异常。
我正在使用 IronXL Excel 接口库来读写 from/to Excel 电子表格。并且异常不断发生在“Console.WriteLine(”, '{0}'”, String);”语句位于。
以下是涉及的class个变量:
public static int NUMBER_OF_ENTRIES = 90;
public static String [] URLs = new String [NUMBER_OF_ENTRIES];
public static String PATH_OF_IO_DOC = "C:\Users\Owner\Desktop\List of Clubs.xlsx";
public static String SHEET_NAME = "Sheet1";
这里是涉及到的方法的代码:
private void buttonGetURLs_Click(object sender, EventArgs e)
{
//read the URLs from the excel doc to an array of strings
WorkBook wb = WorkBook.Load(PATH_OF_IO_DOC);
WorkSheet ws = wb.GetWorkSheet(SHEET_NAME);
int rowCount = NUMBER_OF_ENTRIES;
//start at row 2 to skip the first header
for (int i = 2; i < rowCount; i++)
{
//skip header lines
if (!((rowCount == 13) || (rowCount == 16) || (rowCount == 31) || (rowCount == 32) || (rowCount == 33)))
{
//get value by cell address
//string address_val = ws["A" + rowCount].ToString();
//get value by row and column indexing
string index_val = ws.Rows[rowCount].Columns[1].ToString();
//read each cell's value to the array of URLs
URLs[rowCount] = index_val;
//check to make sure correct values are collected
Console.WriteLine(", '{0}'", index_val);
}
}
}
这是错误的行,因为 URL 将达到 rowCount-1
基于 0:
URLs[rowCount] = index_val;
您可能想要使用 i
索引
URLs[i] = index_val;