Spring boot 如何提供excel 模板下载

Controller层:

@RequestMapping("/admin/model")
public ResponseEntity<byte[]> download2() throws IOException {
    File file = excelModelService.buildXlsById();
    return ResponseUtils.buildResponseEntity(file);
}

Service层:

import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;

/**
 * @author 赵鑫
 * @Time: 2018/7/22
 * @Email:[email protected]
 */
@Service
public class ExcelModelServiceImpl {
    public File buildXlsById(){
        //do something to find this file
        File file=null;
        try {
             file = ResourceUtils.getFile("classpath:static/model.xlsx");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return file;
    }
}

页面代码

<a href="${request.contextPath}/admin/model">下载模板</a>

文件所放位置

猜你喜欢

转载自blog.csdn.net/qq_37668945/article/details/81170447