【问题标题】:writing excel range to an integer array type casting exception将excel范围写入整数数组类型转换异常
【发布时间】: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


【解决方案1】:

编辑您需要下载EPPLUS。这个库可以通过 NuGet 包管理器安装。它非常易于使用。

我使用它的数据:

输出(使用 excel 数据创建的二维数组的 AKA 可视化):

 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;
        }

    }
}

【讨论】:

  • 我特别需要一个二维数组作为神经网络的输入层,我认为 List 不会解决我的问题(至少我不知道它会如何,我在 Object 方面还很新面向)
  • 我添加了一个函数 populate2darrayfromList,它可以很好地用来自 excel 范围的列表数据填充一个二维数组。
  • nuget 让我在版本问题上遇到了困难,但我终于成功了,我真诚的感谢。
  • 对其进行了一些修复,包括指向图书馆的链接。很高兴它有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多