以双格式获取日期 43318.4847916667 而不是 06-08-2018 11:38:06

Getting date in double format 43318.4847916667 instead of 06-08-2018 11:38:06

我正在编写 java 代码以使用 HSSFWorkbook(.xls) 生成 excel 文件。我需要以 06-08-2018 11:38:06 这种格式在一栏中写日期,但它生成的是这样的 43318.4847916667.
这是我使用的代码片段。 请帮助我如何才能成功编写

Workbook workbook = new HSSFWorkbook();
Sheet excelSheet = workbook.createSheet();
Row dataRow = excelSheet.createRow(1);;
Cell dataCell = dataRow.createCell(1);;
CellStyle cStyle = workbook.createCellStyle();
CreationHelper createHelper = workbook.getCreationHelper();         
cStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
dataCell.setCellValue(new Date());
dataCell.setCellStyle(cStyle);

无法重现问题。

以下完整示例生成一个 Excel 工作簿,该工作簿具有工作表和单元格 B2 中格式正确的日期。

代码:

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("yyyy-MM-dd HH:mm:ss"));

   Sheet excelSheet = workbook.createSheet();
   Row dataRow = excelSheet.createRow(1);;
   Cell dataCell = dataRow.createCell(1);;
   dataCell.setCellValue(new java.util.Date());
   dataCell.setCellStyle(cStyle);

   excelSheet.setColumnWidth(1, 25 * 256);

   workbook.write(fileout);
  }

 }
}

结果:


从你的评论来看,你的问题没有体现出全部。行有一个循环,每一行都应创建一个日期。这只适用于第 42 行。

嗯,这个问题是众所周知的。 Excel 每个工作簿的单元格样式数有限制。参见:Excel specifications and limits.

因此,如果您在循环中反复创建新的单元格样式,有时会达到该限制。但是,您不必总是在循环中创建新的单元格样式。单元格样式存储在工作簿级别。只需在循环外创建任何需要的单元格样式。然后仅将先前创建的单元格样式应用于循环内的单元格。

以下对我有效并创建了 1000 个正确格式的日期:

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("yyyy-MM-dd HH:mm:ss"));

   Sheet excelSheet = workbook.createSheet();

   for (int r = 1; r < 1000; r++) {
    Row dataRow = excelSheet.createRow(r);;
    Cell dataCell = dataRow.createCell(1);;
    dataCell.setCellValue(new java.util.GregorianCalendar(2019, 9, r));
    dataCell.setCellStyle(cStyle);
   }

   excelSheet.setColumnWidth(1, 25 * 256);

   workbook.write(fileout);
  }

 }
}

如果您想要“06-08-2018 11:38:06”,那么您的日期格式应该是 "dd-MM-YYYY HH:mm:ss"。否则,您只是缺少需要将工作簿写入其中的 FileOutputStream

private static final String FILE_NAME = "./out/MyFirstExcel.xls";

public static void main(String ... args) {
    Workbook workbook = new HSSFWorkbook();
    Sheet excelSheet = workbook.createSheet();
    Row dataRow = excelSheet.createRow(1);
    Cell dataCell = dataRow.createCell(1);
    CellStyle cStyle = workbook.createCellStyle();
    CreationHelper createHelper = workbook.getCreationHelper();
    cStyle.setDataFormat(createHelper.createDataFormat().getFormat("dd-MM-YYYY HH:mm:ss"));
    dataCell.setCellValue(new Date());
    dataCell.setCellStyle(cStyle);

    try {
        File file = new File(FILE_NAME);
        if(!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file);
        workbook.write(outputStream);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

按照以下代码以 DD-MM-YYYY HH:MM:ss.

格式打印日期
String fileName = "myExcel.xls";
WritableWorkbook writableWorkbook = null;
response.setContentType("application/vnd.ms-excel");
writableWorkbook = Workbook.createWorkbook(response.getOutputStream());
WritableSheet excelOutputsheet = writableWorkbook.createSheet("Sheet1", 0);

DateFormat customDateFormatWithTime = new DateFormat("dd-MM-yyyy HH:mm:ss");
WritableCellFormat dateFormatWithTime = new 
WritableCellFormat(customDateFormatWithTime);

int row = 0;
int col = 0;
excelOutputsheet.setColumnView(col, 20);
Label lable1 = new Label(col, row, "Date and Time", cellFormat);
excelOutputsheet.addCell(lable1);

row = row + 1;
col = col + 1;
DateTime myDate = new jxl.write.DateTime(col, row, "Value", dateFormatWithTime);
excelOutputsheet.addCell(myDate);