我正在尝试使用 C# 创建 Excel 文件,并且为此目的使用 OpenXML 库。但是,我遇到了一个问题,数字格式无法识别,导致出现如下图所示的错误。
我正在分享向下面的单元格添加数据的部分:
// The text here actually contains a number, like this: "1"
CellValue cellValue = new CellValue(text);
cell.DataType = CellValues.Number;
cell.CellValue = cellValue;
使用正确的类型而不是文本:
cell.DataType = CellValues.Number;
cell.CellValue = new CellValue(1);
Excel 中的数字不仅仅是具有
Number
样式的文本。它们作为强类型值存储在 XML 文件本身中,确保 3.21
和 3,21
之间不会因本地化解析而出现任何混淆。
CellValue 构造函数接受不同的值类型,包括
int
、DateTime
、Decimal
、Boolean
等