我想使用Open XML将日期值插入excel文件。
这是我的代码示例。
cell.CellValue = new CellValue(value.ToString());
cell.DataType = new EnumValue<CellValues>(CellValues.Date);
答案 0 :(得分:1)
显然,应该省略DataType!
我有同样的问题,但只是设置CellValue就可以了!
cell.CellValue = new CellValue(value.ToOADate().ToString())
答案 1 :(得分:0)
我得到了答案,并与大家分享......
只需像我们一样添加单元格。
Cell cell =new Cell(){ cellReference=A1 }; //Or other necessary details
//Add value to cell in Double Format then convert it into string using ToString()
cell.cellValue = new CellValue(DateTime.Now().ToDouble().ToString());
//Set the data type as Number
cell.DataType = new EnumValue<CellValues>(CellValues.Number);
//Give its StyleIndex = 5 (default style index of date)
cell.StyleIndex=5;
我在这里用过
cell.StyleIndex=5;
这是Excel中日期的默认样式索引。所以不需要添加所有外部styylesheets
享受:)
答案 2 :(得分:-1)
以下为我们工作:
c.CellValue = new CellValue(datetimeValue).ToOADate().ToString());
c.DataType = CellValues.Number;
c.StyleIndex = StyleDate;
将DataType设置为CellValues.Number,然后确保使用CellFormats中的相应样式索引格式化单元格。在我们的例子中,我们在工作表中构建样式表,StyleDate是样式表中CellFormats的索引。