Allow leading zeros on a worksheet with EP Plus

时光怂恿深爱的人放手 提交于 2019-12-12 11:06:34

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!