【发布时间】:2018-05-15 14:52:02
【问题描述】:
我正在尝试从 excel 文件中填充整数数组,但出现此异常;“无法将类型 object[,] 隐式转换为 int [,]”
我尝试了 (int),但出现“无法将类型 int 隐式转换为 int[,]”错误。 我得到最后两行的错误。
string path = "";
_Application excel = new _Excel.Application();
Workbook wrkbk;
Worksheet wrksht;
int xlRow;
int xlCol;
public int[,] inputs = new int[9,683];
public int[,] outputs = new int[1,683];
public Excel(string path, int sheet)
{
this.path = path;
wrkbk = excel.Workbooks.Open(path);
wrksht = excel.Worksheets[sheet];
xlRow = wrksht.UsedRange.Rows.Count;
xlCol = wrksht.UsedRange.Columns.Count;
inputs = wrksht.Range[wrksht.Cells[1][1], wrksht.Cells[xlRow][xlCol - 1]].Cells.Value2;
outputs = wrksht.Range[wrksht.Cells[1][xlCol], wrksht.Cells[xlRow][xlCol]].Cells.Value2;
【问题讨论】:
-
错误信息会告诉你到底出了什么问题。您正在尝试将 object[,] 放入 int[,] 中,并且正如您在消息中所说的那样,这不会隐式起作用。您必须将整个数组转换为 int[,]
-
你能指定我该怎么做吗?我查看了网站进行转换,但所有这些都是列表或数组列表。
-
for 循环、列表或数组列表有什么问题?
标签: c# excel visual-studio