Spring使用HttpServlet下载excel

时间:2017-01-09 19:12:18

标签: excel spring spring-mvc apache-poi

我想下载创建的excel文件。我正在使用POI。这是我的代码:

 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

存储本地文件。

谢谢!

4 个答案:

答案 0 :(得分:0)

您需要设置HttpResponse对象的正确值

实施例

适用于.Xls文件

response.setHeader(" Content-Type",application / vnd.ms-excel);

response.setCharacterEncoding(" UTF-8&#34);

适用于Excel2007及以上.xlsx文件

response.setCharacterEncoding(" UTF-8&#34);

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

答案 1 :(得分: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;
        }
    }      
}

答案 2 :(得分: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");
    }
    }

答案 3 :(得分:0)

以下是使用Spring MVC下载excel文件的步骤

<强> 1。创建AS-IS POJO类(或域类)

public class Person{

private String name;

private int age;

//Getters and Setters
}

<强> 2。创建服务类并扩展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

      }

}