当前位置:   article > 正文

springboot下载excel模板,excel表损坏数据错误_springboot编译后classes的excel打开损坏

springboot编译后classes的excel打开损坏

问题

在maven编译时excel模板表就已经被损坏了,下载一个已经被损坏的Excel表肯定是无法正常打开的,数据乱码错误

为啥Excel表在编译后会被损坏,这里就涉及到maven resource标签的使用。

pom.xml 添加

image.png
将excel格式文件排除,避免造成编译导致excel文件损坏

	<build>
        <finalName>sim-api</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>**/*.xls</exclude>
                    <exclude>**/*.xlsx</exclude>
                </excludes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xls</include>
                    <include>**/*.xlsx</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.gaolei.sim.Application</mainClass>
                    <layout>JAR</layout>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

控制器

@RequestMapping(value = "/download", method=RequestMethod.POST, produces = { "application/json;" })
    @ApiOperation(value = "下载风险研判信息记录模板", httpMethod = "POST", notes = "下载风险研判信息记录模板")
    public void download(HttpServletResponse res, HttpServletRequest req) throws Exception{
    String fileName ="设备导出.xls";
    String filePath = getClass().getResource("/static/" + fileName).getPath();
    filePath = URLDecoder.decode(filePath, "UTF-8");
    OutputStream os = res.getOutputStream();
    InputStream in =new FileInputStream(filePath) ;
    //InputStream in = getClass().getClassLoader().getResourceAsStream("/static/"+fileName);

    res.setHeader("Content-Length", String.valueOf(in.available()));
    res.setContentType("multipart/form-data");
    res.addHeader("Content-Disposition","attachment;fileName=" + new String(fileName.getBytes("GBK"),"UTF-8"));
    IOUtils.copy(in,os);
    os.flush();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

参考文章: https://blog.csdn.net/qq_42270377/article/details/92771349

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/815985
推荐阅读
相关标签
  

闽ICP备14008679号