[英]In C# Writing to Excel, How to show a big number as it is (Not with scientific notation)?
我正在从后端发送一个 excel 文件。 我有一些卡片号码的长号码。 我需要在 Excel 中显示它们,但我希望它们都是数字并按原样编辑它们(有时值和显示的文本不同,我希望它们相同)。
问题是当我设置单元格的数据类型时
var currentRow = 1;
var cardNo = "9993232323232320";
worksheet.Column(6).CellsUsed().SetDataType(XLDataType.Text);
worksheet.Cell(currentRow, 6).Value = cardNo;
worksheet.Column(6).CellsUsed().SetDataType(XLDataType.Text);//Doesn't matter if it is before or after
我可以使用 SetValue 方法设置值,这使得单元格字符串:
worksheet.Cell(currentRow, 6).SetValue(cardNo);
但是在这种情况下,excel在数字前面添加了一个撇号,这使得它看起来像是一个文本。 当您尝试编辑单元格时,数字前面存在撇号。
最后,我尝试通过以下方式设置单元格的 NumberFormatId
worksheet.Cell(currentRow, 6).Value = cardNo;
worksheet.Column(6).Style.NumberFormat.NumberFormatId = 1;
有关 NumberFormatId 的信息: https : //github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md在这种情况下,单元格是一个数字,看似是一个数字,但是当您尝试编辑时再次行,该行包含一个科学数字。
我可以像这样通过 excel 实现我想要的:
或者像这样:
如何在 C# 中的 ClosedXML 中实现相同的功能
这是数字格式的维基: https : //github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md
在问题中试图实现的是:
- 转到一列 -> 右键单击 -> 设置单元格格式
- 数字选项卡 -> 文本 -> 确定。 并编辑行。
通过查看给定链接中的 NumberFormatId 表,此操作的 Id 为 49,因此代码必须为worksheet.Column(6).CellsUsed().Style.NumberFormat.NumberFormatId = 49;
另一个问题是你必须使用 SetValue 而不是像这样设置 Value:
worksheet.Cell(currentRow, 6).SetValue(cardNo);
之后它必须工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.