gpt4 book ai didi

c# - Excel将我的数字的最后一位更改为零

转载 作者:行者123 更新时间:2023-12-04 15:27:53 25 4
gpt4 key购买 nike

就我而言,我创建了一个 CSV 文件使用 StreamWriter 在 c# 中使用下面的代码:

    [System.Web.Http.HttpPost]
public ActionResult createAllSubscribtionsExcelFile()
{
int ID = Convert.ToInt32(Request.Params["ID"]);
long totalRecords;
var Members = Services.MemberService.GetAll(0, int.MaxValue, out totalRecords);
string baseURL = string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
string packageName = Umbraco.Content(ID).Name;
string currentPath = Server.MapPath("/") + packageName;
string returnPath = baseURL + "/" + packageName;
string orderNumber = "none"; string price = "none"; string theBillNumber = "none";
List<RootObject> packages = null;
if (!System.IO.File.Exists(currentPath))
{
System.IO.File.Create(currentPath);
}
using (StreamWriter sr = new StreamWriter(currentPath + ".csv", false, Encoding.UTF8))
{
foreach (var member in Members)
{
var NestedPackages = member.GetValue<string>("packages");
if (NestedPackages != null)
{
packages = JsonConvert.DeserializeObject<List<RootObject>>(NestedPackages);
}

if (NestedPackages != null)
{
foreach (var package in packages)
{
var udi = package.package;
var content = Umbraco.Content(udi);
orderNumber = package.orderNumber;
price = package.theAmountPaid.ToString();
theBillNumber = package.theBillNumber;

sr.WriteLine("" + "," + orderNumber.Replace(',', ' ').Replace('/', ' ') + "," + price.Replace(',', ' ').Replace('/', ' ')
+ "," + theBillNumber.Replace(',', ' ').Replace('/', ' '));
}
}
}
sr.Close();
}
return Redirect(returnPath + ".csv");
}

就我而言 theBillNumber = "16744817032020146676" , 但是在 excel 表中显示的值是这样的
“16744817032020100000” (最后 5 位 Equuleus 零)。

我该如何解决这个问题,请帮助。

最佳答案

一个小技巧!

使用 c# 我创建了一个 .CSV单行:

12/25/2021,James,'1836542348674987512345363,12

注意 单引号 在真正大数字的开头。

如果我们不预先添加文本字符,Excel 会将数字视为超过 15 个十进制数字的超大整数。 Excel 会自动将尾随数字转换为零。单引号告诉 Excel 保留所有数字。

一旦 Excel 打开 .CSV通常,所有数字都存在,但单元格以单引号开头。以下 VBA 将打开 .CSV并将任何前导单引号转换为 PrefixCharacter。

然后使用 Excel VBA:
Sub dural()
Workbooks.Open Filename:="C:\Users\garys\Desktop\x.csv"

Dim cell As Range

For Each cell In ActiveSheet.UsedRange
v = cell.Value
If Left(v, 1) = "'" Then
cell.Clear
cell.NumberFormat = "@"
cell.Value = v
End If
Next cell
End Sub

我得到:

enter image description here

正如您通过将公式栏中的值与单元格中的值进行比较看到的那样,单引号不再是数字的一部分。它只是一个前缀字符。

这种处理方式适用于:
  • 长电话号码
  • USPS 追踪号码
  • 银行帐户和路由号码
  • 关于c# - Excel将我的数字的最后一位更改为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61890239/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com