简体   繁体   中英

Unable to format Date cell in exported excel using Apache POI

I am using Workbook wb = new XSSFWorkbook(); for exporting an excel sheet, which contains a column which is a DATE . Even after setting the style as DATE , the column is shown as GENERAL in the Excel sheet.

Here is my piece of code for cell creation

Workbook wb = new XSSFWorkbook(); 
int rowValue = 1; CellStyle cellStyleOfHeaderRow = wb.createCellStyle(); CellStyle style = wb.createCellStyle(); 
Font fontOfCell = initializeCellFont(wb, cellStyleOfHeaderRow); initializeCellBorders(cellStyleOfHeaderRow); initializeCellFillOptions(cellStyleOfHeaderRow); 
initializeCellBorders(style); CreationHelper createHelper = wb.getCreationHelper(); 
    short dateFormat = createHelper.createDataFormat().getFormat("yyyy-MM-dd-hh.mm.ss"); 
    style.setDataFormat(dateFormat);

After opening the exported excel, when I try to change the format of the date column from GENERAL to DATE , I am unable to do so.

在此处输入图片说明

Could you suggest some piece of code or any solution to this?

I suspect your data 2016-01-28 12:06:00.0 , ... are Strings rather than Dates. If you set strings in a Excel cell, then the cell cannot be a date cell. The cell value must be a numeric value to let the cell be a date cell. So you needs converting that String s to Date s before setting the cell value. Then set that Date as the cell value.

Using current apache poi 4.1.2 this can be done using java.time.format.DateTimeFormatter and java.time.LocalDateTime since there is Cell.setCellValue(java.time.LocalDateTime value) now.

Up to apache poi 3.17 it can be done using java.text.SimpleDateFormat and java.util.Date . Cell.setCellValue(java.util.Date value) is the setCellValue method used then.

Complete example:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class CreateExcelDateCells {

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

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   String[][] data = new String[][] {
    new String[] {"Date"},
    new String[] {"2016-01-28 12:06:00.0"},
    new String[] {"2016-01-27 08:29:00.0"},
    new String[] {"2016-01-18 21:37:00.0"}
   };

   DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S", Locale.US);
   //SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.US); // up to apache poi 3.17

   CellStyle dateCellStyle = workbook.createCellStyle();
   dateCellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

   Sheet sheet = workbook.createSheet(); 

   for (int r = 0; r < data.length; r++) {
    Row row = sheet.createRow(r);
    Cell cell = row.createCell(0);
    if (r == 0) {
     cell.setCellValue(data[r][0]); // String cell value
    } else {
     cell.setCellValue(LocalDateTime.parse(data[r][0], dateTimeFormatter)); // Date cell value
     //cell.setCellValue(simpleDateFormatter.parse(data[r][0])); // Date cell value up to apache poi 3.17
     cell.setCellStyle(dateCellStyle);
    }
   }

   sheet.autoSizeColumn(0);

   workbook.write(fileout);
  }

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM