c# - EPPlus 忽略小数秒?
问题描述
使用 EPPlus 将数据网格的内容写入 Excel 文件。这个功能:
public static void FormatExcelCell(OfficeOpenXml.ExcelRange cellExcel,
object cellValue,
System.Type cellValueType,
bool UseAlternateDateFormat = false)
{
cellExcel.Value = cellValue;
if (cellValue.ToString() != Constants.ValueForMissingTag)
{
if (cellValueType == typeof(System.DateTime))
cellExcel.Style.Numberformat.Format =
UseAlternateDateFormat ?
Constants.FormatDateTimeAlternateExport :
Constants.FormatDateTime;
}
else
{
cellExcel.Style.Fill.PatternType =
OfficeOpenXml.Style.ExcelFillStyle.Solid;
cellExcel.Style.Fill.BackgroundColor.SetColor
(Constants.ColorForMissingTag);
}
}
如果格式字符串设置为“yyyy-MM-dd HH:mm:ss.ff”,则Excel文件中的日期如下所示:2018-06-18 15:45:25.ff
如果格式字符串设置为“yyyy-MM-dd HH:mm:ss.00”,则Excel文件中的日期如下所示:2018-06-18 15:45:25.00
EPPlus 是在删除几分之一秒(我知道它在那里,因为我可以在数据网格中看到它),还是它以某种方式破坏了格式字符串?这是一个错误吗?有解决方法吗?
谢谢。
更新:将 EPPlus 从 4.1.0.1 更新到 4.5.2.1,并添加了以前不需要的 OpenXML;仍然会产生上述错误的日期时间格式。
解决方案
这是我在代码中的操作方式。这是一种扩展方法。
public static void SetCellDateTimeWithMsValue(this ExcelRange cell, DateTime? value)
{
cell.Style.Numberformat.Format = "yyyy-MM-dd HH:mm:ss.000";
if (!value.HasValue) return;
cell.Value = value.Value;
}
推荐阅读
- python - 遍历 JSON 需要比我想要的更多的循环
- r - 将 qqcomp 与 ggplot 对象相结合
- python - 如何覆盖 if 语句中的函数?
- bash - 集群上 bash 运行之间的延迟
- amazon-web-services - 是否可以按类型(即标准、标准 IA、减少冗余)列出 s3 存储桶?
- python - 我想创建一个工具栏,在单击按钮时更改框架
- node.js - 如何使用量角器选择与文本匹配的行
- wikipedia-api - 从 wikipedia API 获取 wikitext?
- c# - 从 user32.dll EnumDisplayDevices 取回坏字符串
- node.js - Node.js %OptimizeFunctionOnNextCall - 运行时函数错误