How to separate decimals with comma using EPPlus to generate Excel - c#

I want to use comma as decimal separator and point as thousand separator for excel generation, but it doesn't work in EPPlus. As an example I use the code block below for formatting. It generates decimals like "2,300.250" but I need to generate decimals like "2.300,250". Is there a workaround for this ?
Note that, another question answers this if you accept to get number as text.
else if (propertyInfo.PropertyType.In(typeof(double), typeof(decimal)))
{
ws.Cell(row, cell).Style.NumberFormat.Format = "#.##0,00";
ws.Cell(row, cell).Value = propertyInfo.GetValue(detail);
}

Related

Using Regex.Replace function in c# program to replace a string(data) with "data"?

I want to make use of Regex.Replace function to replace data in the format
05-11
to
"05-11"
so that excel can read it as a string.
Excel is converting the data to 05-Nov even though that particular column is defined as char.
In my application code, I have the below piece of code to replace any data that starts with a dash (-) with double quotes, "data"
var newString = Regex.Replace(data, #"^(-.*)$", "=\"$0\"");
How can I make use of this function to replace any data which are like
'05-11', '15-2019'
with
"05-11", "15-2019"
for the excel to read them as a string not as date format.
Unfortunately Excel does not accept " as an indicator of a text column, the only way to be sure is to proceed the value with a single quote.
var newstring = Regex.Replace(data, #"\b""?(\d\d-(?:\d\d)?\d\d)""?\b", "\"=\"\"$0\"\"\"");
This finds possibly double-quoted date strings that constitute the whole column value, and outputs it quoted with an equals sign and double quotes.
Unfortunately this special formatting is lost if you save as CSV from inside Excel and try to reload.
See this question for details.

EPPlus: Locale settings in cell value and formula

I want to export formulas from a C# application to an Excel Sheet using the EPPlus library.
The locale settings of the C# application as well as the Excel Application are identical, which means that the decimal separator is a comma in my case.
While the assignment
sheet.Cells[row, column].Value = value;
will correctly be used by Excel (for example if value is a double),
the same will not work with
sheet.Cells[row, column].Formula = formula;
where formula is a string beginning with "=" and containing string converted doubles correctly containing a comma as a decimal separator.
In the latter case, Excel will say that the document is broken, while it will accept formulas containing a dot as a decimal separator.
I do not want to start replacing commas with dots or vice versa, but keeping the correct locale chain into the Excel formula.
Can this be done somehow? I think I need a setting through EPPlus to tell Excel that the locale setting in the formula is the same as in the normal cell content (where everything works as expected).
The localization option available in EPPlus is NumberFormat property to set your desired number format.
In your case, you could try:
sheet.Cells[row, column].Numberformat.Format="#,##0.00"; //set your format of dots & commas

Decimal not showing leading zero

I have the following code that formats a column as decimal
It works fine however if the number is less than 1 the zero is not displayed
foreach (var deciCol in decimalIndx)
{
var col = deciCol.Start.Column;
sheet.Column(col).Style.Numberformat.Format = "#.####";
}
Input 12.35486 ==> in excel 12.3548 (OK)
Input 0.34845 ==> in excel .3484 (0 is not displayed)
Input 0 ==> in excel (0.) (how can i remove the decimal separator)?
Thank you in advance
Edit:
Thanks to the answer below, i used the following:
"0.0###"
# means optional digit. Use 0 for a leading zero, eg "0.####".
The format string is the same format string you'd use in Excel if you selected a Custom format code. You can test the format string in Excel first and once you find the one you want, use it in EPPlus.
The contents of a custom numeric format string are documented in Excel's docs. Check Create or delete a custom number format. This explains how to specify different formats for positive, negative, zero amounts, include extra text etc.
It would seem that you can even specify colors in the format string. I wonder how [Blue]0.###;[Red]-0.### would look
UDPATE
As the linked page shows, you can specify a different format for zero, eg :
"0.####;-0.###;0"

C# COM Interop Excel: Preserving Leading Zeros In Text Strings

I am outputting an Excel worksheet that contains strings of the form hh:mm:ss. However, Excel is seeing fit to delete any leading zeros even though they are text. I tried myRange.NumberFormat = "#" but this results in bizarre decimal representations.
Any advice is appreciated.
Regards.
Asked too soon. Setting range.NumberFormat = "hh:mm:ss" did the trick without affecting the other strings in that column.

Custom Numeric Format Strings: Dynamic Decimal Point

I am trying to format a double in C# such that it uses the thousand separator, and adds digits upto 4 decimal places.
This is straight forward except that I dont want to have the decimal point if it is an integer. Is there a way to do this using the custom numeric format strings rather than an if statement of tenary operator?
Currently I have:
string output = dbl.ToString(dbl == (int)dbl ? "#,##0" : "#,##0.####");
Thanks
I believe your second format string of "#,##0.##" should be exactly what you want -- the # format character is a placeholder that will NOT display zeros.
If you had "#,###.00" then you would get trailing zeros.
test code:
double d = 45.00;
Console.Writeline(d.ToString("#,##0.##"));
Gives output of "45". Setting d to 45.45 gives output "45.45", which sounds like what you're after.
So you had the answer after all! ;)
Incidentally, there's a handy cheat-sheet for format strings (amongst other handy cheat-sheets) at http://john-sheehan.com/blog/net-cheat-sheets/
No, there is not any built-in format string for this. Your current solution is the best way to accomplish this.
MSDN lists both the standard numeric format strings and custom numeric format strings, so you should be able to see for yourself that none directly matches your needs.

Categories

Resources