前言
本篇主要是设置单元格的时间内容,提供了3种方法。
实例
public static void main(String[] args) throws Exception { //定义工作簿 Workbook wb= new HSSFWorkbook(); //定义sheet Sheet sheet = wb.createSheet("my sheet"); //创建一行 Row row = sheet.createRow(0); //创建第一列 Cell cell = row.createCell(0); //设置单元格时间 cell.setCellValue(new Date()); //创建工具 CreationHelper helper = wb.getCreationHelper(); //单元格样式类 CellStyle cellStyle = wb.createCellStyle(); //设置单元格格式 cellStyle.setDataFormat(helper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss")); //创建第二列 cell = row.createCell(1); //设置单元格内容 cell.setCellValue(new Date()); //设置单元格样式 cell.setCellStyle(cellStyle); //创建第三列 cell=row.createCell(2); //设置内容 cell.setCellValue(Calendar.getInstance()); cell.setCellStyle(cellStyle); FileOutputStream fileout = new FileOutputStream("f:\\poi\\时间.xlsx"); wb.write(fileout); fileout.close(); }总结:设置时间之后,不清楚是否因为单元格长度太段,导致时间会显示###,需要拉长单元格才显示时间。