File upload and download (a) import template download

public void downloadExcel(HttpServletRequest request,HttpServletResponse response){
        response.reset();
        the response.setContentType ( "file application / X-msdownload; GBK charset =" );
         // export file name 
        String excelName = "Import template" ;
         // change the encoding format export file name 
        String fileName = null ;
         the try {
            fileName = new String(excelName.getBytes("gb2312"), "ISO-8859-1");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\"");
            // 获取模板路径
            String realPath= this.getClass().getResource("/").getPath().replaceFirst("/", "");
            TemplateRealPath String = realpath + "/ Template / Import Template .xls"; // files in the webapp template folder 
            File Excel = new new File (templateRealPath);
            FileInputStream is = null;
            is = new FileInputStream(excel);
            HSSFWorkbook wb = new HSSFWorkbook(is);
            Sheet HSSFSheet = wb.getSheetAt (0); // First 
            HSSFFont font = wb.createFont ();
            font.setFontHeightInPoints((short)10);
            font.setFontName ( "Song" );
            HSSFCellStyle style = wb.createCellStyle();
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        
}

Guess you like

Origin www.cnblogs.com/DarGi2019/p/12071294.html