zoukankan      html  css  js  c++  java
  • 下载resource下的excel文件

    1、将excel文件放项目resources目录下

    2、打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况
     <configuration>
       <encoding>UTF-8</encoding>
       <nonFilteredFileExtensions>
       <nonFilteredFileExtension>xls</nonFilteredFileExtension>
       <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
       </nonFilteredFileExtensions>
    </configuration>
    
    3、resource配置
    <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                        <include>**/*.xlsx</include>
                    </includes>
                </resource>
            </resources>
    
    4、读取resources下的xlsx文件
     try {
                //获取模板文件
                File sourceFile = ResourceUtils.getFile("classpath:bankNameTemplate.xlsx");
                //转换为文件流
                BufferedInputStream fs = new BufferedInputStream(new FileInputStream(sourceFile));             
                //指定默认下载名称
                String fileName = "XXX.xlsx";
                //配置response的头
                response.reset();
                response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
                try {
                    response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                //循环从文件中读出数据后写出,完成下载
                byte[] b = new byte[1024];
                int len;
                while ((len = fs.read(b)) != -1) {
                    response.getOutputStream().write(b, 0, len);
                }
                fs.close();
            } catch (Exception e) {
                logger.error("下载Excel模板异常", e);
            }
    
    5.前端请求
      window.location.href = "/exportExcelTemplate";  
    
  • 相关阅读:
    CodeForces 452C Magic Trick (排列组合)
    zoj 3209 Treasure Map(精确覆盖)
    POJ 1459 Power Network(网络流 最大流 多起点,多汇点)
    POJ 1273 Drainage Ditches(网络流 最大流)
    HDU Tickets(简单的dp递推)
    ZOJ 3080 ChiBi(spfa)
    URAL 1036(dp+高精度)
    最佳的 清楚浮动 clearfix
    响应式开发
    javascript 性能优化
  • 原文地址:https://www.cnblogs.com/lanqingyu/p/12017715.html
Copyright © 2011-2022 走看看