使用 EPPlus 写入 IP 地址

Write an Ip Address with EPPlus

我正在尝试使用 EPPlus 将一些 IP 地址写入 excel。 事实上它并不是真正的 ip 地址 -> 300.278.100 它代表一个子网。我试试这个格式:

cell.Style.Numberformat.Format = "##0.##0.##0";
cell.Value = val;

cell.Style.Numberformat.Format = "@";
cell.Value = val;

并且总是得到类似于上图的东西。 如您所见,我得到了不同的结果。我们在哪里可以看到 36892 它应该是“1.1.1”。 一些结果像数字一样出现,像文本一样存储,而其他结果是文本。 我希望他们中的每个人都像没有警告的文本​​一样存储,或者像带有 ip 格式的数字一样存储。

有人知道我该如何实现吗?

问题是多于一位的小数点在数字中没有多大意义。关于我能想到的最接近的,如果你想把所有的东西都保留为数字,就像:

//With Leading zeros - prints as "001.001.001" in excel
//Should work for all subnets
sheet1.Cells[1, 2].Style.Numberformat.Format = "000\.000\.000";
sheet1.Cells[1, 2].Value = 1001001;

不确定是否有一种方法可以选择性地显示像这样的实数中的中间零。我想你可以像这样调整每个单元格:

//Without Leading zeros but has to be adjusted for each value
//print as "1.1.1" in excel
sheet1.Cells[1, 3].Style.Numberformat.Format = "0\.0\.0";
sheet1.Cells[1, 3].Value = 111;

//print as "1.11.1" in excel
sheet1.Cells[1, 4].Style.Numberformat.Format = "0\.00\.0";
sheet1.Cells[1, 4].Value = 1111;

不太漂亮。否则,可以省略格式并将它们设置为字符串。