Excel中不插入前导零 [英] Excel not inserting leading zero

查看:236
本文介绍了Excel中不插入前导零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Office OpenXML可以写入到一个Excel文件。该文件是一个模板,它已经拥有我所有的标题和格式为我的专栏。我插入有前导零到一个特殊的列这基本上是一个10位数字的号码。然而,在我的代码,我可以看到它的值,例如设置为 0000000004 。结果在表与该小区一 4 的值并显示实际电池 0000000004

I am using Office OpenXml to write to an Excel file. The file is a template so it already has all my headers and formatting for my columns. I am inserting numbers that have leading zeroes to a "special" column which is basically a 10 digit number. However in my code I can see that it is setting the value to for example 0000000004. The result in the sheet with a value of 4 in that cell and the actual cell showing 0000000004.

下面是我的代码写入单元格中。

Here is my code to write to the cell.

  if (reader[2].ToString().Length < 9)
  {


        myworksheet.Cell(firstrow, 12).Value = reader[2].ToString(); //0045678945

  }
  else
  {
        myworksheet.Cell(firstrow, 12).Value = reader[2].ToString().Substring(0, 9); //0045678945

  }



当我打开excel表就像我说我上面的值为 45678945 而不是 0045678945

任何。帮助将不胜感激。

Any help would be appreciated.

推荐答案

如果我理解正确的:

Excel中显示了前导零,通过OOXML从C#阅读他们看不到前导零的数字。

Excel shows numbers with leading zeroes, reading them from C# via OOXML you don't see the leading zeroes.

机会是Excel中设置一些格式规则,而不是存储实际前导零。

Chances are Excel is setting some formatting rules instead of storing the actual leading zeroes.

几种方法来抵消。这里有最便宜那些浮现在脑海中,挑选一个:

Several ways to counteract that. Here are the "cheapest" ones that come to mind, pick one:


  • 在Excel格式列文

  • 在C#代码并不指望前导零,而是将它们添加

这篇关于Excel中不插入前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆