internship:编写excel表的上传方法(导入)

整个项目的框架搭建好之后,对于程序员只需要着手于业务的编写即可。比如一般的excel表的导入导出。比如人员的excel表的导入导出。

以下是导入(把excel数据上传实现与数据库的数据交互):

需要依据数据库表和excel表对应起来建类,类似于以下的编写形式

public class governmentsModel extends Model<governmentsModel> {



    private static long serialVersionUID=1L;


    @TableField(value = "id",type= IdType.AUTO)
    private Integer id;

    /**
     *......
     */
    @Excel(name = "",width =25 )
    @TableField("name")
    @ApiModelProperty(value = "")
    private String name;

    /**
     *......
     */
    @Excel(name = "",width =25 )
    @TableField("code")
    @ApiModelProperty(value = "")
    private String code;


    /**
     * 
     */
    @TableField("type")
    private Integer type;

    /**
     *....
     */
    @Excel(name = "",width =50 )
    @TableField("gb")
    @ApiModelProperty(value = "")
    private String gb;

    /**
     *.....
     */
    @Excel(name = "",width =50 )
    @TableField("category_code")
    @ApiModelProperty(value = "")
    private String category_code;

 
 ....................


}

@excel的注解就是在对应着excel表里的字段方便形成excel表。

类建好之后,进行编写上传方法,如下:

/**
     * 上传人员excel
     *
     * @param file
     * @return
     */
    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
    public ResponseData uploadExcel(HttpServletRequest request, MultipartFile file) {
        ImportParams importParams = new ImportParams();
        importParams.setTitleRows(1);
        List<governmentsModel> result;
        try {
            result = ExcelImportUtil.importExcel(file.getInputStream(), governmentsModel.class, importParams);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(500, "表格导入错误!");
        }
        //保存员工信息
        if (ToolUtil.isEmpty(result)) {
            throw new BusinessException(BizExceptionEnum.EXCEL_EMPTY);
        }
        List<governmentsModel> errorList = employerService.importExcel(result);
        request.getSession().setAttribute("employerList", errorList);
        return new ResponseData<>(errorList.size() == 0);
    }

逐行解读代码段:
1、method自然采取post 因为是导入需要上传excel表,ResponseData类是为了方便统一前后端的数据格式。
2、HttpServletRequest request, MultipartFile file 是上传excel表时 方法必要的传参 request意为请求——服务器请求对象 。MultipartFile——MultipartFile是SpringMVC提供简化上传操作的工具类。
在不使用框架之前,都是使用原生的HttpServletRequest来接收上传的数据,文件是以二进制流传递到后端的,然后需要我们自己转换为File类。使用了MultipartFile工具类之后,我们对文件上传的操作就简便许多了。
3、ImportParams 中文译为导入参数 配合 importParams.setTitleRows(1) 表头所占据的行数默认1,代表标题占据一行
4、用集合存储excel表数据,try catch异常捕获 通过ExcelImportUtil工具类 调用导入方法——importExcel 传入文件输入流参数、利用反射机制获取对应的依据数据库表和excel表对应起来建的类参数和标题占据行数
5、判断不为空之后便保存完数据 至于errorList的存在是用于在业务中判断其字段是否为null予以异常其中errorList.size() == 0返回的是布尔类型数据 为true则表明该上传的excel表完整无误。

导出:

    @RequestMapping(value = "/download", method = RequestMethod.GET)
    public void download(HttpServletResponse response) {
        try {
            ExportParams exportParams = new ExportParams("标题:指明用意");
            Workbook workbook = ExcelExportUtil.exportExcel(exportParams, governmentsModel.class, new ArrayList<>());
           
            String filename = "信息模板.xls";
            response.setContentType("application/force-download");
            response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(filename, "UTF-8"));
            OutputStream ouputStream = response.getOutputStream();
            workbook.write(ouputStream);
            ouputStream.flush();
            ouputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(BizExceptionEnum.EXCEL_LOAD_WRONG);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值