简体   繁体   English

在 C# 写入 Excel 中,如何按原样显示大数字(不使用科学记数法)?

[英]In C# Writing to Excel, How to show a big number as it is (Not with scientific notation)?

I am sending an excel file from backend.我正在从后端发送一个 excel 文件。 I have got long numbers for some cards' numbers.我有一些卡片号码的长号码。 I need to show them in Excel but I want all of them to be number and edit them as it is (sometimes the value and the shown text differs, I want them to be the same).我需要在 Excel 中显示它们,但我希望它们都是数字并按原样编辑它们(有时值和显示的文本不同,我希望它们相同)。
The proplem is when I set the data type of the cell like问题是当我设置单元格的数据类型时

    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

I can set the value with SetValue method, which makes the cell string:我可以使用 SetValue 方法设置值,这使得单元格字符串:

worksheet.Cell(currentRow, 6).SetValue(cardNo);

But in this case, excel adds an apostrophe in front of the number, which makes it a text seemingly.但是在这种情况下,excel在数字前面添加了一个撇号,这使得它看起来像是一个文本。 When you try to edit the cell, the apostrophe exists in front of the number.当您尝试编辑单元格时,数字前面存在撇号。

Lastly I tried to set the NumberFormatId of the cell by最后,我尝试通过以下方式设置单元格的 NumberFormatId

    worksheet.Cell(currentRow, 6).Value = cardNo;
    worksheet.Column(6).Style.NumberFormat.NumberFormatId = 1;

For information about NumberFormatId: https://github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md In this case the cell is a number, seemingly a number, but again when you try to edit the row, the row contains a scientific number.有关 NumberFormatId 的信息: https : //github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md在这种情况下,单元格是一个数字,看似是一个数字,但是当您尝试编辑时再次行,该行包含一个科学数字。

I can achieve what I want over excel like this:我可以像这样通过 excel 实现我想要的:

  • Go to a column, right click, Format Cells转到一列,右键单击,设置单元格格式
  • Numbers tab -> Number数字选项卡 -> 数字
  • Options-> Decimal Places, set to 0, Format Code set to 0. And then add a number to a row in that column.选项->小数位,设置为0,格式代码设置为0。然后在该列的行中添加一个数字。

Or like this:或者像这样:

  • Go to a column -> right click -> format cells转到一列 -> 右键单击​​ -> 设置单元格格式
  • Numbers Tab -> Text -> OK.数字选项卡 -> 文本 -> 确定。 And edit the lines.并编辑行。

How can I achieve the same thing in ClosedXML in C#如何在 C# 中的 ClosedXML 中实现相同的功能

Here is the wiki for number formats: https://github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md这是数字格式的维基: https : //github.com/ClosedXML/ClosedXML-wiki/blob/master/NumberFormatId-Lookup-Table.md

In the question what was trying to be achieved was this:在问题中试图实现的是:

  • Go to a column -> right click -> format cells转到一列 -> 右键单击​​ -> 设置单元格格式
  • Numbers Tab -> Text -> OK.数字选项卡 -> 文本 -> 确定。 And edit the lines.并编辑行。

By looking at the NumberFormatId table in the given link, the Id of this operation is 49 so the code must be worksheet.Column(6).CellsUsed().Style.NumberFormat.NumberFormatId = 49;通过查看给定链接中的 NumberFormatId 表,此操作的 Id 为 49,因此代码必须为worksheet.Column(6).CellsUsed().Style.NumberFormat.NumberFormatId = 49;

And another problem is you have to use SetValue instead of setting Value like:另一个问题是你必须使用 SetValue 而不是像这样设置 Value:
worksheet.Cell(currentRow, 6).SetValue(cardNo);

After that it must work.之后它必须工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM