I'm working with the OpenXML SDK at the moment and I've got a problem with the value shown in Excel compared to the value in OpenXML.
Basically I have an excel Spreadsheet where the value in B4 is "202". But when I read the value from the spreadsheet using OpenXml the value I receive is "201.99999999999997".
I've written a test app to check the values, and code I'm using to read the Excel file using OpenXml is as follows:
using (SpreadsheetDocument document = SpreadsheetDocument.Open(_excelFile1, false))
{
WorkbookPart wbPart = document.WorkbookPart;
var sheet = wbPart.Workbook.Descendants<Sheet>().First();
var wsPart = (WorksheetPart)(wbPart.GetPartById(sheet.Id));
Cell cell1 = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "B4").FirstOrDefault();
Cell cell2 = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "C4").FirstOrDefault();
Cell cell3 = wsPart.Worksheet.Descendants<Cell>().Where(c => c.CellReference == "D4").FirstOrDefault();
Console.WriteLine(String.Format("Cell B4: {0}", GetCellValue(cell1)));
Console.WriteLine(String.Format("Cell C4: {0}", GetCellValue(cell2)));
Console.WriteLine(String.Format("Cell D4: {0}", GetCellValue(cell3)));
}
private static string GetCellValue(Cell cell)
{
if (cell != null) return cell.CellValue.InnerText;
else return string.Empty;
}
Then the value I get is as follows:
Cell B4: 201.99999999999997
[UPDATE - RAW XML FROM XLSX FILE]
<row r="4" spans="1:4" x14ac:dyDescent="0.2">
<c r="A4" s="2">
<v>39797</v>
</c>
<c r="B4" s="3">
<v>201.99999999999997</v>
</c>
<c r="C4" s="3">
<v>373</v>
</c>
<c r="D4" s="3">
<v>398</v>
</c>
</row>
So as you can see from the XLSX file XML, OpenXML is reading the correct value. However, Excel must be applying some kind of formatting to display the value "201.99999999999997" as "202". This is the value the users see so this is the value they're expecting when it's read from the XLSX file using OpenXML.
So now I'm wondering if anyone knows of any formatting that's applied by Excel to get the value as the user sees it in the spreadsheet, but using OpenXML.
As a test I've tried recreating the spreadsheet, but sometimes the values appear the same as a decimal when a whole number has been entered into the spreadsheet. It's very intermittent and I can't explain or fix the issue.
GetCellValueis "your own" method. Please provide the implementation for this method as well.