入门案例
Apache POI的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>
写操作
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* 使用POI操作Excel文件
*/
public class POITest {
/**
* 通过POI创建excel文件并且写入文件
*/
public static void write() throws Exception {
//在内存中创建一个Excel文件(平时鼠标右键创建是在磁盘中创建)
XSSFWorkbook excel = new XSSFWorkbook();
//在Excel文件中创建一个Sheet页
XSSFSheet sheet = excel.createSheet("info");
//在Sheet页中创建一行,rownum编号是从0开始的
XSSFRow row = sheet.createRow(1);
//在第1行中创建一个单元格,cellnum编号是从0开始的
XSSFCell cell = row.createCell(1);
//为单元格设置值
cell.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("南京");
//通过删除流将内存中的Excle文件写入磁盘
FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(fileOutputStream);
//关闭资源
fileOutputStream.close();
excel.close();
}
public static void main(String[] args) throws Exception {
write();
}
}
读操作
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
/**
* 使用POI操作Excel文件
*/
public class POITest {
/**
* 通过POI创建excel文件并且写入文件
*/
public static void write() throws Exception {
//在内存中创建一个Excel文件(平时鼠标右键创建是在磁盘中创建)
XSSFWorkbook excel = new XSSFWorkbook();
//在Excel文件中创建一个Sheet页
XSSFSheet sheet = excel.createSheet("info");
//在Sheet页中创建一行,rownum编号是从0开始的
XSSFRow row = sheet.createRow(1);
//在第1行中创建一个单元格,cellnum编号是从0开始的
XSSFCell cell = row.createCell(1);
//为单元格设置值
cell.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("南京");
//通过删除流将内存中的Excle文件写入磁盘
FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\info.xlsx"));
excel.write(fileOutputStream);
//关闭资源
fileOutputStream.close();
excel.close();
}
/**
* 通过POI读取excel文件中的内容
*/
public static void read() throws Exception {
//通过文件流读取磁盘中的Excel文件
FileInputStream fileInputStream = new FileInputStream(new File("D:\\info.xlsx"));
//通过文件流读取磁盘中的Excel文件
XSSFWorkbook excel = new XSSFWorkbook(fileInputStream);
//通过文件流读取磁盘中的Excel文件中的sheet页
//也可以写 XSSFSheet sheet = excel.getSheet(0);
XSSFSheet sheet = excel.getSheet("info");
//获取第一行的行号
int firstRowNum = sheet.getFirstRowNum();
//获取最后一行的行号
int lastRowNum = sheet.getLastRowNum();
//循环读取Excel文件中的数据
for (int i = firstRowNum; i <= lastRowNum; i++) {
//获取第i行的数据
XSSFRow row = sheet.getRow(i);
//获取第i行中单元格的数据并获取单元格中的值
String value1 = row.getCell(1).getStringCellValue();
String value2 = row.getCell(2).getStringCellValue();
System.out.println(value1 +""+ value2);
}
//关闭资源
fileInputStream.close();
excel.close();
}
public static void main(String[] args) throws Exception {
// write();
read();
}
}
简单实战
一般的excel表格要是通过POI来创建会很复杂(合并单元格,设置背景颜色等等)
如:
所以一般我们都会先自己创建一个模板,称为模板文件.
例:
代码开发实现步骤:
-
设计Excel模板文件,并在resources文件下新建个template文件,将模板文件放入
-
查询运营数据
-
将查询到的运营数据写入模板文件
-
通过输出流将Excel文件下载到客户端浏览器
-
controller层
/** * 导出运营数据报表 * @param response */ @GetMapping("/export") @ApiOperation("导出运营数据") public void export(HttpServletResponse response){ reportService.export(response); }
-
service层接口
/** * 导出运营数据 * @param response */ void export(HttpServletResponse response); service层实现类 /** * 导出运营数据 * @param response */ @Override public void export(HttpServletResponse response) { //1.查询数据库,获取营业数据---查询最具30天的运营数据 LocalDateTime begin = LocalDateTime.now().minusDays(30);//30天前 LocalDateTime end = LocalDateTime.now().minusDays(1);//昨天 //查询概览数据 BusinessDataVO businessData = workspaceService.getBusinessData(begin,end);//替换你自己的业务逻辑,把要下载的数据查询回来 //2.通过POI将数据写入Excel文件中,基于模板来填入数据 //通过类加载器获得我们的模板,这里填的是模板中的概览数据 InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx"); try { XSSFWorkbook excel = new XSSFWorkbook(in); //获取表格文件的sheet页 XSSFSheet sheet = excel.getSheet("sheet1"); //填充数据--时间 sheet.getRow(1).getCell(1).setCellValue("时间:" + begin + "至" + end); //获得第四行 XSSFRow row = sheet.getRow(3); row.getCell(2).setCellValue(businessData.getTurnover()); row.getCell(4).setCellValue(businessData.getOrderCompletionRate()); row.getCell(6).setCellValue(businessData.getNewUsers()); //获得第五行 row = sheet.getRow(4); row.getCell(2).setCellValue(businessData.getValidOrderCount()); row.getCell(4).setCellValue(businessData.getUnitPrice()); //填充明细数据(三十天中的每一天的数据),根据你自己的业务来 for (int i = 0; i < 30; i++) { LocalDate date = LocalDate.now().minusDays(30 + i); //查询某一天的营业数据 businessData = workspaceService.getBusinessData(date.atStartOfDay(),date.plusDays(1).atStartOfDay()); //获得第六行 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 out = response.getOutputStream(); excel.write(out); //关闭资源 excel.close(); out.close(); }catch (Exception e){ e.printStackTrace(); } }
-
mapper层
根业务需求来
网页下载后的效果
-