2017-01-09 57 views
0

我想下載創建的excel文件。我正在使用POI。此北京時間我的代碼:使用HttpServlet彈出下載excel

response.setContentType("application/vnd.ms-excel"); 
    response.setHeader("Content-Disposition", "attachment; filename=" + filename); 

    OutputStream out = response.getOutputStream(); 

    HSSFWorkbook workbook = new HSSFWorkbook(); 
    ... // add some sheets 


    workbook.write(out); 

這是我RestService

@Autowired 
Excel excel; 

@RequestMapping(path = "/excel/{testId}", method = RequestMethod.GET) 
public ResponseEntity createFile(HttpServletResponse response, @PathVariable Integer testId) { 


    try { 
     excel.createFile(response, testId); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return new ResponseEntity(HttpStatus.OK); 


} 

這是結果: enter image description here

存儲文件的本地作品。

謝謝!

+0

您需要提供比這更多的代碼。你如何創建工作簿?你的spring控制器或servlet的完整方法是什麼? –

+0

嘿我編輯我的答案希望它會幫助 – 1thingtodo

回答

0

需要設置的HttpResponse對象

適當的值對於.xls文件

response.setHeader( 「內容類型」,應用程序/ vnd.ms-EXCEL) ;

response.setCharacterEncoding(「UTF-8」);

對於Excel2007中和上述的.xlsx文件

response.setCharacterEncoding( 「UTF-8」);

response.setHeader(「Content-Type」,「application/vnd.openxmlformats-officedocument.spreadsheetml.sheet」);

0
public class Admin_SkillsExcelBuilder extends AbstractXlsView{ 
    @Override 
    protected void buildExcelDocument(Map<String, Object> model, 
      Workbook workbook,HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 

     // change the file name 
     response.setHeader("Content-Disposition", "attachment; filename=\"Skills.xls\""); 

     // get data model which is passed by the Spring container 
     List<SkillsVO> skillsList = (List<SkillsVO>) model.get("skillsList"); 

     // create a new Excel sheet 
     Sheet sheet = workbook.createSheet("Skills"); 
     sheet.setDefaultColumnWidth(30); 

     // create header row 
     Row header = sheet.createRow(0); 
     header.createCell(0).setCellValue("SNO"); 
     header.createCell(1).setCellValue("SkillName"); 
     header.createCell(2).setCellValue("SkillType"); 


     // Create data cells 
     int rowCount = 1; 
     int count=1; 
     for (SkillsVO skillsList1 : skillsList){ 

      Row courseRow = sheet.createRow(rowCount++); 
      courseRow.createCell(0).setCellValue(count); 
      courseRow.createCell(1).setCellValue(skillsList1.getSkill()); 
      courseRow.createCell(2).setCellValue(skillsList1.getSkillType()); 
      count=count+1; 
     } 
    }  
} 
0
@RequestMapping(value = "/downloadSkillsAsExcel", method = RequestMethod.GET) 
    public ModelAndView downloadSkillsExcel(HttpSession session, ModelMap model) { 
     logger.info("Entered in to downloadSkillsExcel() method in Controller"); 
     try{ 

     List<SkillsVO> skillsList = keywordsService.getAllSkills("default"); 
     model.addAttribute("skillsList", skillsList); 
     return new ModelAndView("SkillsExcelView"); 
    } 
     catch(Exception e){ 
      logger.error(ExceptionUtil.getExceptionMessage(e)); 
     return new ModelAndView("admin_pages/404"); 
    } 
    } 
0

下面是使用Spring MVC的

1.創建AS-IS POJO類(或域類)

public class Person{ 

private String name; 

private int age; 

//Getters and Setters 
} 

2〜下載Excel文件的步驟。創建服務類並擴展AbstractExcelView類

public class MyExcelServiceClass extends AbstractExcelView{ 

     @Override 
     protected void buildExcelDocument(Map model, HSSFWorkbook workbook, 
      HttpServletRequest request, HttpServletResponse response) 
      throws Exception { 

      //Now you have Excel WorkBook Object available for processing Excel file using POI API 

     } 

}

+0

嗨,如我所說的存儲文件本地作品。答覆中一定有錯誤。 1thingtodo – 1thingtodo