使用开放XML SDK从XLSX读取日期 [英] Reading a date from xlsx using open xml sdk

查看:136
本文介绍了使用开放XML SDK从XLSX读取日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在格式2011/4/5(月/日/年)在一个xlsx档案中的一个细胞的日期。 。我试着去解析文件和一些类加载这些数据。

I have a date in format "4/5/2011" (month/day/year) in a xlsx file in one of the cells. Im trying to parse the file and load those data in some classes.

到目前为止,我解析细胞看起来像这样的部分:

So far the part where I parse the cell looks like this:

string cellValue = cell.InnerText;
if (cell.DataType != null)
{
    switch (cell.DataType.Value)
    {
        case CellValues.SharedString:
            // get string from shared string table
            cellValue = this.GetStringFromSharedStringTable(int.Parse(cellValue));
            break;
    }
}



我希望日将是一个cell.DataType。事实是解析与日期2011/4/5的单元时,cell.DataType的值是零和单元的值是40638,它是不是索引到共享字符串表。 (我以前试过了,它结束了一个例外。)

I hoped that date would be a cell.DataType. The truth is when parsing the cell with the date "4/5/2011", the value of cell.DataType is null and the value of the cell is "40638" and it is not an index to the shared string table. (I have tried that before and it ended up with an exception.)

任何想法?
谢谢

Any ideas? Thanks

推荐答案

打开XML保存日期,天数,从1月1日1900年好多少,跳过不正确29 1900年2月作为一个有效的一天。你应该能够找出算法来帮助您计算正确的值。我相信,一些开发商利用 DateTime.FromOADate()作为帮手。

Open XML stores dates as the number of days from 1 Jan 1900. Well, skipping the incorrect 29 Feb 1900 as a valid day. You should be able to find out algorithms to help you calculate the correct value. I believe some developers use DateTime.FromOADate() as a helper.

此外,单元类有数据类型财产数目默认情况下。所以,如果它为空,这是一个数字,其中包括我们的情况下日期。

Also, the Cell class has the DataType property as Number by default. So if it's null, it's a number, which includes dates in our case.

您只能去共享字符串表时存储的日期是纪元前(1月1日1900在这种情况下)。然后在这种情况下,细胞类的CellValue保持索引到共享字符串表

You only go to the shared strings table when the date stored is before the epoch (1 Jan 1900 in this case). And then in that case, the CellValue of the Cell class holds the index to the shared string table.

这篇关于使用开放XML SDK从XLSX读取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆