Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow leading zeros on a worksheet with EP Plus

Tags:

c#

excel

epplus

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?

like image 484
Jeff Finn Avatar asked Sep 30 '13 15:09

Jeff Finn


2 Answers

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.

like image 188
victor Avatar answered Sep 28 '22 06:09

victor


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

like image 28
Anthony Avatar answered Sep 28 '22 07:09

Anthony