Controller数据导出Excel 详细教程——easypoi-base,easypoi-web,easypoi-annotation

x33g5p2x  于2021-12-18 转载在 其他  
字(5.5k)|赞(0)|评价(0)|浏览(575)

Controller获取数据导出Excel,详细教程

1:导入对应依赖

  1. <dependency>
  2. <groupId>cn.afterturn</groupId>
  3. <artifactId>easypoi-base</artifactId>
  4. <version>4.1.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>cn.afterturn</groupId>
  8. <artifactId>easypoi-web</artifactId>
  9. <version>4.1.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>cn.afterturn</groupId>
  13. <artifactId>easypoi-annotation</artifactId>
  14. <version>4.1.3</version>
  15. </dependency>

2:创建实体类 User

  1. public class User {
  2. //name代表 表头行 orderNum 代表 排序顺序
  3. @Excel(name="姓名",orderNum = "0")
  4. private String name;
  5. @Excel(name="性别",orderNum = "1")
  6. private String sex;
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public String getSex() {
  14. return sex;
  15. }
  16. public void setSex(String sex) {
  17. this.sex = sex;
  18. }

创建工具类 FileUtil工具类,固定写法,拿去直接用就可以

  1. package hr.manage.util;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import org.apache.poi.ss.usermodel.Workbook;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.io.IOException;
  7. import java.net.URLEncoder;
  8. import java.util.List;
  9. public class FileUtil {
  10. public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass,String fileName, HttpServletResponse response){
  11. defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
  12. }
  13. //设置导出文件内的内容
  14. private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) {
  15. Workbook workbook = ExcelExportUtil.exportExcel(exportParams,pojoClass,list);
  16. if (workbook != null);
  17. downLoadExcel(fileName, response, workbook);
  18. }
  19. //设置导出文件名,编码
  20. private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
  21. try {
  22. response.setCharacterEncoding("UTF-8");
  23. //让服务器告诉浏览器它发送的数据属于什么文件类型
  24. response.setHeader("content-Type", "application/vnd.ms-excel");
  25. //当Content-Type 的类型为要下载的类型时 , 这个信息头会告诉浏览器这个文件的名字和类型。
  26. response.setHeader("Content-Disposition",
  27. "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
  28. workbook.write(response.getOutputStream());
  29. } catch (IOException e) {
  30. // throw new NormalException(e.getMessage());
  31. }
  32. }
  33. }

创建UserController

  1. package hr.manage.controller.book;
  2. import hr.manage.entity.User;
  3. import hr.manage.util.FileUtil;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import javax.servlet.http.HttpServletResponse;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. //导出数据为Excel
  9. @RequestMapping("/user")
  10. @Controller
  11. public class UserController {
  12. @RequestMapping("/excel")
  13. public void exceladd(HttpServletResponse response){
  14. List<User>list = new ArrayList<>();
  15. list.add(new User("张三","男"));
  16. list.add(new User("李四","女"));
  17. // 参数
  18. // 1:传入数据集合,2:标题,3:工作表名称 4:实体类的Class 5:保存的Excel名字,传入HttpServletResponse
  19. FileUtil.exportExcel(list,"花名册","员工",User.class,"花名册.xls",response);
  20. }

开启tomcat 访问你自己的路径

打开Excel 这时数据已经导到里面了

前面步骤都完成的,现在我们可以改造一下代码,读取数据库中的数据
创建和数据表对应的实体类

  1. package hr.manage.entity;
  2. import cn.afterturn.easypoi.excel.annotation.Excel;
  3. import java.util.Date;
  4. import javax.xml.soap.Detail;
  5. public class CwgBookManager {
  6. //主键id
  7. private Integer id;
  8. //编号
  9. @Excel(name = "编号",orderNum = "0")
  10. private String number;
  11. //书名
  12. @Excel(name = "书名",orderNum = "1")
  13. private String name;
  14. //图书号
  15. @Excel(name = "图书编号",orderNum = "2")
  16. private String serialNumber;
  17. //出版社
  18. @Excel(name = "出版社",orderNum = "3")
  19. private String publisher;
  20. //作者
  21. @Excel(name = "作者",orderNum = "4")
  22. private String author;
  23. //创建人
  24. @Excel(name = "创建人",orderNum = "5")
  25. private int createBy;
  26. //创建时间
  27. @Excel(name = "创建时间",exportFormat ="yyyy-MM-dd", orderNum = "6")
  28. private Date creationDate;
  29. //册数,数量
  30. @Excel(name = "册数",orderNum = "7")
  31. private int total;
  32. //价格
  33. @Excel(name = "价格",orderNum = "8")
  34. private double price;
  35. //备注,详情
  36. @Excel(name = "备注",orderNum = "9")
  37. private String detail;
  38. // 状态0:无备案,1,有备案
  39. @Excel(name = "备案状态",orderNum = "10",type = 10)
  40. private int status;
  41. //备用字段1
  42. private String reserve;
  43. //备用字段2
  44. private String spare;
  45. //备用字段3
  46. private String standby;
  47. public Integer getId() {
  48. return id;
  49. }
  50. //getter,setter方法略
  51. }

查询数据库表数据

  1. @Repository
  2. public interface BookDao {
  3. /** * 查询book表数据 * @return */
  4. @Select("SELECT * FROM cwg_bookmanager")
  5. public List<CwgBookManager> findBookList();

编写Controller

  1. package hr.manage.controller.book;
  2. import hr.manage.dao.book.BookDao;
  3. import hr.manage.entity.CwgBookManager;
  4. import hr.manage.util.FileUtil;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import javax.annotation.Resource;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.List;
  11. /** * 导出Execl */
  12. @Controller
  13. @RequestMapping("/hrm/book")
  14. public class ExcelBookController {
  15. @Resource
  16. private BookDao book;
  17. @RequestMapping("/demo")
  18. public void execl(HttpServletResponse response) throws IOException {
  19. //对应你们查询数据库方法
  20. List<CwgBookManager> list = book.findBookList();
  21. System.out.println("Excel");
  22. FileUtil.exportExcel(list,"图书信息","张三",CwgBookManager.class,"图书.xls",response);
  23. }
  24. }

访问该Controller路径即可
打开Excel文件 这是可以看到数据库内的数据都导入到Excel中了

前台页面可以编写一个按钮,绑定一个单击事件

jq代码

  1. function excel(){
  2. $.ajax({
  3. url:'/HRManagement/hrm/book/demo',
  4. type:'get',
  5. success:function (data) {
  6. if (data != null){
  7. alert("导出成功");
  8. }
  9. }
  10. });
  11. }

相关文章