Easypoi realizes the export and import of excel

Easypoi realizes the export and import of excel

I have been using poi to import and export before, and it is still very troublesome to write. Although I can paste and copy the same code, it is a big deal after all.

So try to use easypoi to do excel import and export. Gossiping less and start working ...

Step 1: Guide package
  <!--easypoi需要导包的-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
The second step is to write the entity class, import and export annotations, the entity class should be written without deviation (emphasis)
Annotation attribute introduction:

name specified generating export excel column name
a position corresponding to the attribute orderNum may specify derived where the column
width to the width of the cell
type disposed derived type a text, 2 is a picture, 3 is a function, 10 numeral default text
replace: Worth replacing example: replace = {"ID_1"} The database value is "1", it will be automatically replaced by "ID" when exporting
Example: @Excel (name = "Certificate Type", replace = {"ID_ 1 ”}, orderNum =“ 1 ”)
/ **
* Export Data date to yyyy-MM-dd format
* mergeVertical sets whether to merge columns vertically
* mergeRely sets the prerequisites for merging columns, that is: only when the index is 2 columns (Ie: "gender" column) has been
merged, then when the vertical length of this column is the same, it can be merged vertically; if the
* column cited as 2 (ie: "gender" column) has different vertical data, then this is the case The vertical data of the columns are the same, so
* will not be merged
* /
@Excel (name = "time of entering school", mergeVertical = true, mergeRely = {2}, format = "yyyy-MM-dd", orderNum = "5", width = 20)

package com.sinux.easypoi.entity;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Admin
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class NewExcelOperateEntity {
/**
 * @Excel注解的一些常用属性
 * name:列名
 * orderNum:第几列
 * replace:值得替换 例:replace = {"身份证_1"} 数据库值为"1",导出时会自动被"身份证"代
 */
    @Excel(name="姓名",orderNum = "0")
    private String name;
    @Excel(name="证件类型",replace = {"身份证_1"},orderNum = "1")
    private String identifyType;
    @Excel(name = "证件号码", orderNum = "2")
    private String identifyNo;
    @Excel(name = "手机号1", orderNum = "3",mergeVertical = true)
    private String phoneA;
    @Excel(name = "手机号2", orderNum = "4",mergeVertical = true)
    private String phoneB;
    @Excel(name = "手机号3", orderNum = "5",mergeVertical = true)
    private String phoneC;
    @Excel(name = "固定电话", orderNum = "6",mergeVertical = true)
    private String telephone;
    @Excel(name = "电子邮箱", orderNum = "7",mergeVertical = true)
    private String email;
    @Excel(name = "身份证地址", orderNum = "8",mergeVertical = true)
    private String idcardAdress;
    @Excel(name = "户籍地址", orderNum = "9",mergeVertical = true)
    private String householdAddress;
    @Excel(name = "居住地址", orderNum = "10",mergeVertical = true)
    private String liveAddress;
    @Excel(name = "工作地址", orderNum = "11",mergeVertical = true)
    private String workAddress;
}
Step 3: Export
 @GetMapping("/export")
    public void exportExcel(HttpServletResponse response){
        List<NewExcelOperateEntity> list = new ArrayList<>();
        NewExcelOperateEntity newExcelOperateEntity = null;
        Random random = new Random();
        //造数据
        for(int i=0;i<10;i++){
            newExcelOperateEntity = new NewExcelOperateEntity("姓名"+i,
                    "1",
                    random.nextInt(3)+i+"",
                    random.nextInt(3)+i+"",
                    random.nextInt(3)+i+"",
                    random.nextInt(3)+i+"",
                    "固定电话"+i,
                    "电子邮箱"+i,
                    "身份证地址"+i,
                    "户籍地址"+i,
                    "居住地址"+i,
                    "工作地址"+i);
            list.add(newExcelOperateEntity);
        }
        ExportParams exportParams = new ExportParams();
        exportParams.setSheetName("I am sheetName");
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams,NewExcelOperateEntity.class,list);
        try {
            response.setHeader("content-Type", "application/vnd.ms-excel");
            // 下载文件的默认名称
            response.setHeader("Content-Disposition", "attachment;filename=" +
                    URLEncoder.encode("用户数据表","UTF-8") + "**.xlsx**");
            workbook.write(response.getOutputStream());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
Step 4: Import
 @RequestMapping("/import")
    public boolean importExcel(MultipartFile file){
        ImportParams importParams = new ImportParams();
        List<NewExcelOperateEntity> list = new ArrayList<>();
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(),NewExcelOperateEntity.class,importParams);
            for(NewExcelOperateEntity s : list){
                System.out.println(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
Published 67 original articles · Liked12 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/m0_37635053/article/details/104999640