easypoi导入导出

easypoi导入导出

导入依赖

    <!--easypoi 相关jar -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>
    
    /**
     * @author ZJP
     * @Description   相关导出操作    @Excel 实体类注解
     * @date 2023/1/31 13:55
     * @param 
     * @return 
     */
	    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(null, "竞合伙伴", ExcelType.XSSF), CompetitionPartnerImportVo.class, 具体操作数据);
		//设置文件名
		String fileName = "竞合伙伴.xlsx";
		//定义输出类型
		res.setContentType("application/msexcel;charset=utf-8");
		//设定输出文件头
		try {
			res.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
			OutputStream out = res.getOutputStream();
			workbook.write(out);
			out.close();
		} catch (Exception e) {
			String data = e.getMessage();
			//获取OutputStream输出流
			OutputStream outputStream = res.getOutputStream();
			//通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码
			res.setHeader("content-type", "text/html;charset=UTF-8");
			//将字符转换成字节数组,指定以UTF-8编码进行转换
			byte[] dataByteArr = data.getBytes(StandardCharsets.UTF_8);
			//使用OutputStream流向客户端输出字节数组
			outputStream.write(dataByteArr);
		}
	}

     /**
     * @author ZJP
     * @Description   相关导入操作    @Excel 实体类注解
     * @date 2023/1/31 13:55
     * @param 
     * @return 
     */
String suffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf(".") + 1);
AssertUtil.isTrueForBusiness("xls".equals(suffix) || "xlsx".equals(suffix), "上传文件必为.xlsx、.xls格式,上传失败");
mportParams params = new ImportParams();
params.setHeadRows(1);//表头
List<CompetitionPartnerImportVo> competitionPartnerImportVoList = ExcelImportUtil.importExcel(file.getInputStream(), CompetitionPartnerImportVo.class, params);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值