Hi ALL!
我的 OpenXml 有问题,我必须生成一个报告,并且报告的单元格必须是数值,以便最终用户进行一些计算,但我无法使其使用逗号作为小数分隔符,这是代码我使用 DocumentFormat.OpenXml v2.16.0
编号格式
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,如果我将单元格的值设置为小数,则不应用逗号小数分隔符。
如果有人需要的话,有一个类可以追加到Workbook上,类名为ServerFormat,在这个类中你可以指定区域性,它不适用于所有类似excel的程序
private void GenerateWorkbookPartContent(WorkbookPart workbookPart)
{
Workbook workbook = new Workbook();
ServerFormat serverFormat = new ServerFormat() { Culture = "es-ES" };
Sheets sheets = new Sheets();
Sheet sheet1 = new Sheet() { Name = "Sheet1", SheetId = (UInt32Value)1U, Id = "rId1" };
sheets.Append(sheet1);
workbook.Append(sheets);
workbook.Append(serverFormat);
workbookPart.Workbook = workbook;
}
然后只需保存数据类型为number的单元格值