1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > easypoi list中的map导出_java导出excel(easypoi)

easypoi list中的map导出_java导出excel(easypoi)

时间:2021-01-16 11:18:25

相关推荐

easypoi list中的map导出_java导出excel(easypoi)

介绍

easypoi功能如同名字easy,主打的功能就是容易,让一个没接触过poi的人员

就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板

官网地址:http://easypoi.mydoc.io/

easypoi需要导入的包

<dependency>

<groupId>cn.afterturngroupId>

<artifactId>easypoi-baseartifactId>

<version>3.2.0version>

dependency>

<dependency>

<groupId>cn.afterturngroupId>

<artifactId>easypoi-webartifactId>

<version>3.2.0version>

dependency>

<dependency>

<groupId>cn.afterturngroupId>

<artifactId>easypoi-annotationartifactId>

<version>3.2.0version>

dependency>

easypoi工具类

package com.meeno.framework.office.excel.easypoi.utils;

import cn.afterturn.easypoi.excel.ExcelExportUtil;

import cn.afterturn.easypoi.excel.ExcelImportUtil;

import cn.afterturn.easypoi.excel.entity.ExportParams;

import cn.afterturn.easypoi.excel.entity.ImportParams;

import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;

import mons.lang3.StringUtils;

import org.apache.poi.ss.usermodel.Workbook;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.File;

import java.io.IOException;

import .URLEncoder;

import java.util.List;

import java.util.Map;

import java.util.NoSuchElementException;

/**

* @description: easypoiUtils

* @author: Wzq

* @create: -11-08 14:54

*/

publicclassExcelUtiles{

publicstaticvoid exportExcel(List> list, String title, String sheetName, Class> pojoClass,StringfileName, booleanisCreateHeader, HttpServletResponseresponse){

ExportParams exportParams = newExportParams(title, sheetName);

exportParams.setCreateHeadRows(isCreateHeader);

defaultExport(list, pojoClass, fileName, response, exportParams);

}

publicstaticvoid exportExcel(List> list, String title, String sheetName, Class> pojoClass,StringfileName,HttpServletResponseresponse){

defaultExport(list, pojoClass, fileName, response, newExportParams(title, sheetName));

}

publicstaticvoid exportExcel(List> list, String fileName, HttpServletResponse response){

defaultExport(list, fileName, response);

}privatestaticvoid defaultExport(List> list, Class> pojoClass, StringfileName,HttpServletResponseresponse, ExportParamsexportParams) {

Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);if(workbook != null); downLoadExcel(fileName, response, workbook);

}privatestaticvoid 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());

}

}privatestaticvoid defaultExport(List> list, String fileName, HttpServletResponse response) {

Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);if(workbook != null);

downLoadExcel(fileName, response, workbook);

}publicstaticList importExcel(String filePath,Integer titleRows,Integer headerRows, Class<T> pojoClass){if(StringUtils.isBlank(filePath)){returnnull;

}

ImportParams params = newImportParams();

params.setTitleRows(titleRows);

params.setHeadRows(headerRows);Listlist= null;try{list= ExcelImportUtil.importExcel(newFile(filePath), pojoClass, params);

}catch(NoSuchElementException e){//throw new NormalException("模板不能为空");

} catch(Exceptione) {

e.printStackTrace();//throw new NormalException(e.getMessage());

} returnlist;

}publicstaticList importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass){if(file == null){ returnnull;

}

ImportParams params = newImportParams();

params.setTitleRows(titleRows);

params.setHeadRows(headerRows);Listlist= null;try{list= ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);

}catch(NoSuchElementException e){// throw new NormalException("excel文件不能为空");

} catch(Exceptione) {//throw new NormalException(e.getMessage());

System.out.println(e.getMessage());

}returnlist;

}

}

创建导出excel模板的实体类,也可以按照模板导出具体看官网文档

导出excel模板的实体类代码如下:

packagecom.meeno.framework.office.excel.easypoi.model;

importcn.afterturn.easypoi.excel.annotation.Excel;

importlombok.Data;

importjava.io.Serializable;

/**

* @description: 签到记录ExecelModel

* @author: Wzq

* @create: -01-07 11:34

*/

@Data

public class SignRecordExcelModel implements Serializable {

/**

* 序号

*/

@Excel(name= "序号",orderNum = "0")

private Integer number;

/**

* 人员名称

*/

@Excel(name= "人员名称",orderNum = "1")

private String employeeName;

/**

* 座位号

*/

@Excel(name= "座位号",orderNum = "2")

private Integer seatNum;

/**

* 签到状态

*/

@Excel(name= "签到状态",orderNum = "3")

private String signStatusStr;

}

使用easypoi导出excel

controller层调用代码

/**

*@Description导出登录记录表

*@Param[session, request, response, data]

*@Returnvoid

*@AuthorWzq

*@Date/1/7

*@Time11:06

*/

@RequestMapping(value = "exportSignRecord.action")

publicvoidexportSignRecord(finalHttpSession session,finalHttpServletRequest request,finalHttpServletResponse response,

@RequestParam(value = "Data",required = false)String data){

JSONObject jsonObject = JSONObject.parseObject(data);

//会议id

Long meetingId = jsonObject.getLong("meetingId");

List signRecordExcelModels = this.signRecordService.exportSignRecord(meetingId);

Meeting meeting = this.meetingService.getMeetingDetail(meetingId);//第一参数:导出的模板类集合//第二参数:excel中里面内容合并单元格的title//第三参数:seeetName名称//第四参数:导出模板实体类的class//第五参数:导出文件文件名//第六参数:HttpServletResponse

ExcelUtiles.exportExcel(signRecordExcelModels,"","签到表",SignRecordExcelModel.class,meeting.getName()+"签到表.xlsx",response);

}

导出效果

有你想看的精彩GitNote基于git的个人云笔记

Docker创建自己的github(Gitea)

使用seafile搭建自己的百度云docker开源系统监控软件NagiosSpringBoot监听redis订阅监听和发布订阅SpringBoot监听redis过期key

长按扫码关注

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。