问题
I'm tried to convert .xlsx file to DataTable format by using NPOI 2.0 library. It's OK, but I have a problem with convert to string date cell. When I try to use construction like row.GetCell(j).ToString() - it's threw exception "Cannot get numeric value from a text cell". I tried to use DateCellValue property, but it also threw this exception. With other cell formats it's work good. Function, that I use it's:
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;
}
UPDATE: If I insert code like
row.GetCell(j).SetCellType(CellType.STRING);
in problem cell I have value like "36496.392581018517". Another cells converted correctly
回答1:
Second column of your excel file has a date format (12/2/1999
). This format is not recognized by NPOI in your current culture ("ru-RU"). This seems like a bug in NPOI, becuase when this happens, there is no way to read anything from that cell. The only way I came to, is to change the thread's culture before reading the excel file (and change it back after):
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;
}
}
回答2:
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;
}
}
回答3:
My solution is create function getValue like this
FileStream fs = new FileStream(@"\export.xlsx", FileMode.Open);
XSSFWorkbook wb = new XSSFWorkbook(fs);
ISheet sheet = wb.GetSheetAt(0);
// GetValue here
string text = getValueFromCell(sheet.GetRow(row).GetCell(1));
public string getValueFromCell(ICell cell) {
string output ="";
if (cell != null)
{
switch (cell.CellType)
{
case CellType.String:
output = cell.StringCellValue;
break;
case CellType.Numeric:
//dataRow[j] = cell.NumericCellValue;
output = cell.NumericCellValue.ToString();
break;
case CellType.Boolean:
output = cell.BooleanCellValue.ToString();
//dataRow[j] = cell.BooleanCellValue;
break;
default:
output = "";
break;
}
}
return output;
}
回答4:
This is correct code for 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, e. g. 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;
}
来源:https://stackoverflow.com/questions/15040567/c-xlsx-date-cell-import-to-datatable-by-npoi-2-0