2017-01-09 58 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