网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

如何在Java中利用EasyPoi实现一个sheet导出功能-创新互联

如何在Java中利用EasyPoi实现一个sheet导出功能?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

成都创新互联是专业的霸州网站建设公司,霸州接单;提供做网站、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行霸州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

一、引入maven jar包


    cn.afterturn
    easypoi-base
    3.2.0


    cn.afterturn
    easypoi-web
    3.2.0


    cn.afterturn
    easypoi-annotation
    3.2.0

二、编写导出的实体类(使用注解形式)

1、DeptUtil 类

@ExcelTarget("deptUtil")
public class DeptUtil {

  @Excel(name = "部门编号", width = 30 , needMerge = true)
  private Integer id;

  @Excel(name = "部门名称", width = 30 , needMerge = true)
  private String deptName;

  @ExcelCollection(name = "员工信息")
  private List emps;


	....省略getter、setter方法

2、EmpUtil类

@ExcelTarget("empUtil")
public class EmpUtil{

  @Excel(name = "序号", width = 30, isColumnHidden = true)
  private Integer id;

  @Excel(name = "员工姓名", width = 30, groupName = "基本信息")
  private String empName;

  @Excel(name = "年龄", width = 30, type = 10, groupName = "基本信息")
  private Integer age;

  @Excel(name = "入职时间", width = 30, groupName = "工作信息", format = "yyyy/MM/dd HH:mm")
  private Date hiredate;

  @Excel(name = "薪酬", width = 30, type = 10, groupName = "工作信息")
  private BigDecimal salary;


	....省略getter、setter方法

3、核心代码

public String export(){

    Workbook workBook = null;
    try {
      List exportList = exportService.exportList();
      System.err.println(JSONArray.toJSONString(exportList));

      // 创建参数对象(用来设定excel得sheet得内容等信息)
      ExportParams deptExportParams = new ExportParams();
      // 设置sheet得名称
      deptExportParams.setSheetName("员工报表1");
      // 创建sheet1使用得map
      Map deptExportMap = new HashMap<>();
      // title的参数为ExportParams类型,目前仅仅在ExportParams中设置了sheetName
      deptExportMap.put("title", deptExportParams);
      // 模版导出对应得实体类型
      deptExportMap.put("entity", DeptUtil.class);
      // sheet中要填充得数据
      deptExportMap.put("data", exportList);

      ExportParams empExportParams = new ExportParams();
      empExportParams.setSheetName("员工报表2");
      // 创建sheet2使用得map
      Map empExportMap = new HashMap<>();
      empExportMap.put("title", empExportParams);
      empExportMap.put("entity", DeptUtil.class);
      empExportMap.put("data", exportList);

      // 将sheet1、sheet2、sheet3使用得map进行包装
      List> sheetsList = new ArrayList<>();
      sheetsList.add(deptExportMap);
      sheetsList.add(empExportMap);
      // 执行方法
      workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
      fileName = URLEncoder.encode("员工报表导出", "UTF-8");
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      workBook.write(outputStream);
      outputStream.flush();
      byte[] byteArray = outputStream.toByteArray();
      excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length);
      outputStream.close();

    }catch (Exception e){
      e.printStackTrace();
    }finally {
      if(workBook != null) {
        try {
          workBook.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return "success";
  }

三、效果展示

如何在Java中利用EasyPoi实现一个sheet导出功能

有一个问题就是如果sheet填充的数据源是一样的,那么第二个sheet的内容就会为空


如何在Java中利用EasyPoi实现一个sheet导出功能

不过一般上要实现多sheet页展示,内容应该是不一样的,这里只是作为实现效果展示,就采用了同一个数据获取源。

关于如何在Java中利用EasyPoi实现一个sheet导出功能问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。


分享题目:如何在Java中利用EasyPoi实现一个sheet导出功能-创新互联
转载来于:http://bjjierui.cn/article/dooeph.html

其他资讯