EXCELL单元格样式问题 [英] Excell Cell Style issue

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

问题描述

我使用低于code摆脱XLSX文件日期值。这是工作绝对罚款有些XLSX文件。但它不会给确切的日期格式,这是XLSX文件。这个问题是对一些文件。

例如;我有约会这样的 21/01/2016(日/月/年)
但看完后,它给人日期 16年1月21日(MM / DD / YY)

是否有任何其他方式来获得cellstyle?

它是XLSX文件的问题??

 字符串dateFmt = cell.getCellStyle()getDataFormatString()。如果(DateUtil.isCellDateFormatted(单元)){
    双VAL = cell.getNumericCellValue();
    日期日期= DateUtil.getJavaDate(VAL);    字符串dateFmt = cell.getCellStyle()getDataFormatString()。    的System.out.println(dateFmt+ dateFmt);    值=新CellDateFormatter(dateFmt).format(日期);    的System.out.println(日期+值);}


解决方案

如果这是一个包含日期的单元格,被格式化为默认的日期格式(短日期 ),则仅格式ID 0xE(14)被存储在文件中。请参见 https://poi.apache.org/apidocs/组织/阿帕奇/ POI / SS /的usermodel / BuiltinFormats.html 。在 *。XLSX 文件只包含

< XF numFmtId =14...... applyNumberFormat =1/>

styles.xml 。有没有特别的格式code 保存这个 numFmtId

那么这将如何在 Excel中显示依赖于系统的区域设置。

例如我的德语版Windows系统中的 numFmtId =14将显示为 TT.MM.JJJJ 如设在区域和语言设置:

在这里输入的形象描述

在英国大不列颠Windows系统,这将是 DD / MM / YYYY 每默认值。

但是,如果您更改短日期在系统设置中,设置为 JJJJ-MM-TT 的例如,那么这种格式也将与 numFmtId =14。在Excel中显示

因此​​,要知道Excel将如何准确显示与 numFmtId =14之日起,人们需要知道确切的Windows系统设置区域和语言

所以也Apache的POI不知道这个应该怎么不知道系统的区域设置进行显示,因为文件中没有这个信息来源。所以它会假设 EN-US 区域设置。这导致 M / D / YY 日期。

您可以检查格式ID 14被使用,如果是这样定义自己的默认日期格式。

 如果(DateUtil.isCellDateFormatted(细胞)){
    日期日期= cell.getDateCellValue();    的System.out.println(日期);    串dateFmt =;    如果(cell.getCellStyle()。getDataFormat()== 14){//默认短日期没有明确的格式
     dateFmt =DD / MM / YYYY;为此//默认日期格式
    }其他{//其他数据格式有明确的格式
     dateFmt = cell.getCellStyle()getDataFormatString()。
    }    的System.out.println(dateFmt+ dateFmt);    字符串值=新CellDateFormatter(dateFmt).format(日期);    的System.out.println(日期+值);   }

需要明确的是:这一切都只能是 numFmtId =14 cell.getCellStyle()getDataFormat()== 14 。所有其他的日期格式将有明确的数据格式字符串 cell.getCellStyle()。getDataFormatString()等POI可以在Excel中显示的正是他们像。

I am using below code to get date value from XLSX file. This is working absolutely fine for some xlsx file. But It is not giving exact date format which is in xlsx file. This issue is for some file.

eg; I have date like this 21/01/2016 (dd/mm/yyyy) but after reading, it gives date as 01/21/16(mm/dd/yy)

Is there any other way to get cellstyle?

Is it issue of xlsx file??

String dateFmt = cell.getCellStyle().getDataFormatString();

if (DateUtil.isCellDateFormatted(cell)) {
    double val = cell.getNumericCellValue();
    Date date = DateUtil.getJavaDate(val);

    String dateFmt = cell.getCellStyle().getDataFormatString();

    System.out.println("dateFmt "+dateFmt);

    value = new CellDateFormatter(dateFmt).format(date);

    System.out.println("Date "+value);

}

解决方案

If the cell, which is containing the date, is formatted as the default date format (Short Date), then only the format id 0xE (14) is stored in the file. See https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/BuiltinFormats.html. The *.xlsx file contains only

<xf numFmtId="14" ... applyNumberFormat="1"/>

in styles.xml. There is no special formatCode saved for this numFmtId.

So how this will be displayed in Excel depends on the locale settings of the system.

For example with my German Windows system the numFmtId="14" will be displayed as TT.MM.JJJJ as is set in Region and Language settings:

In English Great Britain Windows systems this will be DD/MM/YYYY per default.

But if you change the setting for Short Date in the system settings, to JJJJ-MM-TT for example, then this format will also displayed in Excel with the numFmtId="14".

So to know how exactly Excel will display the date with the numFmtId="14", one needs to know the exact Windows system settings in Region and Language.

So also apache POI can't know how this should be displayed without knowing the locale settings of the system, since the file contains no infomation about this. So it will assume en-us locale. This leads to m/d/yy for date.

You could check if the format id 14 is used and if so define your own default date format.

   if (DateUtil.isCellDateFormatted(cell)) {
    Date date = cell.getDateCellValue();

    System.out.println(date);

    String dateFmt = "";

    if (cell.getCellStyle().getDataFormat() == 14) { //default short date without explicit formatting
     dateFmt = "dd/mm/yyyy"; //default date format for this
    } else { //other data formats with explicit formatting
     dateFmt = cell.getCellStyle().getDataFormatString();
    }

    System.out.println("dateFmt " + dateFmt);

    String value = new CellDateFormatter(dateFmt).format(date);

    System.out.println("Date " + value);

   }

To be clear: This all is only with numFmtId="14" cell.getCellStyle().getDataFormat() == 14. All other date formats will have explicit data format strings cell.getCellStyle().getDataFormatString() and so POI can display them exactly like in Excel.

这篇关于EXCELL单元格样式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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