问题
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?
回答1:
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.
回答2:
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.

回答3:
The format that I needed was "@" so the line of code was
codeSheet.Cells["A:D"].Style.Numberformat.Format = "@";
回答4:
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"
回答5:
Excel.Range formatRange;
formatRange.NumberFormat = "@";
来源:https://stackoverflow.com/questions/19098569/allow-leading-zeros-on-a-worksheet-with-ep-plus