无法找到列 7 程序遍历超过Excel列数

 

代码遍历到8列,实际Excel少于8列,会报错:无法找到列 7

判断列数

int columns = dtSource.Columns.Count;

获取当前Excel总列数

                        
              int columns = dtSource.Columns.Count;
              //防止不含推广价此列 报错提示:无法找到列 7 decimal? MaxPrice = null; if (columns==8) { //推广价 string maxPriceStr = dtSource.Rows[i][7].ToString(); //如果当前列的单元格报错,也会转类型错误 if (!string.IsNullOrEmpty(maxPriceStr)) { MaxPrice = Convert.ToDecimal(maxPriceStr); } }

 

posted on 2020-04-28 17:16  张彦山  阅读(379)  评论(0编辑  收藏  举报