C#。 xlsxdate单元格通过NPOI 2.0导入到DataTable

我试图通过使用NPOI 2.0库将.xlsx文件转换为DataTable格式。 没关系,但是我有转换为stringdate单元格的问题。 当我尝试使用像row.GetCell(j).ToString()这样的构造 – 它抛出exception“无法从文本单元格获取数值”。 我试图使用DateCellValue属性,但它也抛出了这个exception。 与其他单元格格式是很好的。 function,我使用它的:

private DataTable xlsxToDT(string fileName) { DataTable table = new DataTable(); XSSFWorkbook workbook = new XSSFWorkbook(new FileStream(fileName, FileMode.Open, FileAccess.Read)); ISheet sheet = workbook.GetSheetAt(0); IRow headerRow = sheet.GetRow(0); int cellCount = headerRow.LastCellNum; for (int i = headerRow.FirstCellNum; i < cellCount; i++) { DataColumn column = new DataColumn(headerRow.GetCell(i).StringCellValue); table.Columns.Add(column); } int rowCount = sheet.LastRowNum; for (int i = (sheet.FirstRowNum); i < sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); DataRow dataRow = table.NewRow(); for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { //EXCEPTION GENERATING IN THIS CODE dataRow[j] = row.GetCell(j).ToString(); //////////////////////////// } } table.Rows.Add(dataRow); } workbook = null; sheet = null; return table; } 

更新:如果我插入代码

 row.GetCell(j).SetCellType(CellType.STRING); 

在问题单元格中,我有像“36496.392581018517”的值。 另一个单元正确转换

你的Excel文件的第二列有一个date格式( 12/2/1999 )。 NPOI在您当前的文化(“RU-RU”)中不承认这种格式。 这似乎是NPOI中的一个错误,因为当这种情况发生时,无法从该单元中读取任何东西。 我唯一的方法是在读取excel文件之前改变线程的文化(并在之后改变它):

 private DataTable xlsxToDT(string fileName) { var prevCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; try { // Put your whole method body here. } finally { Thread.CurrentThread.CurrentCulture = prevCulture; } } 
 ICell cell = row.GetCell(j); if (cell != null) { switch (cell.CellType) { case CellType.String: dataRow[j] = cell.StringCellValue; break; case CellType.Numeric: dataRow[j] = cell.NumericCellValue; break; case CellType.Boolean: dataRow[j] = cell.BooleanCellValue; break; default: dataRow[j] = "ERROR"; break; } } 

这是xls的正确代码

 public DataTable GetDataTableFromExcelFile(string excel_file_Path) { HSSFWorkbook wb; NPOI.SS.UserModel.ISheet sh; string Sheet_name; using (var fs = new FileStream(excel_file_Path, FileMode.Open, FileAccess.Read)) { wb = new HSSFWorkbook(fs); Sheet_name = wb.GetSheetAt(0).SheetName; //get first sheet name } DataTable DT = new DataTable(); DT.Rows.Clear(); DT.Columns.Clear(); // get sheet sh = wb.GetSheet(Sheet_name); // add neccessary columns if (DT.Columns.Count < sh.GetRow(0).Cells.Count) { for (int j = 0; j < sh.GetRow(0).Cells.Count; j++) { DT.Columns.Add(Convert.ToString(sh.GetRow(0).Cells[j]), typeof(string)); } } // add row DT.Rows.Add(); int i = 1; while (sh.GetRow(i) != null) { // write row value for (int j = 0; j < sh.GetRow(i).Cells.Count; j++) { var cell = sh.GetRow(i).GetCell(j); if (cell != null) { // TODO: you can add more cell types capatibility, eg formula switch (cell.CellType) { case CellType.Numeric: DT.Rows[i][j] = sh.GetRow(i).GetCell(j).NumericCellValue; //dataGridView1[j, i].Value = sh.GetRow(i).GetCell(j).NumericCellValue; break; case CellType.String: DT.Rows[i-1][j] = sh.GetRow(i).GetCell(j).StringCellValue; break; } } } i++; } return DT; }