Apache POl是一个处理Miscrosoft Ofice各种文件格式的开源项目。简单来说就是,我们可以使用 POI 在 Java 程序中对Miscrosoft Office各种文件进行读写操作,一般情况下,POI都是用于操作 Excel 文件。

1.银行网银系统导出交易明细
2.各种业务系统导出Excel报表
3.批量导入业务数据
-
- <dependency>
- <groupId>org.apache.poigroupId>
- <artifactId>poiartifactId>
- <version>3.16version>
- dependency>
- <dependency>
- <groupId>org.apache.poigroupId>
- <artifactId>poi-ooxmlartifactId>
- <version>3.16version>
- dependency>
- /**
- * 通过POI创建Excel文件并写入文件内容
- */
- public static void write() throws IOException {
- //在内存中创建一个文件
- XSSFWorkbook excel = new XSSFWorkbook();
- //在excel文件中创建一个sheet页
- XSSFSheet sheet = excel.createSheet("info");
- //在sheet页中创建行对象,rownum的编号从0开始
- XSSFRow row = sheet.createRow(1);
- //创建单元格并写入内容
- row.createCell(1).setCellValue("城市");
- row.createCell(2).setCellValue("姓名");
- //创建新行
- row = sheet.createRow(2);
- //创建单元格并写入内容
- row.createCell(1).setCellValue("北京");
- row.createCell(2).setCellValue("张三");
- //创建新行
- row = sheet.createRow(3);
- //创建单元格并写入内容
- row.createCell(1).setCellValue("北京");
- row.createCell(2).setCellValue("李四");
- //通过输出流将内存中的excel文件输出到磁盘
- FileOutputStream outputStream = new FileOutputStream(new File("B:\\info.xlsx"));
- excel.write(outputStream);
- //关闭资源
- outputStream.close();
- excel.close();
- }

- public static void read() throws IOException {
- //读取磁盘上已经存在的Excel文件
- XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("B:\\info.xlsx")));
- //读取Excel文件中的第一个Sheet页
- XSSFSheet sheet = excel.getSheetAt(0);
- //获取Sheet中的最后一行的行号
- int lastRowNum = sheet.getLastRowNum();
- for (int i =1;i<= lastRowNum;i++){
- //获取一行
- XSSFRow row =sheet.getRow(i);
- //获取单元格对象
- String cellValue1 = row.getCell(1).getStringCellValue();
- String cellValue2 = row.getCell(2).getStringCellValue();
- System.out.println(cellValue1 + " " +cellValue2);
- }
- }

如果excel的格式过于复杂可以先创建好模版,再将数据填入
- /**
- * 导出运营数据报表
- * @param response
- */
- public void exportBusinessData(HttpServletResponse response) {
- //1.查询数据库获取最近30天的运营数据
- LocalDate dateBegin = LocalDate.now().minusDays(30);
- LocalDate dateEnd = LocalDate.now().minusDays(1);
- //查询运营数据
- BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin,LocalTime.MIN),LocalDateTime.of(dateEnd,LocalTime.MAX));
-
- //通过POI将数据写入Excel文件中
- InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/1.xlsx");
-
- try {
- //基于模版文件创建一个新的Excel文件
- XSSFWorkbook excel = new XSSFWorkbook(in);
- XSSFSheet sheet = excel.getSheet("Sheet1");
- //填空数据
- //时间
- sheet.getRow(1).getCell(1).setCellValue("时间:"+dateBegin+"致"+dateEnd);
- //概览数据
- XSSFRow row = sheet.getRow(3);
- row.getCell(2).setCellValue(businessDataVO.getTurnover());
- row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());
- row.getCell(6).setCellValue(businessDataVO.getNewUsers());
-
- row = sheet.getRow(4);
- row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());
- row.getCell(4).setCellValue(businessDataVO.getUnitPrice());
- //明细数据
- for (int i =0;i<30;i++){
- LocalDate date = dateBegin.plusDays(i);
- //查询运营数据
- BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date,LocalTime.MIN),LocalDateTime.of(date,LocalTime.MAX));
- //获取某一行
- row = sheet.getRow(7+i);
- row.getCell(1).setCellValue(date.toString());
- row.getCell(2).setCellValue(businessData.getTurnover());
- row.getCell(3).setCellValue(businessData.getValidOrderCount());
- row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
- row.getCell(5).setCellValue(businessData.getUnitPrice());
- row.getCell(6).setCellValue(businessData.getNewUsers());
- }
- //获取一个输出流对象将excel下载的浏览器
- ServletOutputStream outputStream = response.getOutputStream();
- excel.write(outputStream);
- //关闭资源
- excel.close();
- outputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }