0

I'm trying to import excel file to list of class (CLA). Excel sheet is like a table, first row has names of objects, first and second column have also specific names. Each cell has corresponding 3 names and one value.

And if the cell is null, it will be skipped to List. But there is NullException occurs in grouping.

 public class ImportFile
    {
        public HSSFWorkbook Workbook;
        public ISheet Sheet;
        public List<List<Cla>> ImportResult;

        public ImportFile(string a)
        {
            try
            {
                using (FileStream file = new FileStream(a, FileMode.Open, FileAccess.Read))
                {
                    Workbook = new HSSFWorkbook(file);
                }

                string sname = Workbook.GetSheetName(0);
                Sheet = Workbook.GetSheet(sname);

                int z = Sheet.PhysicalNumberOfRows;  // RowNum - 1 : 0-s, Z-2 
                int x = Sheet.GetRow(0).PhysicalNumberOfCells; //ColNum -1 : 0-s, X-3

                List<Cla> Clsit = new List<Cla>();

                for (int i = 2; i <= x - 1; i++)
                {
                    for (int j = 1; j <= z - 1; j++)
                    {
                        if (ClMaker(i, j, Sheet) != null)
                        {
                            Clsit.Add(ClMaker(j, i, Sheet));
                        }
                    }
                }

                ImportResult = Clsit.GroupBy(item => new { item.PriznakName, item.ClassNumber }).Select(group => group.ToList()).ToList(); //right here
            }


            catch (IOException)
            {
                MessageBox.Show("File is using");
            }

        }

        public Cla ClMaker(int a, int b, ISheet c)
        {
                        string name;
                        double value;
                        string pname;
                        string cln;
                        Cla Cl;
            if(c.GetRow(a).GetCell(b) == null)
            {
                Cl = null;
                return Cl;
            }
            else
            {
                value = c.GetRow(a).GetCell(b).NumericCellValue;
                name = c.GetRow(a).GetCell(0).StringCellValue;
                pname = c.GetRow(0).GetCell(b).StringCellValue;
                cln = c.GetRow(a).GetCell(1).NumericCellValue.ToString();
                Cl = new Cla(name, value, pname, cln);
                return Cl;
            }


        }
    }
ldn
  • 147
  • 1
  • 9

0 Answers0