官网的是如下几个依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.1.0</version>
</dependency>
在SpringBoot项目中只导入这个依赖就行
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
当然,要想测试的话,基础的springboot依赖,web依赖,测试依赖还是得有,不写get,set,构造方法,还得有lombok。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
package com.lq.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class OwnerCarInfoDto {
//姓名
private String name;
//性别
private String sex;
//楼栋
private String building;
//电话
private String tel;
//车牌号
private String licensePlate;
//品牌
private String brand;
//颜色
private String color;
//停车位
private String parkingSpace;
//使用方式
private String usage;
//起始时间
private String carRentalDateTime;
}
package com.lq.controller;
import com.lq.service.ICarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@RestController
@RequestMapping("/car")
public class AppController {
@Autowired
public ICarService carService;
@GetMapping("/export")
public void export(HttpServletResponse response) throws IOException {
carService.export(response);
}
}
package com.lq.service.impl;
import com.lq.domain.OwnerCarInfoDto;
import com.lq.service.ICarService;
import com.lq.util.MyExcelUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;
@Service
public class CarServiceImpl implements ICarService {
@Override
public void export(HttpServletResponse response) throws IOException {
List<OwnerCarInfoDto> infoList = new ArrayList<>();
OwnerCarInfoDto ownerCarInfoDto = new OwnerCarInfoDto();
ownerCarInfoDto.setName("张三");
ownerCarInfoDto.setSex("男");
ownerCarInfoDto.setBuilding("1栋1单元101室");
ownerCarInfoDto.setTel("15363536353");
ownerCarInfoDto.setLicensePlate("渝C:78773");
ownerCarInfoDto.setBrand("奥迪");
ownerCarInfoDto.setColor("红");
ownerCarInfoDto.setParkingSpace("A001-A8");
ownerCarInfoDto.setUsage("月租车");
ownerCarInfoDto.setCarRentalDateTime("2024-01-03 00:00:00~2024-02-04 00:00:00");
infoList.add(ownerCarInfoDto);
MyExcelUtil.exportExcel("幸福","恒大",infoList,response);
}
}
package com.lq.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.lq.domain.OwnerCarInfoDto;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
public class MyExcelUtil {
/**
* @param infoList excel数据
* @param response
* @throws IOException
*/
public static void exportExcel(String communityName, String propertyName, List<OwnerCarInfoDto> infoList, HttpServletResponse response) throws IOException {
try {
TemplateExportParams params = new TemplateExportParams(
"E:\\小区业主车辆息统计表模板.xls");//模版的位置
Map<String, Object> map = new HashMap<String, Object>();
map.put("communityName", communityName); //社区名
map.put("propertyName", propertyName); //物业名
//所有数据
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < infoList.size(); i++) {
//一行数据
Map<String, Object> lm = new HashMap<>();
OwnerCarInfoDto ownerCarInfoDto = infoList.get(i);
lm.put("no", i + 1); //序号
lm.put("name", ownerCarInfoDto.getName());//业主姓名
lm.put("sex", ownerCarInfoDto.getSex());//业主性别
lm.put("building", ownerCarInfoDto.getBuilding());//楼栋号
lm.put("tel", ownerCarInfoDto.getTel());//联系电话
lm.put("licensePlate", ownerCarInfoDto.getLicensePlate());//车牌号
lm.put("brand", ownerCarInfoDto.getBrand());//品牌
lm.put("color", ownerCarInfoDto.getColor());//颜色
lm.put("parkingSpace", ownerCarInfoDto.getParkingSpace());//停车位
lm.put("usage", ownerCarInfoDto.getUsage());//使用方式
lm.put("carRentalDateTime",ownerCarInfoDto.getCarRentalDateTime());
list.add(lm);
}
map.put("list", list);
//工作簿(第一个参数是表头信息,第二个参数是每一行的数据)
Workbook workbook = ExcelExportUtil.exportExcel(params, map);
//excel表格,第一个参数是excel文件名,第二个参数是响应,第三个参数是工作簿
MyExcelUtil.downLoadExcel(map.get("communityName") + "小区业主车辆信息登记表", response, workbook);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* excel下载
*
* @param fileName 下载时的文件名称
* @param response
* @param workbook excel数据
*/
public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xls", "UTF-8"));
workbook.write(response.getOutputStream());
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
}
{{communityName}}小区业主车辆信息登记表
物业公司:{{propertyName}}物业公司
{{$fe: list t.no t.name t.sex t.building t.tel t.licensePlate t.brand t.color t.parkingSpace t.usage t.carRentalDateTime}}