目录
一、Apache POI 简介
二、入门案例(写入导出)
三、实际开发过程中的导出操作——(将文件下载至客户端浏览器中)
一、Apache POI 简介
Apache POI(Poor Obfuscation Implementation)是 Apache 软件基金会的开源项目,提供 Java 操作 Microsoft Office 格式文件(如 Excel、Word、PowerPoint 等)的 API 库。
二、入门案例(写入导出)
1.导入Maven坐标
<!-- poi -->
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId>
</dependency>
<dependency>
2.创建excel文件,并写入数据
/*** 通过POI创建Excel文件并写入数据*/public static void write() throws IOException {// 在内存中创建一个新的Excel文件XSSFWorkbook excel = new XSSFWorkbook();// 创建一个新的sheet(sheet名为“info”)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 out = new FileOutputStream("D:\\info.xlsx");excel.write(out);
// 关闭资源out.close();excel.close();}
效果图:
3.读取excel文件中的内容
/*** 通过POI读取Excel文件中的数据*/public static void read() throws IOException {// 创建输入流FileInputStream in = new FileInputStream("D:\\info.xlsx");// 通过输入流读取Excel文件XSSFWorkbook workbook = new XSSFWorkbook(in);// 获取第一个sheetXSSFSheet sheet = workbook.getSheetAt(0);
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);}// 关闭资源in.close();workbook.close();}
效果图:
三、实际开发过程中的导出操作——(将文件下载至客户端浏览器中)
1.准备好模板excel文件(调格式会比较麻烦,因此直接准备好)放到resources下的template文件夹下。
2.查询数据,将数据写入至excel文件,并生成输出流供客户端下载。
代码示例:
/*** 导出运营数据报表*/
@Override
public void exportBusinessData(HttpServletResponse response) {//1.查询数据库,获取营业数据---查询最近30天的运营数据LocalDate begin = LocalDate.now().minusDays(30);LocalDate end = LocalDate.now().minusDays(1);//查询概览数据BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));//2.通过POI将数据写入Excel文件InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");try {//基于模板文件创建一个新的Excel文件XSSFWorkbook excel = new XSSFWorkbook(in);//获取表格文件的Sheet标签页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());//获得第五行row = sheet1.getRow(4);row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getUnitPrice());//填充明细数据for(int i=0;i<30;i++){LocalDate date = begin.plusDays(i);//查询某一天的营业数据businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));//获得某一行row = sheet1.getRow(i+7);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(3).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(5).setCellValue(businessDataVO.getUnitPrice());row.getCell(6).setCellValue(businessDataVO.getNewUsers());}//3.通过输出流将Excel文件下载到客户端浏览器ServletOutputStream out= response.getOutputStream();excel.write(out);//关闭资源out.close();excel.close();in.close();} catch (IOException e) {throw new RuntimeException(e);}}