2017-02-10 42 views
0
List<String> headerRow = Arrays.asList("col1", "col2", "col3","col4","col5"); 
    // Creates a xlsx workbook !! 
    XSSFWorkbook wb = ExcelHelper.writeToXlsxExcelFile(headerRow,EjbHelper.getAccountEJb().getCustomersByscPartyNumber("9B7W5")); 
    //Creating a file to dump xlsx 
    String fileLocation = "d://try.xlsx"; 
    FileOutputStream fout = new FileOutputStream(fileLocation); 
    workbook.write(fout); 
    //Want to Create a Webservice to send response without saving file to hard-disk 
    ResponseBuilder response = Response.ok((Object)fout); 
    response.header("Content-Disposition","attachment; filename=" + "ResourceInformation.xlsx"); 
    return response.build(); 

上面的代碼片段創建一個xlsx文件,然後通過響應發送它。我想知道有沒有任何方法直接發送xlsx文件,而無需在硬盤上創建文件?如何發送新創建的Excel文件作爲響應而不將其保存到硬盤上?

回答

0

我想更簡單的方法是直接使用HttpServletResponse

public void yourMethod(@Context HttpServletResponse httpServletResponse) { 
    List<String> headerRow = Arrays.asList("col1", "col2", "col3","col4","col5"); 
    Workbook wb = ExcelHelper.writeToXlsxExcelFile(headerRow,EjbHelper.getAccountEJb().getCustomersByscPartyNumber("9B7W5")); 
    response.setHeader("Content-Disposition","attachment; filename=" + "ResourceInformation.xlsx"); 
    wb.write(response.getOutputStream()); 
    response.flushBuffer(); 
} 
相關問題