Java code exporting workbook to current user PC instead of the actual web app host

Tags: , , , ,



I have a spring MVC project that I have develop for my team at work. I have an endpoint that create a workbook using apache poi, then export to .xlsx file, but my code appeared to write to file on the application host instead of user’s computer. I know I’m missing something, but I try what I found on internet without any luck. An help would be appreciated.

Report endpoint

@RequestMapping(value = "/report", method = RequestMethod.GET)
    String report(HttpServletRequest rq, Model model) throws FileNotFoundException, IOException {
 
       
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Product");
        sheet.setColumnWidth(0, 6000);
        sheet.setColumnWidth(1, 4000);
        
        Row header = sheet.createRow(0);
      
        XSSFFont font =  ((XSSFWorkbook) workbook).createFont();
        font.setFontName("Calibri");
        font.setFontHeight(16);
       
        
        CellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
      //  headerStyle.setFont(font);
       
        
        Cell headerCell = header.createCell(0);
        headerCell.setCellValue("Product Name");
        headerCell.setCellStyle(headerStyle);
        
        headerCell = header.createCell(1);
        headerCell.setCellValue("Manufacturer");
        headerCell.setCellStyle(headerStyle);
        
        headerCell = header.createCell(2);
        headerCell.setCellValue("Model No.");
        headerCell.setCellStyle(headerStyle);
        
        headerCell = header.createCell(3);
        headerCell.setCellValue("Part No..");
        headerCell.setCellStyle(headerStyle);
        
        headerCell = header.createCell(4);
        headerCell.setCellValue("Qauntity");
        headerCell.setCellStyle(headerStyle);
        
        headerCell = header.createCell(5);
        headerCell.setCellValue("Location");
        headerCell.setCellStyle(headerStyle);
    
        
        CellStyle rowStyle = workbook.createCellStyle();
        rowStyle.setWrapText(true);
        
        List<Items> allItems = IT.getAllItem();
        
int rowsCount = 1;
        for(Items eachItem : allItems){
       
        Row row = sheet.createRow(rowsCount++);
        
        Cell cell = row.createCell(0);
        cell.setCellValue(eachItem.getItemName());
        cell.setCellStyle(rowStyle);
        
        cell = row.createCell(1);
        cell.setCellValue(eachItem.getManufacturer());
        cell.setCellStyle(rowStyle);
        
        cell = row.createCell(2);
        cell.setCellValue(eachItem.getModelNo());
        cell.setCellStyle(rowStyle);
        
        cell = row.createCell(3);
        cell.setCellValue(eachItem.getPartNo());
        cell.setCellStyle(rowStyle);
        
        cell = row.createCell(4);
        cell.setCellValue(eachItem.getQuantity());
        cell.setCellStyle(rowStyle);
        
        for(Locations locations : eachItem.getLocations()){
        cell = row.createCell(5);
        cell.setCellValue(locations.getLocationName());
        cell.setCellStyle(rowStyle);
        }
        
        
        }
        
  String getFilePath = "C://reports//";
        Path path = Paths.get(getFilePath);
         if(!Files.exists(path)) { 
            Files.createDirectories(path);
         }else{
             System.out.print("file exist");
         }
         
     try (FileOutputStream outputStream = new FileOutputStream(getFilePath + "Invenotry_Report" + date.format(formatter) + ".xlsx")) {
            workbook.write(outputStream);
            workbook.close();
            outputStream.flush();
            outputStream.close();
            
}   catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

       return "redirect:home";
   } 
       
        
}

Answer

Found the answer by using Servlet response.

 @RequestMapping(value = "/report", method = RequestMethod.GET)
    String report(HttpServletRequest rq, Model model, HttpServletResponse response) throws FileNotFoundException, IOException {
              
         \excel Workbook code here


             
        response.setContentType("xlsx");
        response.setHeader("Content-disposition", "attachment; filename=Invenotry_Report.xlsx");
        try (OutputStream outputStream = response.getOutputStream()) {
            workbook.write(outputStream);
            workbook.close();
            outputStream.flush();
            outputStream.close();
        return "redirect:home";
 
   }

}


Source: stackoverflow