如何创建 CSV 以便数字在电子表格中显示为字符串?

发布于 2024-10-25 16:02:59 字数 599 浏览 1 评论 0原文

我正在尝试构建一个应用程序,从数据库中提取一些数据,然后使用一些数据创建一个由 Excel 加载的 CSV 文件。代码:

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + "," + cm.BatNbr.Trim()
        + "," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}

“cm.BatNbr”是一个 6 个字符的零填充数字,例如“001234”。我希望 Excel 将该列格式化为文本,这样我就不会丢失前面的零。我尝试了一些技巧,例如在数字前加上单引号(撇号),但我得到的只是撇号前缀。如果我将单元格设置为文本格式,然后删除撇号,我也会丢失前面的零。

我意外地发现,如果我用百分号作为前缀,Excel 会将单元格中的值转换为百分比,所以也许我可以使用一些前缀来使 Excel 在加载单元格中的值时将其作为文本向上?

I am trying to build an application that extracts some data from a database, and then uses some of the data to create a CSV file to be loaded up by Excel. The codez:

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + "," + cm.BatNbr.Trim()
        + "," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}

The "cm.BatNbr" is a 6 character zero-filled numeric such as "001234". I want Excel to format that column as text so I don't lose the zeroes up front. I've tried some tricks, such as prefixing the number with a single-quote (apostrophe), but all I get is an apostrophe prefix. If I set the cells to be formatted as text then remove the apostrophes, I also lose the zeroes at the front.

I accidentally found that if I prefix the thing with a percent sign, Excel converts the value in the cell to a percentage, so perhaps there is some prefix I can use to cause Excel to take the value in the cell as text when I load it up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

苦行僧 2024-11-01 16:02:59

您可以将数据格式设置为 ="001234"。这将导致 Excel 将其显示为 001234。

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + ",=\"" + cm.BatNbr.Trim()
        + "\"," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}

您还可以尝试使用 SYLK 格式而不是 CSV。 SYLK 使您可以更好地控制格式。

You could format the data as ="001234". This will cause Excel to display it as 001234.

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + ",=\"" + cm.BatNbr.Trim()
        + "\"," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}

You could also try using the SYLK format instead of CSV. SYLK gives you better control over formatting.

一抹苦笑 2024-11-01 16:02:59

这对我有用:

更改行:

    + "," + cm.BatNbr.Trim()

用于:

    + ",\t" + cm.BatNbr.Trim()

This worked for me:

Change the line:

    + "," + cm.BatNbr.Trim()

for:

    + ",\t" + cm.BatNbr.Trim()
向地狱狂奔 2024-11-01 16:02:59

尝试引用 BatNbr

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + ", \"" + cm.BatNbr.Trim() + "\""
        + "," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}

Try quoting the BatNbr

foreach (xOFDocInfo cm in docs)
{
    string s = bi.Agency
        + ", \"" + cm.BatNbr.Trim() + "\""
        + "," + cm.RefNbr
        + "," + cm.DocType
        + "," + cm.OrigDocAmt.ToString()
        + "," + cm.CreateDate.ToShortDateString();

    writer.WriteLine(s);

}
时光病人 2024-11-01 16:02:59

用引号包围每个值应该可以解决您的问题(编辑:我发现这现在不起作用,但我将在这里留下这个答案,因为其余信息可能仍然有用)。

从技术上讲,无法在 CSV 中指定类型,这适用于 CSV 创建者和 CSV 阅读者。有些程序尝试从 CSV 推断数据类型,但这不是一个好的做法,并且 CSV 不以任何方式支持。

您可以将每个值括在引号中,事实上我会推荐它,即使它是可选的。

同样重要的是,如果用引号引起来的值,请确保逗号和起始引号之间没有前导空格。这会扰乱 Microsoft Excel,并将单元格呈现为空白。

Surrounding every value with quotes should solve your problem (EDIT: I see that this won't work now, but I will leave this answer here since the rest of the info might still be useful).

Technically there is no way to specify type in CSV, and this applies to both CSV creators and CSV readers. Some programs try to infer data types from the CSV but this is not good practice and not supported in any way by CSV.

You are allowed to surround every value in quotes, in fact I would recommend it even though it is optional.

Also important, if surrounding a value with quotes, make sure there is no leading whitespace between the comma and beginning quote. This messes up Microsoft Excel and it will render the cell as blank.

空城之時有危險 2024-11-01 16:02:59

您可以在将 CSV 导入 Excel 时解决该问题。当您导入 CSV 文件并设置解析时,您可以选择指定数字格式。我一直在营销列表中的邮政编码列中使用它,以防止丢失新英格兰邮政编码中的前导零。

此外,正如 Richard 指出的,您可以在数字之前添加关键字,然后使用 Excel 中的“文本到列”功能解析该列。

You could address the issue while you're importing the CSV into Excel. As you import and set the parsing for CSV files, you have the option of assigning the number format. I use this all the time for ZIP code columns in marketing lists to prevent losing the leading zeros in New England ZIPs.

Also, as Richard noted, you could add a keyword before the numbers then parse the column using the Text to Columns feature in Excel.

久随 2024-11-01 16:02:59

我打开一个空白工作簿,然后转到“数据”菜单,然后在左侧选择“来自文本/CSV”。我选择 CSV 文件,然后在顶部下拉列表之一“数据类型检测”中选择“不检测数据类型”。

我有一列序列号,每个序列号有 24 位数字长。它将“8.929595956+E23”转换为我眼前的原始 24 位数字。

然后我点击底部的“加载”。

24 位序列号栏看起来很完美。

这是 Office 365(版本 2008)中的 Excel 格式。

I opened up a blank workbook, then went to the Data menu, then on the left side selected "From Text/CSV". I choose my CSV file, then in one of the top dropdowns "Data Type Detection", selected "Do not detect data types".

I had a column of serial numbers, each 24 digits long. It converted "8.929595956+E23" to the raw 24-digit number in front of my eyes.

Then I hit Load at the bottom.

The 24-digit serial number column looked perfect.

This is in Excel from Office 365 (Version 2008).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文