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

标签 java excel apache date apache-poi

我在 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/

相关文章:

java - 我无法在表格 View 内设置按钮宽度

JavaFX:将图像插入 GridPane

php - WAMP 服务器上用于 PHP 5.6 的 Sqlsrv

mysql - Node.js MySQL - 错误 : connect ECONNREFUSED

php - 用户在页面上花费的时间

java - 从Eclipse推送到Github Repo,如何 stash 敏感数据,但公开repo?

java - 读取值以插入二维数组

VBA相当于hasext方法?

python - 使用 xlwings 在 excel 中的函数调用之间保留 python 全局变量

vba - 按接收日期指定从 Outlook 获取电子邮件到 Excel