0
我使用NPOI Excel的庫生成一個Excel文件,在Excel文件我正在顯式定義列類型,如日期列,字符串等NPOI集顯柱類型,使用不正常
林下面的代碼來實現這一點。
var row = sheet.CreateRow(currentNPOIRowIndex++);
for (var colIndex = 0; colIndex < exportData.Columns.Count; colIndex++)
{
ICell cell = null;
cell = row.CreateCell(colIndex);
if (exportData.Columns[colIndex].DataType == typeof(DateTime))
{
if (exportData.Rows[rowIndex][colIndex].ToString() != "")
{
cell.SetCellValue((DateTime)exportData.Rows[rowIndex][colIndex]);
cell.CellStyle = (NPOI.HSSF.UserModel.HSSFCellStyle)book.CreateCellStyle();
cell.CellStyle.DataFormat = book.CreateDataFormat().GetFormat("yyyyMMdd HH:mm:ss");
cell = null;
}
else
cell.SetCellValue(exportData.Rows[rowIndex][colIndex].ToString());
}
else
cell.SetCellValue(exportData.Rows[rowIndex][colIndex].ToString());
}
}
上面的代碼工作正常,42行,即它正確地設置列類型,但經過42行列類型不適用。
任何幫助將不勝感激。
你可以試試最新的代碼。我已經爲您提供了可行的解決方案。謝謝。 –