提到的日期格式(dd/MM/yyyy)在使用Apache Poi时无法用于当前日期的Excel。

huangapple go评论67阅读模式
英文:

Mentioned date format (dd/MM/yyyy) is not working for current date in excel using Apache Poi

问题

我需要生成带有日期值的 Excel 文件,使用 HSSFWorkbook(.xlsx)。我传递了 Date 类生成的日期值,同时我还在 Excel 单元格中添加了格式为 dd/MM/yyyy 的格式,使用 createHelper.createDataFormat().getFormat("dd/MM/yyyy"),但在下载的 Excel 文件中显示的格式不同。

传递的日期为 Wed Apr 19 15:00:17 IST 2023,我期望的格式是 19/04/2023,但实际上显示如图所示。

提到的日期格式(dd/MM/yyyy)在使用Apache Poi时无法用于当前日期的Excel。

附上代码片段,

Workbook wb = new HSSFWorkbook();  
CreationHelper createHelper = wb.getCreationHelper();  
Sheet sheet = wb.createSheet("New Sheet");  
Row row     = sheet.createRow(0);  
Cell cell   = row.createCell(0);  
cell.setCellValue("test");
CellStyle cellStyle = wb.createCellStyle();  
cellStyle.setDataFormat(  
            createHelper.createDataFormat().getFormat("dd/MM/yyyy"));  
cell = row.createCell(1); 
Date date = new Date();
System.out.println("Date :"+date);
cell.setCellValue(date);    
cell.setCellStyle(cellStyle);

谢谢

英文:

I need to generate excel file with date values using HSSFWorkbook(.xlsx). I am passing date value generated by Date class and also I am adding the format to the excel cell as dd/MM/yyyy using createHelper.createDataFormat().getFormat("dd/MM/yyyy") but in the downloaded excel file it is showing in different format.

passing date as Wed Apr 19 15:00:17 IST 2023,
I am expecting the format as 19/04/2023 however I am getting as shown in the image.

提到的日期格式(dd/MM/yyyy)在使用Apache Poi时无法用于当前日期的Excel。

Attached the code snippets,

Workbook wb = new HSSFWorkbook();  
CreationHelper createHelper = wb.getCreationHelper();  
Sheet sheet = wb.createSheet("New Sheet");  
Row row     = sheet.createRow(0);  
Cell cell   = row.createCell(0);  
cell.setCellValue("test");
CellStyle cellStyle = wb.createCellStyle();  
cellStyle.setDataFormat(  
            createHelper.createDataFormat().getFormat("dd/MM/yyyy"));  
cell = row.createCell(1); 
Date date = new Date();
System.out.println("Date :"+date);
cell.setCellValue(date);    
cell.setCellStyle(cellStyle);

Thanks

答案1

得分: 3

以下是已经翻译好的部分:

"Excel"的日期格式DD/MM/YYYY表示一个DMY日期格式,其分隔符定义在系统区域设置中。对于您的系统来说,分隔符似乎是连字符(减号)。换句话说,在该格式代码中的/并不表示斜杠,而是默认日期分隔符。

请查看控制面板 - 区域设置,查看日期(简短)中的分隔符。这是默认的日期分隔符。

如果您想设置一个始终使用斜杠作为分隔符的用户定义格式,那么您需要将斜杠标记为无意义的字符。这可以是DD\/MM\/YYYY - 前置反斜杠,或DD"/"MM"/"YYYY - 用双引号括起来的斜杠。

完整示例:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelDate {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("./Excel.xls") ) {

   CellStyle cStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();         
   //cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd/MM/yyyy"));
   //cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd\\/MM\\/yyyy"));
   cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd\"/\"MM\"/\"yyyy"));

   Sheet excelSheet = workbook.createSheet();
  
   for (int r = 1; r < 10; r++) {
    Row dataRow = excelSheet.createRow(r);
    Cell dataCell = dataRow.createCell(1);
    dataCell.setCellValue(new java.util.GregorianCalendar(2023, 3, r*2, r, r, r));
    dataCell.setCellStyle(cStyle);
   }

   excelSheet.setColumnWidth(1, 15 * 256);

   workbook.write(fileout);
  }

 }
}

关于此问题的文档链接如下:

数字格式代码

以及有关区域设置的信息:

数字格式化

但由于我们在文件存储级别使用Apache POI,而不是在Excel GUI级别,还需要知道以下信息:

Microsoft Office始终在文件中存储en_US设置。区域化仅在GUI中进行。

因此,对于数字格式,这意味着:

在文件中存储的数字格式#,##0.00在使用逗号作为小数分隔符和点作为千位分隔符的区域设置的GUI中可能是#.##0,00

在文件中存储的日期格式DD/MM/YYYY在使用点作为日期分隔符的区域设置的GUI中可能是DD.MM.YYYY。也可能是TT.MM.JJJJ,当该区域的语言中,“Day”对应“Tag”,“Year”对应“Jahr”。

如果将DD/MM/YYYY放入区域设置的GUI中,其日期分隔符不是斜杠,则在文件存储中将存储DD\/MM\/YYYY以标记斜杠为无意义字符。但由于Apache POI直接写入文件存储,因此需要直接使用DD\/MM\/YYYY,否则斜杠表示默认的日期分隔符。

英文:

The Excel Number format DD/MM/YYYY means a date format DMY having the delimiter which is defined in Systems region settings. Seems for your system that delimiter is hyphen (minus). In other words, the / in that format code does not mean the slash but the default date delimiter.

Have a look at Control Panel - Region what is the delimiter in Date (short). This is the default date delimiter.

If you want set a user defined format which always uses the slash as the delimiter, then you need mark the slash as meaningless character. That would be DD\/MM\/YYYY - backslash preceded or DD&quot;/&quot;MM&quot;/&quot;YYYY - slash in double quotes.

Complete Example:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

class CreateExcelDate {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new HSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream(&quot;./Excel.xls&quot;) ) {

   CellStyle cStyle = workbook.createCellStyle();
   CreationHelper createHelper = workbook.getCreationHelper();         
   //cStyle.setDataFormat(createHelper.createDataFormat().getFormat(&quot;dd/MM/yyyy&quot;));
   //cStyle.setDataFormat(createHelper.createDataFormat().getFormat(&quot;dd\\/MM\\/yyyy&quot;));
   cStyle.setDataFormat(createHelper.createDataFormat().getFormat(&quot;dd\&quot;/\&quot;MM\&quot;/\&quot;yyyy&quot;));

   Sheet excelSheet = workbook.createSheet();
  
   for (int r = 1; r &lt; 10; r++) {
    Row dataRow = excelSheet.createRow(r);;
    Cell dataCell = dataRow.createCell(1);;
    dataCell.setCellValue(new java.util.GregorianCalendar(2023, 3, r*2, r, r, r));
    dataCell.setCellStyle(cStyle);
   }

   excelSheet.setColumnWidth(1, 15 * 256);

   workbook.write(fileout);
  }

 }
}

When it comes to the question about documentation for this, there is:

Number format codes

and for regionalization:

Number formatting

But since we are on file storage level using Apache POI and not on Excel GUI level, following needs also to be known:

Microsoft Office always stores en_US settings in files. The regionalization is done in GUI only.

So for number formats that means:

A stored number format #,##0.00 in a file may be #.##0,00 in a GUI which uses regional settings where comma is decimal delimiter and dot is thousands separator.

A stored date format DD/MM/YYYY in a file may be DD.MM.YYYY in a GUI which uses regional settings where dot is the date separator. It also may be TT.MM.JJJJ when "Day" spells "Tag" and "Year" spells "Jahr" in that regions language.

If one put DD/MM/YYYY in a GUI which region settings have not slash as the date separator, then in file storage DD\/MM\/YYYY will be stored to mark that slash as a meaningless character. But as Apache POI directly writes in file storage, there DD\/MM\/YYYY needs to be used directly. Else slash means the default date separator.

huangapple
  • 本文由 发表于 2023年4月19日 18:29:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76053442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定