C# NPOI导出日期时间问题

C# NPOI导出日期时间问题,c#,npoi,C#,Npoi,我用NPOI进行了导出。 我设置单元格值: row.CreateCell(6).SetCellValue("This is date string"); 现在,excel将其视为一个文本字段,我需要excel将单元格类型设置为DateTime 这可能吗? 我试过: row.CreateCell(6).SetCellValue(DateTime.Parse("This is date string")); 但不起作用。私有无效添加记录(工作表、IList记录) private void Add

我用NPOI进行了导出。
我设置单元格值:

row.CreateCell(6).SetCellValue("This is date string");
现在,excel将其视为一个文本字段,我需要excel将单元格类型设置为
DateTime

这可能吗?
我试过:

row.CreateCell(6).SetCellValue(DateTime.Parse("This is date string"));
但不起作用。

私有无效添加记录(工作表、IList记录)
private void AddRecords( Sheet sheet, IList<T> records )
{
    foreach( var record in records )
    {
        // append row
        var row = sheet.CreateRow ( sheet.LastRowNum + 1 );

        // iterate through all configured columns
        foreach ( var column in GetColumns() )
        {
            // append cell
            Cell cell = row.CreateCell ( row.LastCellNum == -1 ? 0 : row.LastCellNum );

            object value = GetCellValue ( column, record );
            cell.SetCellValue ( value );
            string dataFormat = column.DataFormat ??"m/d";
            cell.CellStyle = GetCellStyleForFormat( sheet.Workbook, dataFormat );
        }
    }
}

private readonly Dictionary<string, CellStyle> _cellStyleCache = new Dictionary < string, CellStyle > ();

private CellStyle GetCellStyleForFormat( Workbook workbook, string dataFormat )
{
    if( !_cellStyleCache.ContainsKey ( dataFormat ) )
    {
        var style = workbook.CreateCellStyle ();

        // check if this is a built-in format
        var builtinFormatId = HSSFDataFormat.GetBuiltinFormat ( dataFormat );

        if( builtinFormatId != - 1)
        {
            style.DataFormat = builtinFormatId;
        }
        else
        {
            // not a built-in format, so create a new one
            var newDataFormat = workbook.CreateDataFormat ();
            style.DataFormat = newDataFormat.GetFormat ( dataFormat );
        }

        _cellStyleCache[dataFormat] = style;
    }
}
{ foreach(记录中的var记录) { //追加行 变量行=sheet.CreateRow(sheet.LastRowNum+1); //遍历所有配置的列 foreach(GetColumns()中的var列) { //附加单元格 Cell Cell=row.CreateCell(row.LastCellNum==-1?0:row.LastCellNum); 对象值=GetCellValue(列,记录); cell.SetCellValue(值); 字符串dataFormat=column.dataFormat??“m/d”; cell.CellStyle=GetCellStyleForFormat(sheet.Workbook,dataFormat); } } } 私有只读字典_cellStyleCache=新字典(); 专用CellStyle GetCellStyleForFormat(工作簿,字符串数据格式) { if(!\u cellStyleCache.ContainsKey(数据格式)) { var style=workbook.CreateCellStyle(); //检查这是否为内置格式 var builtinFormatId=HSSFDataFormat.GetBuiltinFormat(dataFormat); 如果(内置信息ID!=-1) { style.DataFormat=内置信息ID; } 其他的 { //不是内置格式,所以创建一个新格式 var newDataFormat=workbook.CreateDataFormat(); style.DataFormat=newDataFormat.GetFormat(DataFormat); } _cellStyleCache[dataFormat]=样式; } }