현재 브라우저에서 excel 템플릿 다운로드

2918 단어 상용 기술
1. xlsx:
@PostMapping(value = "userTemplate")
    public void userTemplate(HttpServletResponse response, HttpServletRequest request) {
        File file = new File(this.getClass().getClassLoader().getResource("").getPath() + File.separator + "templates" + File.separator + "userTemplate.xlsx");
        SXSSFWorkbook sxssfWorkbook = null;
        try {
            InputStream is = new FileInputStream(file);
            sxssfWorkbook = new SXSSFWorkbook( (XSSFWorkbook) WorkbookFactory.create(is) );
            //   。
            response.setHeader("Charset", "UTF-8");
            response.setHeader("Content-Type", "application/force-download");
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(" " , "utf-8")+ ".xlsx");
            response.flushBuffer();
            OutputStream outputStream = response.getOutputStream();
            sxssfWorkbook.write(outputStream);
            //   。
            sxssfWorkbook.dispose();
        } catch (Exception e) {
           
        } finally {
            if (null != sxssfWorkbook){
                try {
                    sxssfWorkbook.close();
                }catch (Exception e){
                   
                }
            }
        }
    }

2, xls:
@PostMapping(value = "userTemplate")
    public void userTemplate(HttpServletResponse response, HttpServletRequest request) {
        File file = new File(this.getClass().getClassLoader().getResource("").getPath() + File.separator + "templates" + File.separator + "userTemplate.xls");
        HSSFWorkbook sxssfWorkbook = null;
        try {
            InputStream is = new FileInputStream(file);
            sxssfWorkbook = new HSSFWorkbook(is);
            //   。
            response.setHeader("Charset", "UTF-8");
            response.setHeader("Content-Type", "application/force-download");
            response.setHeader("Content-Type", "application/vnd.ms-excel");
            response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(" " , "utf-8")+ ".xls");
            response.flushBuffer();
            OutputStream outputStream = response.getOutputStream();
            sxssfWorkbook.write(outputStream);
            //   。
            //sxssfWorkbook.dispose();
        } catch (Exception e) {
           
        } finally {
            if (null != sxssfWorkbook){
                try {
                    sxssfWorkbook.close();
                }catch (Exception e){
                   
                }
            }
        }
    }

좋은 웹페이지 즐겨찾기