.net 导出excel_java导出excel(easypoi)

64720c7cffdc735a7548f876266d3167.png

介绍

easypoi功能如同名字easy,主打的功能就是容易,让一个没接触过poi的人员就可以方便的写出Excel导出,Excel模板导出,Excel导入,Word模板导出,通过简单的注解和模板官网地址:http://easypoi.mydoc.io/

easypoi需要导入的包

  cn.afterturn  easypoi-base  3.2.0  cn.afterturn  easypoi-web  3.2.0  cn.afterturn  easypoi-annotation  3.2.0

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 org.apache.commons.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 java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
* @description: easypoiUtils
* @author: Wzq
* @create: 2019-11-08 14:54
*/
public class ExcelUtiles {
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> 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> list, String fileName, HttpServletResponse response) {
Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
if (workbook != null);
downLoadExcel(fileName, response, workbook);
}
public static List importExcel(String filePath,Integer titleRows,Integer headerRows, Class pojoClass){
if (StringUtils.isBlank(filePath)){
return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List 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 List importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class pojoClass){
if (file == null){ return null;
}
ImportParams params = new ImportParams();
params.setTitleRows(titleRows);
params.setHeadRows(headerRows);
List 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;
}
}

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

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

package com.meeno.framework.office.excel.easypoi.model;import cn.afterturn.easypoi.excel.annotation.Excel;import lombok.Data;import java.io.Serializable;/** * @description: 签到记录ExecelModel * @author: Wzq * @create: 2020-01-07 11:34 */@Datapublic 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]    *@Return void    *@Author Wzq    *@Date 2020/1/7    *@Time 11:06    */    @RequestMapping(value = "exportSignRecord.action")    public void  exportSignRecord(final HttpSession session,                                  final HttpServletRequest request,                                  final HttpServletResponse 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);    }

导出效果

4a3d81a3501315044733fdf0e53a4aef.png

原创 GoslingWu 架构师与哈苏

b8665845ac054e06e07a3fabf89bfde8.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值