Currently using the following C# in a script task in SSIS to format an Excel column as a Number.
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?
Currently using the following C# in a script task in SSIS to format an Excel column as a Number.
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.
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)
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")
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')
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
, : 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):