excel文件下载

 public static void downLoadStuInfoExcel(HttpServletResponse response, HttpServletRequest request, String fileName) throws FileNotFoundException{

        //学生新建excel下载模板保存地址从配置文件中读取
//        String folderPath = ResourceBundle.getBundle("systemconfig").getString("stuExcelDownLoadPath") + File.separator + "stuTemplateExcel.xlsx";
        String folderPath = "D:/Desktop/testFile/"+fileName;
        File excelFile = new File(folderPath);
        //判断模板文件是否存在
//        if (!excelFile.exists() || !excelFile.isFile()) {
//            rt.put("status", "0");
//            rt.put("message", "模板文件不存在");
//
//            //  return rt.toJSONString();
//        }
        //文件输入流
        FileInputStream fis = null;
        XSSFWorkbook wb = null;
        //使用XSSFWorkbook对象读取excel文件
        try {
            fis = new FileInputStream(excelFile);
            wb = new XSSFWorkbook(fis);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");  // .xlsx 用这个
//            response.setContentType("application/vnd.ms-excel;charset=utf-8"); // .xls 用这个
        response.setCharacterEncoding("utf-8");

        // Content-disposition属性设置成以附件方式进行下载
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        //使Content-disposition显示在返回体里
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        // 对文件名进行处理。防止文件名乱码,这里前台直接定义了模板文件名,所以就不再次定义了
        //String fileName = CharEncodingEdit.processFileName(request, "stuTemplateExcel.xlsx");


        //调取response对象中的OutputStream对象
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

前端

//根据id去查文件名下载excel文件
        uploadByFileName(id){
          axios.post("http://localhost:8104/tax/uploadByFileName",{id:id},{responseType: "blob"})
                  .then(response => {

                    let blob = new Blob([response.data], {
                      type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
                    });
                    let url = window.URL.createObjectURL(blob); // 3.创建一个临时的url指向blob对象

                    // 4.创建url之后可以模拟对此文件对象的一系列操作,例如:预览、下载
                    let a = document.createElement("a");
                    a.href = url;
                    a.download =response.headers['content-disposition'].substr(21);
                    a.click();
                    // 5.释放这个临时的对象url
                    window.URL.revokeObjectURL(url);

                    console.log(response)
                  }).catch(err => {
                     alert("导出失败")
              })


为了在方法里设置配置文件里的路径

package com.xasz.platform.utils;

import com.xasz.platform.config.UploadConfig;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author:
 * @since: 2022-03-06
 */
@Component
public class FileUtils {

    @Autowired
    private  UploadConfig uploadConfig;

    private static UploadConfig config;

    //为了在静态方法里面得到uploadConfig
    @PostConstruct
    public void init() {
        config = uploadConfig;
    }
    public static void downLoadStuInfoExcel(HttpServletResponse response, HttpServletRequest request, String fileName) throws FileNotFoundException{
        
//        String folderPath = "D:/Desktop/testFile/"+fileName;
        String folderPath = config.savePath+fileName;
        File excelFile = new File(folderPath);

        //文件输入流
        FileInputStream fis = null;
        XSSFWorkbook wb = null;
        //使用XSSFWorkbook对象读取excel文件
        try {
            fis = new FileInputStream(excelFile);
            wb = new XSSFWorkbook(fis);
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");  // .xlsx 用这个
//            response.setContentType("application/vnd.ms-excel;charset=utf-8"); // .xls 用这个
        response.setCharacterEncoding("utf-8");
        // Content-disposition属性设置成以附件方式进行下载
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        //使Content-disposition显示在返回体里
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        // 对文件名进行处理。防止文件名乱码,这里前台直接定义了模板文件名,所以就不再次定义了
        //String fileName = CharEncodingEdit.processFileName(request, "stuTemplateExcel.xlsx");

        //调取response对象中的OutputStream对象
        OutputStream os = null;
        try {
            os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

    @Autowired
    private  UploadConfig uploadConfig;

    private static UploadConfig config;

    //为了在静态方法里面得到uploadConfig
    @PostConstruct
    public void init() {
        config = uploadConfig;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值