C # npoi solution introduced into the intermediate cell is not activated, the corresponding sequence of chaotic datatable problem. Nopi cell or intermediate null null null null problem can not be read subsequent

Scene: an import function to do today, universal npoi import code, are the first ready for a datatable, draw the title bar, and then traverse excel, cycling assignment.

Under normal circumstances, if the value of the table there, or cells may also be the empty string, i.e., all cells are active. Read normally

Common code like this:

  sheet.LastRowNum the rowCount = int; 
                    for (int I = the startRow; I <= the rowCount; I ++) 
                    { 
                        the IRow Row = sheet.GetRow (I); 
                        IF (Row row.Cells.Count == 0 == null || ) continue; // default is no data row null       

                        the DataRow DataRow = data.NewRow (); 

                        for (int J = row.FirstCellNum; J <CellCount; J ++) 
                        { 
                            IF (row.GetCell (J)! = null ) // Similarly, no data cell is by default null 
                                DataRow [J] = row.Cells [J]; 
                            // to Convert.ToDateTime (); 
                        } 

                        data.Rows.Add (DataRow); 
                    }
                }
 

(! Row.GetCell (j) = null) However, if the intermediate code is not activated if there is on the wrong phrase, because if 24, but the middle two inactive cells that sheet.GetRow (i);

Only 22, so the code to run this wrong, and it will result table does not correspond to the assignment.
Solutions are as follows:

 
  Reference numeral // last column of 
                    the rowCount int = sheet.LastRowNum; 
                    for (int I = the startRow; I <= the rowCount; I ++) 
                    { 
                        the IRow Row = sheet.GetRow (I);   
                        IF (== null || Row Row. Cells.Count == 0) continue; // default is no data row null       

                        the DataRow DataRow = data.NewRow (); 
                        for (int J = row.FirstCellNum; J <CellCount; J ++) 
                        { 
                            the try 
                            {  
                                // prevent middle cell with inactive row.GetCell (j)! = null null last valid determination, do not know why, with so try
                                int = the ColumnIndex row.Cells [j] .ColumnIndex; // get the real index of the column, the column to prevent confusion, error or correspondence datatable column 
                                the DataRow [ColumnIndex] = row.Cells [J]; 
                                // IF (row.GetCell (J !) = null) // Similarly, no data cell is by default null 
                                // DataRow [J] = row.Cells [J]; 
                            } 
                            the catch 
                            {} 
                            // to Convert.ToDateTime (); 
                        } 

                        data.Rows. the Add (DataRow); 
                    }

  

Guess you like

Origin www.cnblogs.com/efreer/p/12100539.html