版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/my773962804/article/details/84878103
工作中用到了Excel导出,被迫学习了一下poi,文章都是看视频个人的记录。
poi版本:
<!--excel--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency>
/**
* @ClassName 类名:ExcelDemo1
* @Author作者: hzh
* @Date时间:2018/12/4 8:44
**/
public class ExcelDemo1 {
public static void main(String[] args) throws Exception {
//获得一个工作薄
Workbook wb = new HSSFWorkbook();
//第一个sheet
Sheet sheet = wb.createSheet("第一个sheet");
//创建第一行
Row row = sheet.createRow(0);
//获取第一个单元格(第一列)
Cell cell = row.createCell(0);
//给第一个单元格赋值
cell.setCellValue("第一个单元格的值");
//第二列
cell = row.createCell(1);
cell.setCellValue(new Date());
CreationHelper creationHelper = wb.getCreationHelper();
CellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(creationHelper.createDataFormat().getFormat("yyyy-MM-dd hh:mm:ss"));
//第三列
cell = row.createCell(2);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
//第四列
cell = row.createCell(3);
cell.setCellValue(Calendar.getInstance());
cell.setCellStyle(cellStyle);
FileOutputStream fileOutputStream = new FileOutputStream("D:\\file\\工作薄.xls");
wb.write(fileOutputStream);
wb.close();
}