Apache POI
是一个处理MiscrosoftOffice各种文件格式的开源项目。角度来说就是,我们可以使用POI在java程序中对MiscrosoftOffice各种文件进行读写操作
一般情况下,poi都是用于操作excel文件
直接上例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| @Override public void export(HttpServletResponse response) { LocalDate begin = LocalDate.now().minusDays(30); LocalDate end = LocalDate.now().minusDays(1); BusinessDataVO businessDataVO = workSpaceService.businessDataVOResult(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX)); InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx"); List<LocalDate> dates = new ArrayList<>(); dates.add(begin); while(!begin.equals(end)){ begin = begin.plusDays(1); dates.add(begin); } try{ XSSFWorkbook excel = new XSSFWorkbook(in); XSSFSheet sheet1 = excel.getSheet("Sheet1"); sheet1.getRow(1).getCell(1).setCellValue("时间:"+begin+"至"+end); XSSFRow row = sheet1.getRow(3); row.getCell(2).setCellValue(businessDataVO.getTurnover()); row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate()); row.getCell(6).setCellValue(businessDataVO.getNewUsers()); XSSFRow row1 = sheet1.getRow(4); row1.getCell(2).setCellValue(businessDataVO.getValidOrderCount()); row1.getCell(4).setCellValue(businessDataVO.getUnitPrice()); int index = 7; for (LocalDate localDate :dates){ LocalDateTime beginTime = LocalDateTime.of(localDate,LocalTime.MIN); LocalDateTime endTime = LocalDateTime.of(localDate,LocalTime.MAX); BusinessDataVO businessDataVO1 = workSpaceService.businessDataVOResult(beginTime, endTime); XSSFRow row2 = sheet1.getRow(index); row2.getCell(1).setCellValue(localDate.toString()); row2.getCell(2).setCellValue(businessDataVO1.getTurnover()); row2.getCell(3).setCellValue(businessDataVO1.getValidOrderCount()); row2.getCell(4).setCellValue(businessDataVO1.getOrderCompletionRate()); row2.getCell(5).setCellValue(businessDataVO1.getUnitPrice()); row2.getCell(6).setCellValue(businessDataVO1.getNewUsers()); index++; } ServletOutputStream stream = response.getOutputStream(); excel.write(stream);
stream.close(); excel.close(); }catch (IOException e){ e.printStackTrace(); } }
|