如何从Spring MVC导出到Excel

问题描述 投票:3回答:6

我想将我从数据库中检索到的一些数据导出到控制器级别。从控制器我需要将此数据导出到Excel文件而不使用视图。

我写:

        ReportSearchVO searchL = formL.getObjReportSearchG();

        loggerG.info("Resource List:" + searchL.getResourceListG());

        projDetailReportL = reportServiceG.createProjectDetailReport(formL);

        formL.setProjDetailReport(projDetailReportL);
        formL.setReportTypeEnum(ReportTypeEnum.PROJECTDETAILREPORT);
        formL.setObjReportSearchG(searchL);

        requestR.setAttribute("resLevelForm", formL);
        returnModelAndView = new ModelAndView(
            ViewConstants.FINAL_VIEW_PROJECT_DETAILS_REPORT, "reportForm",
            formL);

但这使用了一个视图。

java spring-mvc
6个回答
3
投票

在Spring MVC中使用AbstractExcelView和ModelAndView是可能的。请参阅下文了解更多详情

http://learnfromexamples.com/generate-excel-in-spring-mvc-application-using-apache-poi/


0
投票

在过去,当我需要生成Excel文档时,我使用Apache POI来创建文件。


0
投票

Apache POI得到了春天的支持,它提供了AbstractExcelView来提供excel下载。

示例代码:

public class ExcelBuilder extends AbstractExcelView {

    @Override
    protected void buildExcelDocument(Map<String, Object> input,
            HSSFWorkbook workbook, HttpServletRequest arg2, HttpServletResponse response)
            throws Exception {
        response.setHeader("Content-Disposition", "attachment; filename=\"sample.xls\"");
         // create a new Excel sheet        
         HSSFSheet sheet = workbook.createSheet("Test");        
         sheet.setDefaultColumnWidth(30);                 
         // create style for header cells        
         CellStyle style = workbook.createCellStyle();       
         Font font = workbook.createFont();       
         font.setFontName("Arial");        
         style.setFillForegroundColor(HSSFColor.BLUE.index);      
         style.setFillPattern(CellStyle.SOLID_FOREGROUND);       
         font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);       
         font.setColor(HSSFColor.WHITE.index);      
         style.setFont(font);         
         // create header row       
         HSSFRow header = sheet.createRow(0);                
         header.createCell(0).setCellValue("Title");       
         header.getCell(0).setCellStyle(style);             
         header.createCell(1).setCellValue("col2");     
         header.getCell(1).setCellStyle(style);             
         header.createCell(2).setCellValue("col3");       
         header.getCell(2).setCellStyle(style);              
         header.createCell(3).setCellValue("col4");   
         header.getCell(3).setCellStyle(style);                
         header.createCell(4).setCellValue("col 5");      
         header.getCell(4).setCellStyle(style);
//Your data goes here
        }
    }

如果你只想要没有poi的excel下载,只需在你的jsp中设置内容dispostion头,并从控制器直接返回建议jsp的视图。请注意,当你这样做时,你只是将jsp的内容粘贴为excel中的html(也可以在Microsoft Excel上打开一个有效的文件),因此没有宏或函数可以使用它。


0
投票

这是工作

在你的控制器中

@RequestMapping(value = "/downloadExcel", method = RequestMethod.GET)
public ModelAndView downloadExcel(Model model) {

    List<String> usersGateways = uDAO.GetGwRoleUser();

    List<User> users = gatewayManagedDAO.findAll();
    return new ModelAndView(new ExcelView(), "users ", users );
    }
}

在ExcelView中

public class ExcelView extends AbstractXlsView{

@Override
public void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub


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

    @SuppressWarnings("unchecked")
    List<User> users= (List<GatewayManage>) model.get("users");

    // create excel xls sheet
    Sheet sheet = workbook.createSheet("Users Detail");
    sheet.setDefaultColumnWidth(30);

    // create style for header cells
    CellStyle style = workbook.createCellStyle();
    Font font = workbook.createFont();
    font.setFontName("Arial");
    style.setFillForegroundColor(HSSFColor.BLUE.index);
    //style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    //font.setBold(true);
    font.setColor(HSSFColor.BLACK.index);
    style.setFont(font);


    // create header row
    Row header = sheet.createRow(0);
    header.createCell(0).setCellValue("First Name");
    header.getCell(0).setCellStyle(style);
    header.createCell(1).setCellValue("Last Name");
    header.getCell(1).setCellStyle(style);
    header.createCell(2).setCellValue("Number");
    header.getCell(2).setCellStyle(style);
    header.createCell(3).setCellValue("Age");
    header.getCell(3).setCellStyle(style);



    int rowCount = 1;
    for(User user : users){
        Row userRow =  sheet.createRow(rowCount++);
        gatewayRow.createCell(0).setCellValue(user.getFirstName());
        gatewayRow.createCell(1).setCellValue(gateway.getLastName());
        gatewayRow.createCell(2).setCellValue(gateway.getNumber());
        gatewayRow.createCell(3).setCellValue(gateway.getAge());

        }

}
}

你可以用你的(Studen,aBook ....)替换我的用户类,这样就可以了!


0
投票
without using a view

要不使用视图,您必须将请求映射方法的返回类型设置为void

@Controller
public class MyController{

  @RequestMapping("/xyz")
  public void getExcel(HttpServletRequest request, HttpServletResponse response){
     // 1. Fetch your data
     // 2. Create your excel
     // 3. write excel file to your response.
  }

}

我相信你已经完成了第1部分。第2部分是完全不同的东西,你必须使用一些第三方api来做到这一点。 Apache POI非常简单有效。 https://poi.apache.org/spreadsheet/。他们的quickguide非常适合搭配。

创建文件后,现在需要将其写入响应,以便将其下载到客户端。这是你如何做到这一点。让我们说你创建的excel是xyz.xls

    response.setContentType("application/octet-stream");    // set content attributes for the response

    FileInputStream inputStream = new FileInputStream(new File("xyz.xls"));

    OutputStream outputStream = response.getOutputStream();             // get output stream of the response

    byte[] buffer = new byte[1024];
    int bytesRead = -1;
    while ((bytesRead = inputStream.read(buffer)) != -1) {  // write bytes read from the input stream into the output stream
        outputStream.write(buffer, 0, bytesRead);
    }

    outputStream.flush();

0
投票

this一样准备Excel表格(使用apache poi)。然后(例如)在您的控制器中,您可以轻松地将其写入正文:

@GetMapping(value = "/alluserreportExcel")
public ResponseEntity<InputStreamResource> excelCustomersReport() throws IOException {
    List<AppUser> users = (List<AppUser>) userService.findAllUsers();
    ByteArrayInputStream in = GenerateExcelReport.usersToExcel(users);
    // return IO ByteArray(in);
    HttpHeaders headers = new HttpHeaders();
    // set filename in header
    headers.add("Content-Disposition", "attachment; filename=users.xlsx");
    return ResponseEntity.ok().headers(headers).body(new InputStreamResource(in));
}

完整的例子是here

© www.soinside.com 2019 - 2024. All rights reserved.