Java实现Excel模板下载以及遇到的问题

Java实现Excel模板下载以及遇到的问题

前言:

项目在开发过程中,会用到Excel的导入,导出,复杂一点的Excel可以写好放在项目指定位置,下载时候直接从指定位置获取即可。

代码实现

excel存放的位置:

controller

c 复制代码
 @GetMapping("downloadxxxTemplate")
 @ApiOperation("下载xx-xx导入模板")
 public void downloadxxxTemplate(@RequestParam(value = "fileName") String fileName, HttpServletResponse response) throws IOException {
        dutyRosterService.downloadxxxTemplate(fileName,response);
    }

service

c 复制代码
   /**
     * 下载xxx-xx信息导入模板
     * 
     * @param fileName
     *            文件名
     * @param response
     *            响应
     * @throws IOException
     *             异常
     */
void downloadxxxTemplate(String fileName, HttpServletResponse response) throws IOException;

serviceImpl

c 复制代码
  public void downloadxxxTemplate(String fileName, HttpServletResponse response) throws IOException {
        InputStream inputStream = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
        	// 从类路径中的 excel 目录下获取名为 fileName.xlsx 的 Excel 文件
            inputStream = this.getClass().getClassLoader().getResourceAsStream("excel/" + fileName + ".xlsx");
            if (inputStream == null) {
               // 异常
            }
            bis = new BufferedInputStream(inputStream);
            fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
            // 设置文件下载头
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            // 设置文件ContentType类型,这样设置,会自动判断下载文件类型
            response.setContentType("multipart/form-data");
            bos = new BufferedOutputStream(response.getOutputStream());
            int len = 0;
            while ((len = bis.read()) != -1) {
                bos.write(len);
                bos.flush();
            }
            bos.close();
            bis.close();
            inputStream.close();
        } catch (IOException e) {
        	// 异常
        } finally {
            if (bos != null) {
                bos.close();
            }
            if (bis != null) {
                bis.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    }

遇到的问题:

问题一:

excel 存放在resources的excel目录下,一直没有获取到报空指针异常,如下图,但是修改后还是空

从类路径中获取指定名称的资源,并返回一个输入流用于读取该资源的内容。

改为:this.getClass().getClassLoader().getResourceAsStream("excel/" + fileName + ".xlsx");

问题二:

打包 无论是package还是install 在target中excel都没有打进去,如下图:

最后找资料需要在maven依赖添加如下配置即可:

c 复制代码
	
    <build>
        <resources>
        	// 用自己话理解 可能有误
        	// 配置maven打包时候,将指定目录下面的文件打到target里面 
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
	//  maven打包Excel等资源时候,使用maven的filter,导致打包的Excel文件乱码或者损坏添加如下配置即可
		<plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xls</nonFilteredFileExtension>
                        <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
        </plugins>

再次package时,target多个excel文件夹如下,问题解决

喜欢我的文章的话,点个阅读或者点个点赞,是我编写博客的动力,持续更新中

相关推荐
liliangcsdn1 分钟前
python如何写数据到excel示例
开发语言·python·excel
CNRio3 分钟前
将word和excel快速转换为markdown格式
python·word·excel
workflower3 小时前
单元测试-例子
java·开发语言·算法·django·个人开发·结对编程
YuanlongWang3 小时前
C# 基础——装箱和拆箱
java·开发语言·c#
b78gb3 小时前
电商秒杀系统设计 Java+MySQL实现高并发库存管理与订单处理
java·开发语言·mysql
wb043072014 小时前
性能优化实战:基于方法执行监控与AI调用链分析
java·人工智能·spring boot·语言模型·性能优化
天若有情6736 小时前
Java Swing 实战:从零打造经典黄金矿工游戏
java·后端·游戏·黄金矿工·swin
lichong9516 小时前
Android studio 修改包名
android·java·前端·ide·android studio·大前端·大前端++
lichong9516 小时前
Git 检出到HEAD 再修改提交commit 会消失解决方案
java·前端·git·python·github·大前端·大前端++
@yanyu6666 小时前
Tomcat安装与HTML响应实战
java·tomcat·html