1. 程式人生 > >NPOI 讀取Excel 表資料 資料裡面帶日期時的處理方法

NPOI 讀取Excel 表資料 資料裡面帶日期時的處理方法

將ExcelToDataTable 方法下的 

//if (row.GetCell(j) != null) //同理,沒有資料的單元格都預設是null
// dataRow[j] = row.GetCell(j).ToString(); 

替換為

    if (row.GetCell(j) != null)
                            {
                                ICell cell = row.GetCell(j);
                                //Cell為非NUMERIC時,呼叫IsCellDateFormatted方法會報錯,所以先要進行型別判斷
if (cell.CellType == CellType.Numeric && DateUtil.IsCellDateFormatted(cell)) dataRow[j] = cell.DateCellValue.ToString("yyyy/MM/dd"); else { dataRow[j]
= row.GetCell(j).ToString(); } }