简体   繁体   English

C# SSIS 脚本任务 - 格式化 Excel 列时添加千位分隔符

[英]C# SSIS Script Task - Add Thousand Seperator when formatting Excel column

Currently using the following C# in a script task in SSIS to format an Excel column as a Number.当前在 SSIS 的脚本任务中使用以下 C# 将 Excel 列格式化为数字。

sheet.Columns[6].NumberFormat = "0.00";

However when the column is created the thousands separator is missing.但是,当创建列时,千位分隔符丢失。 Is there a way to add this in?有没有办法添加这个?

I believe you can use this:我相信你可以使用这个:

sheet.Columns[6].NumberFormat = "#,##0.00";

Places with zeros will show even if the value is 0, whereas places with pound signs (#) will only show if needed.即使值为 0,带零的位置也会显示,而带井号 (#) 的位置只会在需要时显示。

The following are some examples of what will show when using this format:以下是使用此格式时将显示的一些示例:

10000 -> 10,000.00
 1000 -> 1,000.00
   10 -> 10.00
 5.25 -> 5.25
  .25 -> 0.25
   .1 -> 0.10

After doing some research, here is what I found:在做了一些研究之后,这是我发现的:

Looking here, I found a solution that should work for you: format a number with commas and decimals in C# (asp.net MVC3)看这里,我找到了一个适合您的解决方案: 在 C# (asp.net MVC3) 中用逗号和小数格式化数字

To summarize the post, it states you should define a variable (number) and then initialize it.总结这篇文章,它指出你应该定义一个变量(数字)然后初始化它。 After, you will format twice: First using .ToDecimal(number) then .ToString(("#,##0.00")之后,您将格式化两次:首先使用.ToDecimal(number)然后使用.ToString(("#,##0.00")

Now, let's apply this to your example:现在,让我们将其应用于您的示例:

Define & initialize your variable -- let's say you call it the name of the 6th column in your sheet (for this example, let's assume you define it as constant that never changes named 'cost')定义并初始化您的变量——假设您将其称为工作表中第 6 列的名称(对于本示例,假设您将其定义为永远不会更改的常量,名为“成本”)

public const int cost = sheet.Columns[6]

Then you want to format:然后你想格式化:

Convert.ToDecimal(cost).ToString("#,##0.00");

This SHOULD get you the desired result.这应该给你想要的结果。

The Number Format expression consists of the following numeric specifiers:数字格式表达式由以下数字说明符组成:

# : Digit placeholder # :数字占位符
0 : Zero placeholder 0 :零占位符
, : Decimal point , :小数点
. . : Decimal separator :十进制分隔符
[Red] : Color specifier [红色]颜色说明符
% : Percentage placeholder % :百分比占位符

You can use the following expression to show the thousands separators:您可以使用以下表达式来显示千位分隔符:

sheet.Columns[6].NumberFormat = "#,##0.00";

Examples (image taken from references below) :示例(图片取自以下参考资料)

在此处输入图片说明

References:参考:

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

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