springboot project realization excel static template download

Static excel template download, there are two ways: First, specify the output file stream in the http header type is "application / vnd.ms-excel" type, you do not need to add a suffix name of the output file output stream; when the second type of the specified file stream is "multipart / form-data", it needs to be determined when the output stream file .xls / .xlsx, and the suffix name.

1, way:

level code controller:

  @GetMapping ( "/ download" )
     public the Result <String> downLoadTemplate (HttpServletResponse the Response) { 
            
    // get the resource template file of 
       a ClassPathResource Resource = new new a ClassPathResource ( "/ Templates / Excel / Yongjin payment schedule template data .xls" ) ; 
       InputStream inputStream = resource.getInputStream ();
        // create an object based on Excel 
        Workbook Workbook = WorkbookFactory.create (inputStream); 
   String fileName = "definition file name"; ExcelUtil.downLoadExcel (fileName, the Response, Workbook);
  }
ExcelUtil tools Code:
public class ExcelUtil {
  //模板下载工具类
  private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {    try { response.setCharacterEncoding("UTF-8"); response.setHeader("content-Type", "application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); workbook.write(response.getOutputStream()); } catch (IOException e) { // throw new NormalException(e.getMessage()); } } }

2. Second way: Tools can extract integration

  @GetMapping ( "/ download" )
     public the Result <String> downLoadTemplate (HttpServletResponse the Response) {
         the try {
             // get the resource template file of 
            a ClassPathResource Resource = new new a ClassPathResource ( "/ Templates / Excel / payment schedule template .xls data Yongjin " ); 
            InputStream inputStream = resource.getInputStream ();
             // create different objects depending on excel, Excel2003 version -> HSSFWorkbook, Excel2007 version -> XSSFWorkbook 
            Workbook wb = WorkbookFactory.create (inputStream);         
            response.reset (); 
            the response.setContentType ( "multipart / form-Data");
             // determine excel file types, download the acquired template and rename 
            System.out.println (wb.getClass () getSimpleName ());.
             IF .. (Wb.getClass () getSimpleName () the equals ( "HSSFWorkbook " )) { 
                response.setHeader ( " the Content-Disposition " ,
                         " Attachment; filename = "+ new new String (" definition file name ".getBytes (" UTF-8 " )," iso8859-1 ") +" .xls " ); 
            } the else { 
                response.setHeader ( "the Content-Disposition" ,
                         "Attachment; filename =" + new new String ( "definition file name."getBytes("UTF-8"), "iso8859-1") + ".xlsx");
            }
            wb.write(response.getOutputStream());
  }

Note: Whether it is a way or two, all the way to export a static template, the template will need to be placed under the template resource folder, as shown below:

 

Guess you like

Origin www.cnblogs.com/H-Dream/p/11408368.html