导入excel文档,并且做业务处理,然后返回包含导入结果的excel文档

一、依赖

<easyexcel.version>3.3.4</easyexcel.version>
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>easyexcel</artifactId>
	<version>${easyexcel.version}</version>
</dependency>

二、编写接口

/**
     * 从Excel导入字段标准列表
     */
    @ApiOperation("从Excel导入字段标准列表")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public HttpResultVO<String> importMemberList(@RequestParam("file") MultipartFile file,
                                 @RequestParam("importStrategy") String importStrategy)
            throws IOException {
        List<ColumnStandardImportDTO> list = EasyExcel.read(file.getInputStream())
                .head(ColumnStandardImportDTO.class)
                .sheet(0)
                .doReadSync();
        // dataGovColumnStandardService.importData(list, importStrategy);
        // 将结果生成excel文件
        //创建一个流,等待写入excel文件内容
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //将excel文件写入byteArrayOutputStream中
        EasyExcel.write(byteArrayOutputStream, ColumnStandardImportDTO.class)
                .sheet("字段标准").doWrite(list);
        //创建inputStream流
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        // 将结果excel文件存到minio上
        try {
            String fileName = file.getOriginalFilename();
            String[] fileNameArray = fileName.split("\\.");
            String filePrefix = fileNameArray[fileNameArray.length-1];
            // 原文件名
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String date = sdf.format(new Date());
            String sb = "/data-gov/columnStandard/"+date.split("-")[0]+"/"
                    +date.split("-")[1]+"/"
                    +date.split("-")[2]+"/"
                    + UUID.randomUUID().toString().replaceAll("-","")
                    + filePrefix;

            myMinioClient.getInstance().putObject(
                    PutObjectArgs.builder()
                            .bucket(minioConfig.getBucket()) // 桶名称
                            .object(sb) // 文件存储名称
                            .stream(inputStream, byteArrayOutputStream.size(), -1)
                            .contentType(file.getContentType())
                            .build());
            String excelUrl = minioConfig.getEndpoint() + "/" + minioConfig.getBucket() + "/" + sb;
            return HttpResultVO.success(excelUrl);
        } catch (Exception e) {
            log.error("Error occurred: " , e);
            AssertUtil.throwException(true, "excel上传失败!");
        }
        return HttpResultVO.error("导入失败");
    }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值