在Excel中根据日期对数据进行排序

时间:2018-07-20 15:03:05

标签: java excel apache-poi

通过某些博客(其中只有https://blog.cometdocs.com/how-to-sort-data-by-date-in-excel个),我了解到要在excel中对Date列进行排序,month应该为整数格式,而不是文本格式(例如:jan或feb)。 我说得对吗?

默认情况下(除非您更改系统),日期格式将始终是mm / dd / yyyy?有任何文档支持吗?

1 个答案:

答案 0 :(得分:2)

The confusion comes from formatting Excel applies to a cell that will convert a number to look like a text date and date that is entered as text. visually there is no way to distinguish.

You can test the cell by say =ISNUMBER(cell) or =ISTEXT(cell) you can determine if its a date stored as a number but displayed as TEXT, or if it is just TEXT. An other test is changing the formatting for the cell to general and seeing if the cells displayed information changes in appearance.

If it is a number, you can simply sort the column and it will do so numerically. If it is not a number but text, it will sort the date alphabetically.

On a side note, the integer portion of the date represents number of days starting January 1st 1900 (on the mac this might be 1904). The decimal portion of the number represents time as a fraction of a day. 0.5 would be 12 noon.

As for the format, it will take your system settings for display purposes only. You can also apply a custom format to the excel cell for display purposes after loading. yy/mm/dd is the system I prefer to use. You have many options for formatting your custom display

yyyy - four digits of year
yy   - last 2 digits of year
m    - minimal digits for displaying month
mm   - leading zero included for single digit months
mmm  - 3 letters for month is displayed as text
mmmm - full name of month is displayed as text
d    - minimal digits for displaying day
dd   - leading zero included for single digit days
ddd  - 3 letters for day is displayed as text
dddd - full name of the day is displayed as text