gpt4 book ai didi

java - org.apache.poi 使​​用日期单元格创建 excel 工作簿

转载 作者:行者123 更新时间:2023-11-29 06:34:29 24 4
gpt4 key购买 nike

我在 Java 中有这种格式的日期:“2014.01.31 14:24:28”。我想使用单元格格式类型为“日期”的 apache poi 库将它放入 excel 中。我怎样才能做到这一点?

最佳答案

检查 here : 以您想要的格式更改日期并使用它。

Workbook wb = new HSSFWorkbook();
//Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow(0);

// Create a cell and put a date value in it. The first cell is not styled
// as a date.
Cell cell = row.createCell(0);
cell.setCellValue(new Date());

// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(
createHelper.createDataFormat().getFormat("yyyy/mm/dd hh:mm:ss"));
cell = row.createCell(1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);

//you can also set date as java.util.Calendar
cell = row.createCell(2);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();

关于java - org.apache.poi 使​​用日期单元格创建 excel 工作簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24057886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com