I have to consume some xlsx
documents. I've read Reading a date from xlsx using open xml sdk and http://www.dotnetperls.com/fromoadate. Most of my columns are texts (shared strings), but there are some numbers (integer numbers), and I have also some dates and date-times.
I'm using OpenXML SDK 2.5.
My problem is that I don't know how to distinguish the actual numbers from the dates. Both of them has DataType
of null
, and the textual number representation is in the Text
property of the cell.
Some code:
using (var xlsxStream = assembly.GetManifestResourceStream("Checklist.xlsx"))
using (var spreadsheetDocument = SpreadsheetDocument.Open(xlsxStream, false))
{
var workbookPart = spreadsheetDocument.WorkbookPart;
var sharedStringTable = workbookPart.SharedStringTablePart.SharedStringTable;
var worksheetPart = workbookPart.WorksheetParts.First();
var sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
string text;
foreach (Row r in sheetData.Elements<Row>())
{
foreach (Cell c in r.Elements<Cell>())
{
if (c.CellValue != null)
{
text = c.CellValue.Text;
if (c.DataType != null)
{
if (c.DataType.Value == CellValues.SharedString)
{
int tableIndex = int.Parse(text);
text = sharedStringTable.ChildElements[tableIndex].InnerText;
}
// note: the date cells do not have c.DataType.Value == CellValues.Date
// Their c.DataType is null, if they are OLE Automation date numbers
}
// So here I am, and I'd need to know if the number supposed to be an
// OLE Automation date or a number, so I can transform it if needed.
//if (it's a date) // <- ?????
//{
// double dateDouble = double.Parse(text);
// DateTime dateTime = DateTime.FromOADate(dateDouble);
// text = dateTime.ToShortDateString();
//}
Console.Write(text + " ");
}
else
{
Console.Write("NULL" + " ");
}
}
Console.WriteLine();
}
Console.WriteLine();
Console.ReadKey();