1.导入相关依赖
<!--poi-->
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
2.在entity中添加注释
@Data
@Accessors(chain = true)
@TableName("cmfz_album")
@ExcelTarget(value = "album")
public class Album extends Model<Album> {
private static final long serialVersionUID = 1L;
@Excel(name = "编号")
private String id;
@Excel(name = "标题")
private String title;
@Excel(name = "分数")
private String score;
@Excel(name = "描述")
private String detail;
@Excel(name = "播音")
private String broadcast;
@Excel(name = "封面", type = 2 ,width = 20 , height = 20)
private String cover;
@Excel(name = "作者")
private String author;
@Excel(name = "数量")
private Integer count;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@TableField("create_date")
@Excel(name = "出版日期",format = "yyyy年MM月dd日 HH时mm分ss秒",width = 30)
private Date createDate;
@TableField(exist = false)
@ExcelCollection(name = "专辑")
private List<Chapter> chapter;
}
3.引入ExcelUtils工具类
public class ExcilUtils {
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
boolean isCreateHeader, HttpServletResponse response) {
ExportParams exportParams = new ExportParams(title, sheetName);
exportParams.setCreateHeadRows(isCreateHeader);
defaultExport(list, pojoClass, fileName, response, exportParams);
}
public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
HttpServletResponse response) {
defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
}
public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
defaultExport(list, fileName, response);
}
private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
ExportParams exportParams) {
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
}
private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
try {
response.setCharacterEncoding("UTF-8");
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
workbook.write(response.getOutputStream());
} catch (IOException e) {
// throw new NormalException(e.getMessage());
}
}
private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null)
;
downLoadExcel(fileName, response, workbook);
}
public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
if (StringUtils.isBlank(filePath)) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
} catch (NoSuchElementException e) {
// throw new NormalException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
// throw new NormalException(e.getMessage());
}
return list;
}
public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
Class<T> pojoClass) {
if (file == null) {
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List<T> list = null;
try {
list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
} catch (NoSuchElementException e) {
// throw new NormalException("excel文件不能为空");
} catch (Exception e) {
// throw new NormalException(e.getMessage());
System.out.println(e.getMessage());
}
return list;
}
}
4.controller层代码
(1)导出
@RequestMapping("poi")
public void poi(HttpServletResponse response) throws IOException {
List<Chapter> chapters = chapterService.selectList(null);
List<Album> albums = albumService.selectList(null);
for (Album album : albums) {
String id = album.getId();
album.setCover("D:\\java\\ideacodes\\cmfz_ly\\src\\main\\webapp\\files\\" + album.getCover());
album.setChapter(new ArrayList<>());
for (Chapter chapter : chapters) {
if (id.equals(chapter.getAlbumId())) {
album.getChapter().add(chapter);
}
}
}
ExcilUtils.exportExcel(albums, "专辑", "专辑", Album.class, "article.xls", response);
(2)导入
@RequestMapping("importExcel")
public String importExcel(MultipartFile file) {
List<Album> albums = ExcilUtils.importExcel(file, 1, 1, Album.class);
for (Album album : albums) {
if (album.getId() == null) {
} else {
System.out.println("导入的结果为:" + album);
albumService.insert(album);
}
}
return "导入成功";
}
5.在jsp页面中调用方法
<a href="${pageContext.request.contextPath}/album/poi">导出专辑Excel表</a>
<form action="${pageContext.request.contextPath}/album/importExcel" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="将表中数据导入数据库">
</form>
6.注意:没有引入util时controller层代码
//设置响应头信息
try {
response.setHeader("content-disposition","attachment;fileName="+ URLEncoder.encode("专辑详情.xls","UTF-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
ServletOutputStream outputStream = response.getOutputStream();
Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("所有专辑","音频"),Album.class, albums);
workbook.write(outputStream);
workbook.close();