C# 日期格式单元格
我正在使用NPOI在Sheet1中创建固定的工作表模板,需要来自Sheet2的日期格式的数据。我从数据库生成DataTable以设置Sheet2中的数据。 这是我的密码:C# 日期格式单元格,c#,date,format,cell,npoi,C#,Date,Format,Cell,Npoi,我正在使用NPOI在Sheet1中创建固定的工作表模板,需要来自Sheet2的日期格式的数据。我从数据库生成DataTable以设置Sheet2中的数据。 这是我的密码: private DataTable getWeek() { strConn = WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString; conn = new OdbcConnection(strConn); conn.Ope
private DataTable getWeek()
{
strConn = WebConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
conn = new OdbcConnection(strConn);
conn.Open();
sql = "SELECT week, sunday, saturday FROM tb_weekreferences";
da = new OdbcDataAdapter(sql, conn);
dt = new DataTable();
da.Fill(dt);
conn.Close();
return dt;
}
然后将数据表导出到Excel:
private int ExportDataTableToExcel(DataTable sourceTable, ISheet sheet)
{
IRow headerRow = sheet.CreateRow(0);
ICell headerCell;
ICell cell = null;
Dictionary<String, ICellStyle> styles = CreateExcelStyles(hssfwb);
//handling value
int rowIdx = 1;
foreach (DataRow row in sourceTable.Rows)
{
IRow dataRow = sheet.CreateRow(rowIdx);
foreach (DataColumn column in sourceTable.Columns)
{
cell = dataRow.CreateCell(column.Ordinal);
cell.SetCellValue(row[column].ToString());
cell.CellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("");
cell.CellStyle = styles["cell"];
}
rowIdx++;
}
//handling header
foreach (DataColumn column in sourceTable.Columns)
{
headerCell = headerRow.CreateCell(column.Ordinal);
headerCell.SetCellValue(column.ColumnName);
headerCell.CellStyle = styles["header"];
sheet.AutoSizeColumn(column.Ordinal);
sheet.SetColumnWidth(column.Ordinal, (sheet.GetColumnWidth(column.Ordinal) + (5 * 256))); // set width = autofit() + (5 * 256)
}
return rowIdx;
}
private int ExportDataTableToExcel(数据表源表,ISHET表)
{
IRow headerRow=sheet.CreateRow(0);
ICell-headerCell;
ICell单元=空;
字典样式=CreateExcelStyles(hssfwb);
//处理值
int rowIdx=1;
foreach(sourceTable.Rows中的DataRow行)
{
IRow dataRow=sheet.CreateRow(rowIdx);
foreach(sourceTable.Columns中的DataColumn列)
{
cell=dataRow.CreateCell(column.Ordinal);
cell.SetCellValue(行[列].ToString());
cell.CellStyle.DataFormat=HSSFDataFormat.GetBuiltInfo(“”);
cell.CellStyle=样式[“cell”];
}
rowIdx++;
}
//装卸头
foreach(sourceTable.Columns中的DataColumn列)
{
headerCell=headerRow.CreateCell(列序号);
headerCell.SetCellValue(column.ColumnName);
headerCell.CellStyle=样式[“标题”];
sheet.AutoSizeColumn(列序号);
sheet.SetColumnWidth(column.Ordinal,(sheet.GetColumnWidth(column.Ordinal)+(5*256));//set width=autofit()+(5*256)
}
返回rowIdx;
}
在表1中,我使用vlookup公式从表2中得到“星期天”和“星期六”。但它不起作用,因为Sheet2中的week、sunday和saturday的值看起来像字符串(左对齐单元格)。生成excel数据时,如何设置日期中的单元格格式?
请给我一个解决办法
谢谢。第一个问题是这条线路
cell.SetCellValue(row[column].ToString());
您正在将值转换为字符串,但可能会将其转换为DateTime格式,所以NPOI知道这是DateTime
或者,如果这样做有效,请尝试:
cell.SetCellValue(DateTime.Now);
第二,尝试先设置单元格的样式,然后将其更改为DataFormat属性,但使用如下自定义格式:
IDataFormat dataFormatCustom = workbook.CreateDataFormat();
cell.CellStyle = styles["cell"];
cell.CellStyle.DataFormat = dataFormatCustom.GetFormat("yyyyMMdd HH:mm:ss");
这将把日期时间值格式化为可读格式。在您的第一个代码中将是“立即获取日期”,但我需要从其他工作表中获取日期。我已经尝试了您的第二个代码,它不起作用,通过vlookup公式将单元格格式化为sheet2中的字符串。因此,似乎sheet2中存在问题。从数据库填充Sheet2时(在执行查找之前),是否能够将该日期转换为DataTime格式?数据库中有什么格式?也许在您使用getWeek()方法读取的DataTable中,您可以创建一些日期为DateTime格式的临时列,而不是字符串,并使用这些列进行查找?我使用SQL Server 2008,字段“sunday”和“saturday”是日期。在sql查询中,我使用以下命令进行转换:convert(varchar(9),sunday,6)作为sunday->12月31日。当从datatable导出到excel时,我检查列的类型:if(type==“System.DateTime”){cell.CellValue(Convert.ToDateTime(row[column]);}如果类型是Int或String,它就工作了,但在date中我得到了这个错误:对象不能从DBNull转换为其他类型。你有更好的主意吗?