将excel范围写入整数数组类型转换异常

时间:2018-05-15 14:52:02

标签: c# excel visual-studio

我正在尝试从excel文件填充一个整数数组但是得到这个异常;"不能隐式地将类型对象[]转换为int [ ]"

我试过(int)并且我得到了#34;不能隐式地将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;

1 个答案:

答案 0 :(得分:0)

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

我用过的数据:

输出(使用Excel数据创建的2d数组的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;
        }

    }
}