C# 将excel范围写入整数数组类型转换异常
我试图从excel文件中填充整数数组,但出现了此异常;无法将类型对象[,]隐式转换为int[,] 我尝试了(int),但得到了“无法将int类型隐式转换为int[,]”错误。 我得到了最后两行的错误C# 将excel范围写入整数数组类型转换异常,c#,excel,visual-studio,C#,Excel,Visual Studio,我试图从excel文件中填充整数数组,但出现了此异常;无法将类型对象[,]隐式转换为int[,] 我尝试了(int),但得到了“无法将int类型隐式转换为int[,]”错误。 我得到了最后两行的错误 string path = ""; _Application excel = new _Excel.Application(); Workbook wrkbk; Worksheet wrksht; int xlRow; int xlCol; public int[,] inputs = new in
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;
编辑您需要下载的内容。可以通过NuGet软件包管理器安装此库。它很容易使用
我使用它的数据:
输出(即使用excel数据创建的二维数组的可视化):
使用OfficeOpenXml;
使用System.Collections.Generic;
使用System.IO;
命名空间等于
{
班级计划
{
公共静态void Main()
{
尝试
{
FileInfo newfile=newfileinfo(@“C:\Users\Evan\Desktop\new.xlsx”);
ExcelPackage pkg=新的ExcelPackage(新文件);
Excel工作表wrksheet=pkg.Workbook.Worksheets[0];
var lastRow=wrksheet.Dimension.End.Row;
ExcelRange rng=wrksheet.Cells[1,1,lastRow,2];
系统控制台写入线(注册地址);
列表值范围=新列表();
int-cell值;
foreach(rng中的var单元)
{
cellValue=System.Convert.ToInt32(cell.Value);
如果(cellValue!=0)
{
值范围。添加(单元格值);
}
}
int countOfList=valuesInRange.Count;
int[,]数组=新的int[countOfList/2,2];
数组=从列表(countOfList,valuesInRange)中填充2dArrayFromList;
foreach(数组中的变量项)
{
系统控制台写入线(项目);
}
}
捕获(System.IO.IOException错误)
{
System.Console.WriteLine(错误消息);
}
}
公共静态int[,]填充2darrayFromList(int countOfList,List List)
{
int[,]数组=新的int[countOfList/2,2];
int listNum=0;
for(int-rownum=0;rownum
错误消息会准确地告诉您出了什么问题。您试图将一个对象[,]放入和int[,]中,但正如消息中所说的那样,它不能隐式工作。您必须将整个数组转换为和int[,]您可以指定如何进行转换吗?我查看了网站进行转换,但都是针对列表或arraylist的。for循环、列表或arraylist有什么问题?我特别需要一个2d数组作为神经网络的输入层,我认为列表不能解决我的问题(至少我不知道它是如何解决的,我在面向对象方面比较新),我添加了一个函数,从列表中填充2darrayFromList,这将很好地使用excel range中的列表数据填充二维数组。nuget在版本方面给了我很大的困难,但我最终成功了,我真诚的感谢。修复了一点,包括到库的链接。很高兴它有帮助!
using OfficeOpenXml;
using System.Collections.Generic;
using System.IO;
namespace equals
{
class Program
{
public static void Main()
{
try
{
FileInfo newfile = new FileInfo(@"C:\Users\Evan\Desktop\new.xlsx");
ExcelPackage pkg = new ExcelPackage(newfile);
ExcelWorksheet wrksheet = pkg.Workbook.Worksheets[0];
var lastRow = wrksheet.Dimension.End.Row;
ExcelRange rng = wrksheet.Cells[1, 1, lastRow, 2];
System.Console.WriteLine(rng.Address);
List<int> valuesInRange = new List<int>();
int cellValue;
foreach (var cell in rng)
{
cellValue = System.Convert.ToInt32(cell.Value);
if (cellValue != 0)
{
valuesInRange.Add(cellValue);
}
}
int countOfList = valuesInRange.Count;
int[,] array = new int[countOfList/2, 2];
array = Populate2DArrayFromList(countOfList, valuesInRange);
foreach (var item in array)
{
System.Console.WriteLine(item);
}
}
catch (System.IO.IOException err)
{
System.Console.WriteLine(err.Message);
}
}
public static int[,] Populate2DArrayFromList(int countOfList, List<int> list)
{
int[,] array = new int[countOfList/2, 2];
int listNum = 0;
for(int rownum = 0; rownum < countOfList/2; rownum++)
{
array[rownum, 0] = list[listNum];
System.Console.WriteLine("added to column A {0}", list[listNum]);
listNum = listNum + 2;
}
listNum = 1;
for (int rownum = 0; rownum < countOfList/2; rownum++)
{
array[rownum, 1] = list[listNum];
System.Console.WriteLine("added to column B {0}", list[listNum]);
listNum = listNum + 2;
}
return array;
}
}
}