title: Spring MVC and Apache POI to create xls doc. Can't save newly created file from model
body: <p>Good day people. I'm new in Spring as well as new here. </p>
  
  <p>I have problem. I have class to create xls document with Apache POI:</p>
  
  <pre><code>public class PagetoExcelConverter extends AbstractExcelView{
  
      List&lt;FormDate&gt; attributesList = null;   
  
      //Create massive of constants for making table header
      private final String[] HEADER_NAMES_MASSIVE = {"HEADER1", "HEADER2", "HEADER3"};
  
  
      @SuppressWarnings("unchecked")
      @Override
      protected void buildExcelDocument(Map&lt;String, Object&gt; model,
              HSSFWorkbook workbook, HttpServletRequest request,
              HttpServletResponse response) throws Exception {
  
                  //Creating new instance of ArrayList for add model attributes to
              attributesList = new ArrayList&lt;FormDate&gt;();
  
                  //Adding model attributes to ArrayList
                  attributesList.addAll((List&lt;FormDate&gt;)model.get("findAttributes"));
  
          //Creating sheet inside of book with given name 
          Sheet sheet = workbook.createSheet("Result");
              sheet.autoSizeColumn(0);
  
          //Making first row as a header 
          Row headerRow = sheet.createRow(0);
  
  
          for(int i=0; i&lt;HEADER_NAMES_MASSIVE.length; i++) {      
  
              Cell headCell = headerRow.createCell(i); 
              headCell.setCellValue(HEADER_NAMES_MASSIVE[i]);
              headCell.setCellStyle(headCellstyle);               
              }
  
  
              int rowNumber=1;
  
          for(int i=0; i&lt;attributesList.size(); i++) {
  
              Row dataRow = sheet.createRow(rowNumber);
              Cell dataCell;
  
              int cellNumber=0;
  
                  dataCell = dataRow.createCell(cellNumber);
                  dataCell.setCellValue(attributesList.get(i).getFormDescriptionList().get(i).getInstitutions().getNameOfInstitution());
  
                  cellNumber++;
  
                  dataCell = dataRow.createCell(cellNumber);
                  dataCell.setCellValue(attributesList.get(i).getFormDescriptionList().get(i).getInstitutionType().getTypeOfInstitution());
  
                  cellNumber++;
  
                  dataCell = dataRow.createCell(cellNumber);
                  dataCell.setCellValue(attributesList.get(i).getParticularDate().toString());
  
                  cellNumber++;
  
  
                  rowNumber++;
  
                  FileOutputStream fos = new FileOutputStream("/home/vadim/Desktop/mybook.xls");
                  workbook.write(fos);
  
          }
  
          attributesList = null;
  
      }                       
  }   
  </code></pre>
  
  <p>In my servlet-context I have:</p>
  
  <pre><code>&lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/classes directory. Goes first --&gt;
      &lt;beans:bean id="xlsviewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver"&gt;
      &lt;beans:property name="order" value="1" /&gt;
      &lt;beans:property name="basename" value="views"/&gt;
      &lt;/beans:bean&gt;
  </code></pre>
  
  <p>In my controller class I have method:</p>
  
  <pre><code>@RequestMapping(value="/result", method=RequestMethod.POST, params="asexcel")
      public String resultXLS(@RequestParam String particularDate,
                              @RequestParam String institutionName,
                              @RequestParam String institutionType, Model model) throws Exception {
  
          if((!particularDate.equals("")) &amp;&amp; !institutionName.equals("") &amp;&amp; institutionType.equals("")) { 
  
              model.addAttribute("findAttributes", educationWebService.fetchByDateAndNameService(dateConvertation(particularDate), institutionName));
  
          } else if((!particularDate.equals("")) &amp;&amp; (institutionName.equals("")) &amp;&amp; (!institutionType.equals(""))) {
  
              model.addAttribute("findAttributes", educationWebService.fetchByDateAndTypeService(dateConvertation(particularDate), institutionType));                 
  
          } else if((!particularDate.equals("")) &amp;&amp; institutionName.equals("") &amp;&amp; institutionType.equals("")) {   
  
              model.addAttribute("findAttributes", educationWebService.fetchByDateService(dateConvertation(particularDate))); 
  
          } else {        
              throw new Exception("Exception occurs because it's not correspond to any case in controller");
          }       
  
          return "xlspage";
      }
  </code></pre>
  
  <p>The problem is that it doesn't save newly created file fetched from model data. Instead of this it saves something completely different file looks like <code>TEXT/HTML</code> not xls. When I open this this file try to open browser and direct me on url. When I add to my <code>PagetoExcelConverter</code> class this:</p>
  
  <pre><code>FileOutputStream fos = new FileOutputStream("/home/vadim/Desktop/mybook.xls");
                  workbook.write(fos);
  </code></pre>
  
  <p>It saves all correctly, I mean it saves the file with <code>TXT/HTML</code> which I don't need and saves xls by where I point it to. I need a little window pop up for user from his browser to give a user chance to save in particular place. Could you help me please?</p>
  
  <p>Added call to <code>buildExelDocument():</code></p>
  
  <pre><code>#This view property triggered from org.springframework.web.servlet.view.ResourceBundleViewResolver for xls converting
  #Here is xlspage is name of the jsp page, is tied in with (class) with do converting model to xls
  xlspage.(class)=edu.demidov.service.PagetoExcelConverter
  </code></pre>