easypoi导入数据并存库

需求:excel表格导入数据,然后做匹配存库

  1. 自定义导入实体类
@ExcelTarget(value = "appPovertyRelief")
public class AppPovertyRelief extends BPSResult {
    //主键
    @TableId(value = "id", type = IdType.AUTO)
    private String id;
    /**
     * 产品码
     **/
    private String productId;
    /**
     * 电话号码
     **/
    @Excel(name = "电话号码", orderNum = "1")
    private String phone;
    /**
     * 扶贫账号
     **/
    @Excel(name = "扶贫账号", orderNum = "2")
    private String account;

@Excel(name = "表格列表头名", orderNum = "位置")
2.导入表格处理

String filePath = "/data/workspace/manager-web/" + System.currentTimeMillis() + ".xlsx";
FileOutputStream out = new FileOutputStream(filePath);
out.write(file.getBytes());
out.flush();
out.close();
ImportParams params = new ImportParams();
params.setTitleRows(1);
params.setHeadRows(1);
InputStream inputStream = new FileInputStream(new File(filePath));
List<AppPovertyRelief> list = ExcelImportUtil.importExcel(inputStream, AppPovertyRelief.class, params);
// 保存流程自行书写

项目需要先存跟 所以做保存处理 若无需保存

//file 是spring的MultipartFile对象  一行代码搞定
ImportParams params = new ImportParams();
//标题行占格数
params.setTitleRows(1);
// 表头行占格数
params.setHeadRows(1);
List<AppPovertyRelief> list = ExcelImportUtil.importExcel(file.getInputStream(), AppPovertyRelief.class, params);
  1. 模板字段跟实体类定义的对齐
    image.png
# java  

评论

企鹅群:39438021

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×