I am reading data from an Excel worksheet. Some of the data is percent values and these values get converted to decimals automatically — ie 90% –> 0.90. I can’t seem to find anyway to keep this from happening. Also, aren’t all the data on the Excel sheet strings? So why would number formatting be happening at all?
Any advice is appreciated.
Regards.
Check the Excel worksheet – if the cells are formatted as
Percentagethen the data is only displayed as, eg:90.0%, but is stored as0.9.If you check the cell format before importing the number then you can handle any conversions required.
Assuming you are using Interop.Excel :
The
Range.NumberFormatproperty returns the same format strings you would use in theFormat Cellsdialog box in Excel.Percentageis just a shortcut to a format string of the type0.00%where thedecimal placesselected in thePercentagetype simply alters the number of zeroes after the decimal point in the format string.MSDN – Range.NumberFormat
EDIT
Example of how to find the format of a range of cells :
Where
nfmtwill contain a format string which you can check for%to determine whether the cell (or range) is formatted to display a fraction as a percentage. Note thatnfmtwill beNULLif the range spans cells with different format strings!