最近一个项目用到了到处Excel表格。

1.首先 通过Maven 添加jar包。

                <!--导出excel--><dependency><groupId>org.jeecg</groupId><artifactId>easypoi-base</artifactId><version>2.4.0</version></dependency><dependency><groupId>org.jeecg</groupId><artifactId>easypoi-annotation</artifactId><version>2.4.0</version></dependency><dependency><groupId>org.jeecg</groupId><artifactId>easypoi-web</artifactId><version>2.4.0</version></dependency>

2.写一个公共类(这里面的方法用来导出表格)。

package com.bdqn.util;import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.enmus.ExcelType;
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;/*** Created by xiaox on 2018/3/19.*/
public class FileUtil {public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws Exception {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) throws Exception {defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));}public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception {defaultExport(list, fileName, response);}private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws Exception {Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);if (workbook != null);downLoadExcel(fileName, response, workbook);}private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws Exception {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 Exception(e.getMessage());}}private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception {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) throws Exception {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 Exception("模板不能为空");} catch (Exception e) {e.printStackTrace();throw new Exception(e.getMessage());}return list;}public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception {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 Exception("excel文件不能为空");} catch (Exception e) {throw new Exception(e.getMessage());}return list;}
}

3.在实体类添加注解

package com.bdqn.pojo;import org.jeecgframework.poi.excel.annotation.Excel;import java.time.DateTimeException;
import java.util.Date;/*** Created by 李云鹏 on 2018/2/26.* 员工实体类*/
public class Staff {/***员工爱好*/private String staffHobby;/***员工姓名*/@Excel(name = "姓名", orderNum = "1")private String staffName;/***员工性别*/@Excel(name = "性别", replace = {"男_1", "女_2"}, orderNum = "2")private String staffGender;/***员工编号*/@Excel(name = "编号", orderNum = "0")private Integer staffId;/***员工生日*/@Excel(name = "生日", exportFormat = "yyyy-MM-dd", orderNum = "3")private Date staffBirthDay;/*** 员工学历*/@Excel(name = "学历", orderNum = "4")private String staffEducation;/*** 员工手机*/@Excel(name = "手机号", orderNum = "5")private String staffMobile;/*** 员工邮箱*/@Excel(name = "Email", orderNum = "6")private String staffEmail;/*** 员工住址*/@Excel(name = "住址", orderNum = "7")private String staffAddress;/*** 员工部门编号*/private Integer branceId;/*** 员工入职日期*/@Excel(name = "入职日期",exportFormat = "yyyy-MM-dd", orderNum = "8")private Date staffEntryData;/*** 回收站(0:在职 1:回收站)*/private Integer recyclebin;/*** 职位*/private Integer postId;/*** 系统角色(1:超级管理 2:普通管理 3:员工)*/@Excel(name = "管理权限", orderNum = "12")private Integer roleManagerial;/*** 员工薪水*/@Excel(name = "员工薪水", orderNum = "9")private Integer staffSalary;/*** 员工密码*/private String password;//别的表的数据字段/*** 部门名称*/@Excel(name = "所属部门", orderNum = "10")private String branceName;public String getBranceName() {return branceName;}public void setBranceName(String branceName) {this.branceName = branceName;}/*** 职位名称*/@Excel(name = "职位", orderNum = "11")private String postName;public String getPostName() {return postName;}public void setPostName(String postName) {this.postName = postName;}
//-----------------------/***员工爱好*/public String getStaffHobby() {return staffHobby;}/***员工爱好*/public void setStaffHobby(String staffHobby) {this.staffHobby = staffHobby;}/***。员工年龄*/public String getStaffName() {return staffName;}/***。员工年龄*/public void setStaffName(String staffName) {this.staffName = staffName;}/***员工性别*/public String getStaffGender() {return staffGender;}/***员工性别*/public void setStaffGender(String staffGender) {this.staffGender = staffGender;}/***员工编号*/public Integer getStaffId() {return staffId;}/***员工编号*/public void setStaffId(Integer staffId) {this.staffId = staffId;}/***员工生日*/public Date getStaffBirthDay() {return staffBirthDay;}/***员工生日*/public void setStaffBirthDay(Date staffBirthDay) {this.staffBirthDay = staffBirthDay;}/*** 员工学历*/public String getstaffEducation() {return staffEducation;}/*** 员工学历*/public void setstaffEducation(String staffEducation) {this.staffEducation = staffEducation;}/*** 员工手机*/public String getStaffMobile() {return staffMobile;}/*** 员工手机*/public void setStaffMobile(String staffMobile) {this.staffMobile = staffMobile;}/*** 员工邮箱*/public String getStaffEmail() {return staffEmail;}/*** 员工邮箱*/public void setStaffEmail(String staffEmail) {this.staffEmail = staffEmail;}/*** 员工住址*/public String getStaffAddress() {return staffAddress;}/*** 员工住址*/public void setStaffAddress(String staffAddress) {this.staffAddress = staffAddress;}/*** 员工部门*/public Integer getbranceId() {return branceId;}/*** 员工部门*/public void setbranceId(Integer branceId) {this.branceId = branceId;}/*** 员工入职日期*/public Date getStaffEntryData() {return staffEntryData;}/*** 员工入职日期*/public void setStaffEntryData(Date staffEntryData) {this.staffEntryData = staffEntryData;}/*** 回收站(0:在职 1:回收站)*/public Integer getRecyclebin() {return recyclebin;}/*** 回收站(0:在职 1:回收站)*/public void setRecyclebin(Integer recyclebin) {this.recyclebin = recyclebin;}/*** 职位*/public Integer getPostId() {return postId;}/*** 职位*/public void setPostId(Integer postId) {this.postId = postId;}/*** 系统角色(1:超级管理 2:普通管理 3:员工)*/public Integer getRoleManagerial() {return roleManagerial;}/*** 系统角色(1:超级管理 2:普通管理 3:员工)*/public void setRoleManagerial(Integer roleManagerial) {this.roleManagerial = roleManagerial;}/*** 员工薪水*/public Integer getStaffSalary() {return staffSalary;}/*** 员工薪水*/public void setStaffSalary(Integer staffSalary) {this.staffSalary = staffSalary;}/*** 员工密码*/public String getPassword() {return password;}/*** 员工密码* @param password*/public void setPassword(String password) {this.password = password;}
}

4.最后写方法导出

 //导出处表@RequestMapping("/excelStaff")public void download(HttpServletResponse  response,@RequestParam(value = "staffId", required = false) Integer staffId, @RequestParam(value = "staffName", required = false) String staffName,@RequestParam(value = "branceId", required = false) Integer branceId, @RequestParam(value = "postId", required = false) Integer postId,@RequestParam(value = "roleId", required = false) Integer roleId ){try {PageSupport pageSupport = new PageSupport();pageSupport.setPageSize(Integer.MAX_VALUE);pageSupport.setTotalCount(staffService.getCountStaff(staffId, staffName, branceId, postId,roleId,0));pageSupport.setCurrentPageNo(1);List<Staff> staffList = staffService.findStaff(staffId, staffName, branceId, postId,roleId, pageSupport,0);FileUtil.exportExcel(staffList,"员工表","",Staff.class,"员工信息表.xls",response);} catch (Exception e) {e.printStackTrace();}}

这样可以实现导出简单的Excel表格了!

使用EasyPoi 导出简单的Excel表格。相关推荐

  1. extjs 导出简单的excel表格

    js代码: this.returnJnlExportBtn = new Ext.Button({text : "退单文件导出",id : "returnJnl_expor ...

  2. 使用EasyPOI导出复杂的Word表格

    在我们日常开发中常常有需求,需要以Word格式导出各种合同信息,收费凭证等问题.格式较为复杂(如下图).我们该怎么解决呢? 下面通过五步带大家学习如何使用EasyPOI导出复杂的Word表格! 文章目 ...

  3. 利用EasyExcel完整的springboot +vue前后端导出并下载excel表格

    文章目录 写在前面 正文 1. springboot后端引入easyexcel及使用 1.1 引入依赖 1.2 接口serviceImpl方法 1.3 提供一个对list集合去重的方法(根据相同key ...

  4. 如何在 Vue 中导出数据至 Excel 表格 - 卡拉云

    本文首发:<如何在 Vue 中导出数据至 Excel 表格 - 卡拉云> 我们经常需要在 Vue 搭建的后台管理系统里导出数据到 Excel / CSV ,方便我们将数据共享给其他同学或在 ...

  5. 如何将物流信息导出保存在EXCEL表格里面,物流查询

    很朋友在问如何查询物流信息并将查询到的物流信息导出表格保存,有需要的朋友可以看下这篇文章,小编将分享物流的查询方法,物流批量查询,查询完成后将物流信息导出保存在EXCEL表格里面,一起来看看吧! 第一 ...

  6. 百度指数常见php框架,怎么导出数据到excel表格-如何将百度指数数据导出到Excel表格...

    如何将百度指数数据导出到Excel表格 第一步:打开CAD.CAD命令行输入"Li"."选择对象"选需要提取坐标的多段线.回车. 第二步:将CAD文本框中的数据 ...

  7. 安卓mysql导出excel_Android开发实现的导出数据库到Excel表格功能【附源码下载】...

    本文实例讲述了Android开发实现的导出数据库到Excel表格功能.分享给大家供大家参考,具体如下: 之前一直在电脑上用Excel表格记录家庭帐单,不久前重装系统不小心干掉了,伤心了好久,那可是我记 ...

  8. python excel库 linux_用python写一个简单的excel表格获取当时的linux系统信息

    最近在学习excel表格的制作,顺便结合之前学习的内容,利用python的两个模板,分别是获取系统信息的psutil,和生成excel表格的xlsxwriter.利用这两个模板将生成一个简单的exce ...

  9. 如何导出数据到Excel表格

    开发工具与关键技术:Visual Studio.MVC 作者:幻奏 撰写时间:2019.5.5 我们在日常的生活中常常会看到很多的数据,有时,我们不一定只是在项目里面看到数据,可能我们还要在其他的地方 ...

最新文章

  1. 牛逼!微信红包封面可以更换了!!!
  2. 忘了root口令解决方法
  3. Dapper 多数据库优化
  4. python封装函数、实现将任意的对象序列化到磁盘上_Python系列之lambda、函数、序列化...
  5. 转载:工程师笔记|STM32F030在低温下无法启动
  6. SAP Fiori Elements 框架里 Smart Table 控件的工作原理介绍
  7. ubuntu下面挂载mtp设备的目录位置
  8. TVP5158的多路复用技术
  9. 怎么在虚拟机上安装linux mint,如何在VirtualBox上安装Linux Mint?
  10. oracle机票,全球机票分销系统
  11. Javascript+css 实现网页换肤功能
  12. 在Exchange Server 2007中修改邮件接受域
  13. 初识 Powershell 5.0 class
  14. esxi6.7封装nvme驱动
  15. 计算机硬件性能检测报告,性能测试实验报告.doc
  16. Arduino ESP32 深度睡眠与外部唤醒(EXT0)
  17. 如何根据边长数值计算六边形的面积
  18. linux kde vga参数1366,Archlinux+KDE 下双屏VGA高分辨率设置
  19. 医院对讲管理his系统服务器,医院智能化医护对讲系统建设技术方案.doc
  20. SOS:硬盘数据丢失怎么办?

热门文章

  1. 从规模走向规模经济,锅圈食汇回归餐饮初心
  2. PS用橡皮檫檫除图形与背景颜色一样的方法
  3. mysql5.7.25安装包,Mysql5.7.25在windows下安装
  4. 【答读者问12】如何理解backtrader的line以及对line进行操作?
  5. Hive sql 常用命令2
  6. 一道面试题(限流,幂等key)
  7. excel自动化的第一个实用例子(宿舍分饭)
  8. 2023-spring 2.探险营地 — 字符串
  9. 云直播SDK核心功能对比|腾讯云、阿里云、声网、即构等SDK厂商对比
  10. 协方差意味着什么_微服务意味着我们可以使用所需的任何语言? 真?