EPPlus: Locale settings in cell value and formula - c#

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

Related

How to separate decimals with comma using EPPlus to generate Excel

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);
}

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"

How to maintain or force currency formatting in PDF with Aspose

I'm using Aspose to populate fields in a PDF document from a database. Getting the value itself into the document is easy:
form.FillField("PurchasePrice", PurchasePrice.ToString()); // PurchasePrice is decimal
However, when I open the resulting PDF, the field shows up just as a decimal, even though the field in the PDF file is formatted as a Number with the Separator Style and Currency Symbol set.
So, a value of 1200.00 is being displayed as such, though I would expect it to be $1,200.00. If I change the field manually, ie remove the trailing decimals, the field is updated and displays in currency format as expected.
Is there a way using Aspose/C# or Javascript to force the PDF field formatting to be applied?
The value needs to remain as a numeric value since other calculations are dependent on it, so I can NOT something like this:
form.FillField("PurchasePrice", string.Format("{0:c}", PurchasePrice.ToString()));
I'm using Aspose.Pdf version 8.2.0.0 and .NET 4.5
It looks like the Aspose.PDF uses incorrect decimal separator i.e. ',' instead of '.' As a workaround you can try to change your System locale.

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.

Currency export to Excel results in  character for empty space

I am essentially exporting a webpage to Excel format. The export works fine, but when exporting Currency values that were formatted ToString("C") results in displaying the  character in the Excel cell before any space in the money value.
For Example:
decimal Invoice = 322800.38;
decimal Allocated = 695.55;
Label1.Text = Invoice.ToString("C");
Label2.Text = Allocated.ToString("C");
After the export the value in excel is: R 322 827,27 (Wrong)
and R 695,55 (Correct)
Any ideas?
Thank you
That's actually caused by a mis-interpretation of the encoding used to save it, when you have a hard space (160), usually given as in a web page. It looks like the web page is encoded as UTF8, but you're loading it into windows 'ANSI' format.
It depends on precisely what you're doing and how you're exporting, but you need to either change the encoding of the web page, or have excel use the encoding it was exported to when you import it.

Categories

Resources