2

我正在编写一个 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);
4

3 回答 3

2

无法重现问题。

下面的完整示例生成一个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 规范和限制

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

以下对我有用,并创建了 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);
  }

 }
}
于 2019-10-24T11:53:52.230 回答
0

按照以下代码在 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);
于 2019-10-24T12:39:52.427 回答
0

如果您想要“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();
    }
}
于 2019-10-24T12:04:13.647 回答