Good afternoon, tell me, please, how to get to save the excel file generated on the server to the client's computer?

HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("FirstSheet"); List<Material>materials = materialDAO.getMaterialsList(); HSSFRow rowhead = sheet.createRow((short)0); rowhead.createCell(0).setCellValue("№ з/п"); rowhead.createCell(1).setCellValue("Назва"); rowhead.createCell(2).setCellValue("Кількість"); rowhead.createCell(3).setCellValue("Од.вим."); for(int i=1;i<materials.size();i++){ HSSFRow row = sheet.createRow((short)i); row.createCell(0).setCellValue(i); row.createCell(1).setCellValue(materials.get(i-1).getTitle()); row.createCell(2).setCellValue(materials.get(i-1).getNumber()); row.createCell(3).setCellValue(materials.get(i-1).getUnity()); } FileOutputStream fileOut = new FileOutputStream(filename); workbook.write(fileOut); fileOut.close(); System.out.println("Your excel file has been generated!"); 
  • What libraries do you use for your project to create an excel file? Well, give the code itself as sent to the client. - MrFylypenko
  • @MrFylypenko This code (above) to save to disk. But, as far as I understand, this file will be saved on the server. And how to transfer it to save on the client's computer? - Nikolay Egorov

2 answers 2

Something like this:

 @RequestMapping(value = "/myExcel", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public void myExcel(HttpServletResponse response) throws IOException, ParseException { HSSFWorkbook workbook = new HSSFWorkbook(); //тут дальше создаем файл response.setHeader("Content-Disposition", "inline;filename=\"" + URLEncoder.encode("Мой файл.xls", "UTF-8") + "\""); response.setContentType("application/xls"); OutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } 
  • one
    The more correct Content-Type for Excel format up to version 2007 is application/vnd.ms-excel . - Nofate

Spring versions 4.2 and higher for returning to the Clement XLS (X) offers to extend the abstract classes AbstractXlsView and AbstractXlsxView .

 public class MyXlsView extends AbstractXlsView { private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT); @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=\"report.xls\""); Sheet sheet = workbook.createSheet("FirstSheet"); // собираем файл // ... Row header = sheet.createRow(0); rowhead.createCell(0).setCellValue("№ з/п"); rowhead.createCell(1).setCellValue("Назва"); rowhead.createCell(2).setCellValue("Кількість"); rowhead.createCell(3).setCellValue("Од.вим."); // итд } 

and return them via ModelAndView from the controller.

 @RequestMapping(value = "/report", method = RequestMethod.GET) public ModelAndView getReport() { ModelAndView mav = new ModelAndView(new MyXlsView()); return mav; } 

To give the file name with Cyrillic cross-browser is a separate problem.
In my memory, it worked best like this:

 response.setHeader("Content-Disposition", "attachment; filename*=utf8'ru-ru'" + URLEncoder.encode(filename, "UTF-8")); 
  • and in RestControllere how to do it (without a model). Just throw an array of bytes? - Nikolay Egorov
  • @RestController similar to the @Controller and @ResponseBody annotation pair. - Nofate
  • And if you just send an array of bytes. What to do with it then in Javascript? - Nikolay Egorov
  • no need to handle it with javascript. Just let the user follow the link and get an array of bytes. With the right Content-Type and Content-Disposition browser will automatically start downloading the file. - Nofate
  • understood, thanks ... - Nikolay Egorov