我必须消耗一些xlsx
文件。我已经阅读了使用 open xml sdk和http://www.dotnetperls.com/fromoadate从 xlsx 读取日期。我的大部分专栏都是文本(共享字符串),但也有一些数字(整数),还有一些日期和日期时间。我正在使用 OpenXML SDK 2.5。
我的问题是我不知道如何区分实际数字和日期。它们都具有DataType
of null
,并且文本数字表示在Text
单元格的属性中。
一些代码:
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();