您的位置:首页 > 编程语言 > Java开发

java POI创建Excel单元格并填充时间数据

2017-01-11 15:43 671 查看
//Workbook wb = new XSSFWorkbook();
//FileOutputStream fileOut = new FileOutputStream("E:\\standarcode\\workbook.xlsx");
Workbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("E:\\standarcode\\workbook.xls");
Sheet sheet1 =  wb.createSheet("new sheet");
//创建一行
Row row=sheet1.createRow(0);
//第一个单元格,没有格式化
Cell cell = row.createCell(0);
cell.setCellValue(new Date());

//创建格式,可用于单元格
CreationHelper createHelper = wb.getCreationHelper();
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);

//写到文件里面,并释放资源
wb.write(fileOut);
fileOut.close();
wb.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: