Question

Hi I am using Ep Plus with a web application. I am creating an Excel file that the user can fill out and upload back to the application. When I enter in a number with leading zeros, they are truncated.

Is there away to allow leading Zeroes to be entered by the user?

Was it helpful?

Solution 3

The format that I needed was "@" so the line of code was

codeSheet.Cells["A:D"].Style.Numberformat.Format = "@";

OTHER TIPS

The accepted answer for this question did not work for me. I found this article from Microsoft which solved my problem. https://support.microsoft.com/en-gb/help/81518/using-a-custom-number-format-to-display-leading-zeros

I was dealing with zip codes so I was able to do this:

workSheet.Column(4).Style.Numberformat.Format = "00000";

This works since my zipcodes will always be 5 digits.

Playing around with the style of the ExcelRange you can achieve what you want:

var range = // get the range you want
// Force the numbers to have 2 digits to the left and 1 digit to the right
// of the decimal place.
range.Style.NumberFormat.Format = "00.0"

You can play with the different formats available by opening Excel and playing with the number format dialog.

Format Cells

Make sure you use the insert or cell formatting as text not number than it should allow you to use leading zero

string text  = "'0001"
 Excel.Range formatRange;
    formatRange.NumberFormat = "@";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top