Java download excel template file

I. Introduction

Recently, I made a function to import Excel, and the requirements are:

  1. First provide a function to download the Excel template.
  2. After the user downloads the template, he can fill in the content to be uploaded in the template file, and then import it into Excel, and then save the data filled in by the user to the database.

2. Download the template

1. Put the template in the resources directory, try to create a special folder to store the template, as follows:

2. Here I use two dependencies, one is the most popular tool class of hutool, and easyexcel is basically used for importing and exporting Excel.

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.18</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.2.1</version>
</dependency>
@GetMapping("/downInChargeOfTemplate")
public void downInChargeOfTemplate(HttpServletResponse response) {
    
    
    downloadService.downInChargeOfTemplate(response);
}
import cn.hutool.core.io.IoUtil;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

@Service
public class DownloadService {
    
    
    public void downInChargeOfTemplate(HttpServletResponse response) {
    
    
        responseSetting(response, "各分任务负责人导入模板", ".xlsx",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
    
    
            // 读取文件的输入流
            inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("templates/各分任务负责人导入模板.xlsx");
            XSSFWorkbook wb = new XSSFWorkbook(inputStream);
            outputStream = response.getOutputStream();
            wb.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            IoUtil.close(inputStream);
            IoUtil.close(outputStream);
        }
    }

    public void responseSetting(HttpServletResponse response, String fileName, String suffix, String contentType) {
    
    
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String newFileName = null;
        try {
    
    
            newFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
        }
        // 当客户端请求的资源是一个可下载的资源(这里的“可下载”是指浏览器会弹出下载框或者下载界面)时,对这个可下载资源的描述(例如下载框中的文件名称)就是来源于该头域。
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + newFileName + suffix);
        // 服务器告诉浏览器它发送的数据属于什么文件类型,也就是响应数据的MIME类型
        response.setContentType(contentType);
        response.setCharacterEncoding("utf-8");
        // 关闭缓存(HTTP/1.1)
        response.setHeader("Cache-Control", "no-store");
        // 关闭缓存(HTTP/1.0)
        response.setHeader("Pragma", "no-cache");
        // 缓存有效时间
        response.setDateHeader("Expires", 0);
    }
}

3. Test interface

4. Supplementary knowledge

After getting the InputStream input stream of the file, we can also write the input stream to the output stream without using XSSFWorkbook, and can directly use the stream copy method, which can also complete the export template function.

insert image description here

3. Excel import

For the import function, just refer to the official website of easyexcel: https://easyexcel.opensource.alibaba.com/docs/current/quickstart/read

Guess you like

Origin blog.csdn.net/weixin_43888891/article/details/130108377#comments_27828754