阿里巴巴-easyexcel 下载案例

1. pom.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.fresher.boot</groupId>
        <artifactId>spring-boot-parent-starter</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.fresher.kkk</groupId>
    <artifactId>kkk-bff-cater</artifactId>
    <version>${release.version}</version>
    <name>kkk-bff-cater</name>
    <description>Cater Service</description>

    <!-- 环境配置 -->
    <profiles>
        <profile>
            <id>testing</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>testing</spring.profiles.active>
                <service.provider.version>1.0-testing-SNAPSHOT</service.provider.version>
                <release.version>1.0-testing-SNAPSHOT</release.version>
                <common.provider.version>1.0-testing-SNAPSHOT</common.provider.version>
            </properties>
        </profile>
        <profile>
            <id>qa</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>qa</spring.profiles.active>
                <service.provider.version>1.0-qa-SNAPSHOT</service.provider.version>
                <release.version>1.0-qa-SNAPSHOT</release.version>
                <common.provider.version>1.0-qa-SNAPSHOT</common.provider.version>
            </properties>
        </profile>
        <profile>
            <id>pre</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>pre</spring.profiles.active>
                <service.provider.version>1.0-pre-SNAPSHOT</service.provider.version>
                <release.version>1.0-pre-SNAPSHOT</release.version>
                <common.provider.version>1.0-pre-SNAPSHOT</common.provider.version>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <activation>
                <activeByDefault>false</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>pro</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

    <dependencies>
        <!-- Spring Cloud -->
        <dependency>
            <groupId>com.fresher.cloud</groupId>
            <artifactId>spring-cloud</artifactId>
            <version>1.0.2021011419</version>
        </dependency>

        <!-- Service Provider -->
        <dependency>
            <groupId>com.fresher.kkk</groupId>
            <artifactId>kkk-service-provider</artifactId>
            <version>${service.provider.version}</version>
        </dependency>

        <dependency>
            <groupId>com.fresher.common</groupId>
            <artifactId>common-provider</artifactId>
            <version>${common.provider.version}</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.7</version>
        </dependency>

        <!-- Spring Boot Test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>

                <!-- 排除日志包(log4j2),解决日志包冲突问题 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <!-- 表示在执行package打包时,执行 assembly:single,所以可以直接使用mvn package打包 -->
                    <execution>
                        <id>bundle</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <finalName>${project.artifactId}</finalName>
                            <descriptors>
                                <descriptor>${basedir}/release/assembly.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <!-- 过滤后缀文件 -->
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <!-- Nexus -->
    <repositories>
        <repository>
            <id>nexus</id>
            <url>http://139.224.21.88:8081/repository/maven-public/</url>
        </repository>
    </repositories>
</project>

2. 代码

controller

    @ApiOperation("下载")
    @GetMapping("/download")
    public void download(@Valid @Hashids ReportListRequest reportListRequest,
                         HttpServletResponse response) {
        try {
            reportService.download(reportListRequest, response);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("销售报表导出失败。msg = " + e.getMessage());
        }
    }

service
    @Override
    public void download(ReportListRequest reportListRequest, HttpServletResponse response) throws IOException {
       
        String mallName = getMallName(reportListRequest.getMallId());
        // 构造文件名
        String fileName = buildFileName(mallName);

        // 数据查询
        List<SaleRecordVO> saleRecordVOList = list(reportListRequest);

        // 汇总
        int total = getTotal(saleRecordVOList);

        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        InputStream inputStream = this.getClass().getResourceAsStream("/template" + File.separator + "report_templates.xlsx");
        ZipSecureFile.setMinInflateRatio(-1.0d);

        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .excelType(ExcelTypeEnum.XLSX)
                .withTemplate(inputStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().sheetNo(0).build();

        FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
        excelWriter.fill(saleRecordVOList, fillConfig, writeSheet);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mallName", mallName);
        map.put("dateType", getDateTypeName(reportListRequest));
        map.put("startTime", DateUtils.format(new Date(reportListRequest.getStartTime()), DateUtils.DATE_FORMAT_10));
        map.put("endTime", DateUtils.format(new Date(reportListRequest.getEndTime()), DateUtils.DATE_FORMAT_10));
        Integer itemType = reportListRequest.getItemType();
        map.put("itemType", Objects.isNull(itemType) ? BaseConstant.ALL_STR : ItemTypeEnum.of(itemType).getMessage());
        String categoryId = reportListRequest.getSpuCategoryId();
        map.put("categoryType", StringUtil.isBlank(categoryId) ? BaseConstant.ALL_STR : getCategoryName(reportListRequest.getSpuCategoryId()));
        map.put("total", total);
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值