You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为DocumentFormat.OpenXml设置带逗号小数分隔符的单元格数字格式

使用DocumentFormat.OpenXml v2.16.0生成数值型单元格时的小数分隔符问题

我在使用DocumentFormat.OpenXml v2.16.0生成报表时遇到问题:要求单元格为数值型以支持用户使用SUM等计算函数,但无法实现用逗号作为小数分隔符的显示效果。

编号格式代码

NumberingFormats numberingFormats = new NumberingFormats() { Count = (UInt32Value)1U };
NumberingFormat decimalFormat = new NumberingFormat()
{ 
    NumberFormatId = (UInt32Value)165U,
    FormatCode = "#,##0.000" 
};
numberingFormats.Append(decimalFormat);

单元格格式代码

CellFormats cellFormats1 = new CellFormats() { Count = (UInt32Value)1U };
CellFormat cellFormat5 = new CellFormat() { NumberFormatId = (UInt32Value)165U, FontId = (UInt32Value)0U, FillId = (UInt32Value)0U, BorderId = (UInt32Value)1U, FormatId = (UInt32Value)2U, ApplyBorder = true, ApplyNumberFormat = true };
cellFormats1.Append(cellFormat5);

创建单元格函数代码

private Cell CreateCell(decimal value, uint styleIndex, CellValues dataType )
{
    Cell cell = new Cell();
    cell.StyleIndex = styleIndex;
    cell.DataType = dataType;
    cell.CellValue = new CellValue(value.ToString("#,##0.000", CultureInfo.CreateSpecificCulture("es-ES")));
    return cell;
}

我尝试过两种处理方式,但都存在问题:

  • 将单元格值设为字符串时,格式显示正常,但SUM函数返回0,因为文本类型无法参与计算
  • 将单元格值设为decimal类型时,单元格能正常参与SUM计算,但逗号小数分隔符的显示不生效

SUM函数示例

内容的提问来源于stack exchange,提问作者Juan Matías Ojeda

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:46:14