从 Excel 电子表格中读取数据:错误 CS0021
Read data from Xcel spreadsheet: Error CS0021
为什么我会收到此错误消息?它抱怨的是我代码中的这一行:
website = x1range.Cells[i][1].value2;
我收到了
Error CS0021 Cannot apply indexing with [] to an expression of type 'object'
这是我的代码:
using System; using System.Threading; using OpenQA.Selenium; using
OpenQA.Selenium.Chrome; using excel =
Microsoft.Office.Interop.Excel;
namespace Create Editions
{
class Program
{
static void Main(string[] args)
{
excel.Application x1app = new excel.Application();
excel.Workbook x1workbook = x1app.Workbooks.Open(@"D:\DATA\CreateEditions.xlsx");
excel._Worksheet x1worksheet = (excel._Worksheet)x1workbook.Sheets[1];
excel.Range x1range = x1worksheet.UsedRange;
string website;
for(int i=1; i<=3; i++)
{
website = x1range.Cells[i][1].value2;
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(website);
}
}
}
}
x1range.Cells[i]
returns 一个 object
是 Range
索引器的 return 类型。所以请将x1range.Cells[i]
的类型转换为Range
。
Reference
website = (string)((Range)x1range.Cells[i])[1].value2;
为什么我会收到此错误消息?它抱怨的是我代码中的这一行:
website = x1range.Cells[i][1].value2;
我收到了
Error CS0021 Cannot apply indexing with [] to an expression of type 'object'
这是我的代码:
using System; using System.Threading; using OpenQA.Selenium; using
OpenQA.Selenium.Chrome; using excel =
Microsoft.Office.Interop.Excel;
namespace Create Editions
{
class Program
{
static void Main(string[] args)
{
excel.Application x1app = new excel.Application();
excel.Workbook x1workbook = x1app.Workbooks.Open(@"D:\DATA\CreateEditions.xlsx");
excel._Worksheet x1worksheet = (excel._Worksheet)x1workbook.Sheets[1];
excel.Range x1range = x1worksheet.UsedRange;
string website;
for(int i=1; i<=3; i++)
{
website = x1range.Cells[i][1].value2;
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(website);
}
}
}
}
x1range.Cells[i]
returns 一个 object
是 Range
索引器的 return 类型。所以请将x1range.Cells[i]
的类型转换为Range
。
Reference
website = (string)((Range)x1range.Cells[i])[1].value2;