title: Excel file get corrupted while downloading from server using java
body: <p>I have written a code for downloading an excel file. File content derived from database. Code working on local machine but when deployed on server. Excel file getting currupted while downloading. I am unable to find the problem in my code. Please help me to get rid of this problem. First method to export the excel and second method for writing contents in excelsheet. And second methos is called by first method.</p>
  
  <pre><code>public static void doGetExportException(String sheetId, ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws ServletException, IOException 
  {
  
  
      HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);
      try 
      {
          long maxSheetId = Long.parseLong(sheetId);
          System.out.println("maxSheetId="+maxSheetId);
          List&lt;Customer_Exception&gt; lstCustomer_Exceptions =UploadExcelUtil.getCustomerExceptions(maxSheetId);
  
          XSSFWorkbook outPutSheet= writeExcelsheet(lstCustomer_Exceptions);  
  
          ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
          outPutSheet.write(outByteStream);
          byte [] outArray = outByteStream.toByteArray();
  
          String fileName = "Download_"+new Date(new java.util.Date().getTime())+".xlsx";
          response.setContentType("application/ms-excel; charset=UTF-8"); 
          response.setContentLength(outArray.length);
          response.setHeader("Expires:", "0"); 
          response.addHeader(HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");
          response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+fileName);
          response.addHeader(HttpHeaders.CONTENT_ENCODING, "Binary");
          OutputStream outStream = response.getOutputStream();
          outStream.write(outArray);
          outStream.flush();
          outStream.close();   
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  private static XSSFWorkbook writeExcelsheet(List&lt;Customer_Exception&gt; lstCustomer_Exceptions) {
      XSSFWorkbook workbook = new XSSFWorkbook();
      XSSFSheet sheet = workbook.createSheet("Exceptions");
  
      CellStyle styleHeader = workbook.createCellStyle();
      XSSFFont fontHeader = workbook.createFont();
      XSSFColor color = new XSSFColor(Color.BLACK);
      fontHeader.setColor(color);
      fontHeader.setBold(true);
      fontHeader.setFontHeight(13);
      styleHeader.setFont(fontHeader);
      sheet.createFreezePane(0, 1);
      CellStyle styleData = workbook.createCellStyle();
      XSSFFont fontData = workbook.createFont();
      fontData.setColor(color);
      fontData.setFontHeight(12);
      styleData.setFont(fontData);
  
      int rownum = 0;
      XSSFRow rowHeader = sheet.createRow(rownum++);
      List&lt;String&gt; lstCellHeaders =getCellHeaders();
      int cellnum = 0;
      for(String cellHeader:lstCellHeaders){
          XSSFCell cell = rowHeader.createCell(cellnum++);
          cell.setCellStyle(styleHeader);
          cell.setCellValue((String)cellHeader);
  
      }  
  
  
  
      for (Customer_Exception customer_Exception:lstCustomer_Exceptions) {
          List&lt;String&gt; lstCellValues =getCellValues(customer_Exception);
          XSSFRow rowData = sheet.createRow(rownum++);
  
          cellnum = 0;
          for(String cellValue:lstCellValues){
              XSSFCell cell = rowData.createCell(cellnum++);
              cell.setCellStyle(styleData);
              cell.setCellValue((String)cellValue);
  
          }  
      }
      return workbook;
  }
  </code></pre>