赞
踩
介绍:
Apache POI 是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用 POI 在 Java 程序中对Miscrosoft Office各种文件进行读写操作。
一般情况下,POI 都是用于操作 Excel 文件。
如何使用:
1.maven坐标引入
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>3.16</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>3.16</version>
- </dependency>
2.通过入门案例的方式了解代码,代码里面的方法都比较见名知意,就不一 一列举了,主要的逻辑就是用过文件流的方式写入或者写出,然后作为参数,调用excel对象的方法
将创建的excel对象写到磁盘中
- public static void write() throws Exception {
- //在内存中创建一个Excel对象
- XSSFWorkbook excel = new XSSFWorkbook();
- //创建sheet页
- XSSFSheet sheet = excel.createSheet("sheet");
- //在sheet页中创建行,0表示第一行
- XSSFRow row = sheet.createRow(0);
- //创建单元格,并为单元格设置值,单元格索引也是从0开始
- row.createCell(0).setCellValue("姓名");
- row.createCell(1).setCellValue("年龄");
-
- //第二行
- XSSFRow row1 = sheet.createRow(1);
- row1.createCell(0).setCellValue("张三");
- row1.createCell(1).setCellValue("20");
-
- //将excel写入磁盘中
- FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\liyuzhe.xlsx"));
- excel.write(fileOutputStream);
- //关闭资源
- fileOutputStream.flush();
- fileOutputStream.close();
- excel.close();
- }

将磁盘中的excel文件读取出来:
- /**
- * 使用poi读取excel中的内容
- */
- public static void read() throws Exception {
- //创建一个输入流
- InputStream in = new FileInputStream(new File("D:\\liyuzhe.xlsx"));
- //创建excel对象
- XSSFWorkbook excel = new XSSFWorkbook(in);
- //读取excel文件中的第一个sheet页
- XSSFSheet sheet = excel.getSheetAt(0);
- //获取sheet中最后一行的行号,获取带有文字的最后一行
- int lastRowNum = sheet.getLastRowNum();
- for (int i = 0; i <= lastRowNum; i++) {
- //获得某一行
- XSSFRow row = sheet.getRow(i);
- //获得单元格对象
- String cell1 = row.getCell(0).getStringCellValue();
- String cell2 = row.getCell(1).getStringCellValue();
- //将单元格内容输出
- System.out.println(cell1 + " " +cell2);
- }
- //关闭资源
- in.close();
- excel.close();
- }

main函数:
- public static void main(String[] args) throws Exception {
- //write();
- read();
- }
在项目中的使用:
一般在项目中,前端编辑的页面会有一个交互的按钮,点击后交互到后端,后端将excel模板文件内的内容进行填充,然后通过浏览器进行下载,下载到磁盘
例如:
点击后会通过浏览器下载一个excel文件
后端代码:
controller层:
service层:
- /**
- * 导出数据报表
- *
- * @param httpServletResponse
- */
- @Override
- public void exportBusiness(HttpServletResponse httpServletResponse) {
- //1.查询数据库,获取营业数据 -- 查询最近30天的运营数据
- LocalDate dateBegin = LocalDate.now().minusDays(30);//30天前
- LocalDate dateEnd = LocalDate.now().minusDays(1);//昨天
- //查询概览数据
- BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));
- //2.通过poi将数据写入到excel表格中
-
- //获取resources中的excel模板
- /*InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");*/
- InputStream in = ClassLoader.getSystemResourceAsStream("template/运营数据报表模板.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());
-
-
- //填充明细数据,遍历30天的明细数据,所以只需要小于30即可
- for (int i = 0; i < 30; i++) {
- LocalDate date = dateBegin.plusDays(i);//从30天前开始遍历
-
- //查询某一天的营业数据
- BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
- //获取第8行,后面的行依次遍历
- 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());//新增用户数
-
- }
-
- //3.通过输出流将excel文件下载到客户端浏览器
- ServletOutputStream outputStream = httpServletResponse.getOutputStream();
- excel.write(outputStream);
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }

最终会把文件写入到浏览器中进行下载
注:例如此种页面较为复杂的excel模板不会再java中处理,一般都是自己再磁盘创建好一个excel,在java中读入使用即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。