Apache的POI时间单元 [英] Apache POI Time Cell

查看:204
本文介绍了Apache的POI时间单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测,单元格的格式是日期,时间或日期时间。结果
我的code是:

I need to detect, whether cell format is Date, Time or Datetime.
My code is:

        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
            if (DateUtil.isCellDateFormatted(cell)) {
                if (   ???   )
                    return "Time";
                else if (   ???   )
                    return "Date";
                else
                    return "Datetime";
            }
        }

我用ApachePOI 3.6

I use ApachePOI 3.6

推荐答案

我没有看到一个内置的方式POI做到这一点很容易。首先,似乎没有被时间,日期和日期时间之间有明显的区别。这些值只是作为数字存储和格式应用到显示它。

I don't see a built-in way to do this easily in POI. First, there doesn't seem to be a clear distinction between "Time", "Date", and "Datetime". The values are just stored as numbers and a format is applied to display it.

有一件事情你可以做的是让细胞( HSSFCellStyle ),然后写你自己的方法的风格,无论是读取 style.getDataFormatString( ) style.getDataFormat()并告诉你格式是否属于时间,日期或日期时间类别根据你如何定义它们。第二种方法返回一个 INT 所以它可能是更容易的工作。

One thing you could do is get the style of the cell (HSSFCellStyle), then write your own method that reads either style.getDataFormatString() or style.getDataFormat() and tells you whether the format falls in the "Time", "Date", or "Datetime" category depending on how you define them. The second method returns an int so it might be easier to work with.

例如,下面的格式具有突出的字符串数据格式为[$ -F400] H:MM:SS \\ AM / PM和一个 INT 价值166。

For example, the format highlighted below has a String data format of "[$-F400]h:mm:ss\ AM/PM" and an int value of 166.

我假设这是你所说的日期时间的格式是什么意思,因为它的Excel内置的格式之一,显示双方的日期和时间。

I'm assuming this is what you mean by "Datetime" format, as it's one of Excel's built-in formats that displays both the date and the time.

这种方法的缺点是,我不会期望它具有自定义日期/时间格式细胞工作。

The drawback of this approach is that I wouldn't expect it to work with cells that have custom date/time formats.

这篇关于Apache的POI时间单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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