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.
Related
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);
}
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
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"
this field has leading zeros.
newSheet.Cells[i + 2, 5] = drugorder.NDC;
the code is seeing it when it looks up the NDC description in the SQL Server table. But it doesnt display when we write this col to the new sheet in Excel.
how can we display the leading zeros?
Did you try drugorder.NDC.ToString("0000") ?
You can set the number format of the cells in question to be Text. This will preserve the leading zeroes. Take the Range object in question and set its NumberFormat property to be #
I make from my program an excel file (xls or csv).
I send 00123 and in Excel I see 123
How I can send and see 00123
Thanks in advance
Its because excel is treating the data as 'numeric'. A simple way to force Excel to accept anything as text is to prepend an apostrophe to the text.
e.g. to write an integer 123 as 00000123 just write:
ActiveCell = "'" & Format(123, "00000000")
EDIT: Another solution is to set the Cells NumberFormatProperty to text:
Worksheet.GetRange(..).EntireColumn.NumberFormat = "#"
You might want to see this article: Excel Cell Auto Format
In C# to see the CSV with the padding use
myVar.PadLeft(5,'0')
In Excel set the number format to custom 00000 or ZipCode