Easypoi excel simple export

Use easypoi for simple excel export

1. After the project environment is configured, add EasyPoi dependency in the pom file

        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>4.1.0</version>
        </dependency>

2. Add annotations to the entity class corresponding to the table to be exported

public class Applys {
    
    
    @ExcelIgnore
    private Integer id;
    @Excel(name="考试批次")
    private String examNo;
    @Excel(name="姓名")
    private String realName;
    @Excel(name="性别")
    private String sex;
    @Excel(name="身份证号")
    private String idNo;
    @Excel(name="籍贯")
    private String homeTown;
    @Excel(name="文化程度")
    private String highestDegree;
    @Excel(name="从业年限")
    private String workYears;
    @Excel(name="职业工种")
    private String workTypeName;
    @ExcelIgnore
    private String workName;
    @Excel(name="工种代码")
    private Integer workTypeCode;
    @Excel(name="技能等级")
    private String skillGrade;
    @Excel(name="等级代码")
    private String skillGradeCode;
    @Excel(name="工作单位")
    private String workCompany;

    @ExcelIgnore
    private Date createDate;
    @ExcelIgnore
    private String phoneNo;
    @ExcelIgnore
    private Integer createUserId;
    @ExcelIgnore
    private String state;
}

@Excel(name="考试批次")This annotation indicates that this field corresponds to the header name in the Excel table.
@ExcelIgnoreThis annotation indicates that this field is not imported or exported.
3. dao layer

    /**
     * 查询所有数据
     * @return
     * @author
     */
    List<Applys> queryAll();

4. dao implementation layer

 <sql id="Base_Column_List">
    id, real_name, sex, id_no, home_town, highest_degree, work_years, work_type_name, 
    work_name, work_type_code, skill_grade, skill_grade_code, work_company, exam_no, 
    create_date, phone_no, create_user_id, state
  </sql>
  <!--查询所有-->
  <select id="queryAll" resultType="com.example.excel.entity.Applys">
    select
    <include refid="Base_Column_List" />
    from apply
  </select>

5. service and service implementation

   /**
     * 查询所有数据
     * @return
     * @author
     */
    List<Applys> queryAll();
   @Override
    public List<Applys> queryAll() {
    
    
        return dao.queryAll();
    }

6. Controller layer

    /**
     * 导出数据
     * @param response
     */
    @PostMapping(value = "/export",produces = "application/octet-stream")
    public void exportTsgz(HttpServletResponse response){
    
    
        List<Applys> list = ApplyService.queryAll();
        ExportParams params = new ExportParams("报名表","报名表", ExcelType.HSSF);
        Workbook workbook = ExcelExportUtil.exportExcel(params, Applys.class, list);
        ServletOutputStream out = null;
        try {
    
    
            //流的形式传输数据
            response.setHeader("content-type","application/octet-stream");
            //防止中文乱码
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode("报名表.xls","UTF-8"));
            out=response.getOutputStream();
            workbook.write(out);
        }catch (IOException e){
    
    
            e.printStackTrace();
        }finally {
    
    
            if (null!=out){
    
    
                try {
    
    
                    out.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }

7. Front-end page

       <div class="layui-btn-container">
              <button type="button" class="layui-btn btdisabled" id="export">开始导出</button>
       </div>

js code

        var postDownLoadFile = function (options) {
    
    
            var config = $.extend(true, {
    
     method: 'post' }, options);
            var $iframe = $('<iframe id="down-file-iframe" />');
            var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
            $form.attr('action', config.url);
            for (var key in config.data) {
    
    
                $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
            }
            $iframe.append($form);
            $(document.body).append($iframe);
            $form[0].submit();
            $iframe.remove();
        }

        $("#export").click(function() {
    
    
            var url = 'excel/export';
            var param={
    
    };
            postDownLoadFile({
    
    
                url:url,
                data:param,
                method:'post'
            });
        });

Source code: https://gitee.com/zhangxjgg/excel.git

Acho que você gosta

Origin blog.csdn.net/qq_42042158/article/details/121960749
Recomendado
Clasificación