| | |
| | | return table.Rows.Count; |
| | | } |
| | | #endregion |
| | | |
| | | |
| | | #region |
| | | public static List<ExcelErro> InportExcelToTableListErro(List<DataTable> excelTable) |
| | | { |
| | | List<ExcelErro> list = new List<ExcelErro>(); |
| | | for (int i = 0; i < excelTable.Count; i++) |
| | | { |
| | | var emptyColumn = excelTable[i].Columns.Cast<DataColumn>().FirstOrDefault( |
| | | column => column.ColumnName.Contains("*") && excelTable[i].AsEnumerable().Any(row => row.IsNull(column)) |
| | | ); |
| | | |
| | | if (emptyColumn != null) |
| | | { |
| | | int columnIndex = excelTable[i].Columns.IndexOf(emptyColumn); |
| | | int rowIndex = excelTable[i].AsEnumerable().ToList().FindIndex(row => row.IsNull(emptyColumn)); |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = (rowIndex + 1).ToString(); |
| | | erro.ErrorField = emptyColumn.ColumnName; |
| | | erro.ErrorCont = "模板表头带*的列中存在空值,第" + (rowIndex + 1).ToString() + "行,必填字段:"+ emptyColumn.ColumnName +"为空"; |
| | | list.Add(erro); |
| | | } |
| | | var duplicateColumn = excelTable[i].Columns.Cast<DataColumn>().FirstOrDefault( |
| | | column => column.ColumnName.Contains("唯一") && excelTable[i].AsEnumerable().GroupBy(row => row[column]).Any(group => group.Count() > 1)); |
| | | if (duplicateColumn != null) |
| | | { |
| | | int columnIndex = excelTable[i].Columns.IndexOf(duplicateColumn); |
| | | var duplicateRows = excelTable[i].AsEnumerable() |
| | | .Where(row => row[duplicateColumn] != DBNull.Value) |
| | | .GroupBy(row => row[duplicateColumn]) |
| | | .Where(group => group.Count() > 1) |
| | | .SelectMany(group => group.ToList()) |
| | | .ToList(); |
| | | foreach (var row in duplicateRows) |
| | | { |
| | | ExcelErro erro = new ExcelErro(); |
| | | erro.RoeNumber = excelTable[i].Rows.IndexOf(row).ToString(); |
| | | erro.ErrorField = emptyColumn.ColumnName; |
| | | erro.ErrorCont = "模板表头带(唯一)的列中存在重复值,第" + excelTable[i].Rows.IndexOf(row).ToString() + "行,必填字段:" + duplicateColumn.ColumnName + "重复"; |
| | | list.Add(erro); |
| | | |
| | | } |
| | | } |
| | | } |
| | | return list; |
| | | } |
| | | #endregion |
| | | } |
| | | } |