Quantcast
Channel: ClosedXML - The easy way to OpenXML
Viewing all articles
Browse latest Browse all 1877

Patch Uploaded: #15406

$
0
0

das974 has uploaded a patch.

Description:
This library is great! Great work and thank you!
I am not sure the correct way upload a patch, so I have submitted the file I modified. If you need something else (like the entire solution), please let me know.
Anyways, I have an issue with reading dates that are formatted with a non local format.
Steps:
1) Create Excel file - in cell A1 enter date 5/5/2013
2) Format cell using dd-mmm-yy
3) Read date using the following code:
XLWorkbook wbk = new XLWorkbook(@"Sample.xlsx");
IXLWorksheet sheet = wbk.Worksheets.FirstOrDefault();
DateTime val = sheet.Cell("A1").GetDateTime();

Expected:
val is datetime with value 5/5/2013
Actual:
Error thrown from ClosedXML.Excel.XLCell - function T GetValue<T>
"Cannot convert cell value to System.DateTime"

I believe that the error is because of the locale ID contains a zero which is pre-pended to the number format. In the attached Sample.zip file, you will find the following number format in styles.xml:

<numFmt numFmtId="165" formatCode="[$-409]d\-mmm\-yy;@" />

The string "[$-409]" (the English locale) is expected as described here: http://office.microsoft.com/assistance/hfws.aspx?AssetID=HA010346351033

According to the article - "If you specify a different locale before you select a format, a locale component will be added to the code, in brackets, and the resulting format code may look like this: [$-412]m/d/yy"

This causes the method XLCellValues GetDataTypeFromFormat(String format) in the ClosedXml.Excel.XLWorkbook class to return number as the type.

I modified the function to detect the locale string and skip it, but I feel like there should be a way to get the type from the OpenXml API. Is that not true? It seems like the Excel app knows when numbers are stored as text so I would think (hopefully) there should be some standard way of doing this through the API. I would be glad to research this, though for the short term, I think this patch would suffice as a work-around.


Viewing all articles
Browse latest Browse all 1877

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>