【发布时间】:2017-09-20 11:28:36
【问题描述】:
我在 C# 中使用 OpenXML 电子表格包将 datatable 转换为 excel。在代码中,我正在编写此代码以附加具有数字类型的单元格:
public void MakeNumericCell(string cellReference, string cellStringValue, Row excelRow)
{
// Add a new Excel Cell to our Row
Cell cell = new Cell() { CellReference = cellReference, DataType=CellValues.Number };
CellValue cellValue = new CellValue(cellStringValue);
cell.Append(cellValue);
excelRow.Append(cell);
}
请注意,我不是在问How to convert Text cell to Number 或change data type (format) of Excel Cell谁的答案已经存在于 SO。
在我的例子中,单元格 do 在 Excel 工作表中转换为数字格式。我可以使用公式=Type(Cell) 找到 Excel 单元格的类型,如果单元格类型是数字,则返回 1。如图所示,最后一列(列名是 Type of Cell)使用 =Type(cell) 公式计算,该公式为 C 列返回 1,但它仍然出现在 General 中,如图所示。
我在这里做错了吗?还是 Excel 本身的问题?
【问题讨论】:
-
除了答案,您可能还想使用
=CELL("format", A2)等来检查单元格的格式。参考检查TYPE function和CELL function。
标签: c# excel openxml-sdk