title: Got error when write to excel in java
body: <p>I can't figure it out what's the problem. I tried but errors are still exist.</p>
  
  <p>my program is to export to excel and i'm using apache poi api.</p>
  
  <p>Below is my code. EDIT:</p>
  
  <pre><code>public void exportToExcel(ValueObjectList columnBody, String pageDef, ValueObject vo){
      try {
      HttpServletResponse response = vo.getResponse();
  
       SimpleDateFormat sd = new SimpleDateFormat("ddMMyy");
       Date dt = new Date();
      response.setContentType("application/vnd.ms-excel");
      if(pageDef == "promo" || pageDef.equals("promo"))
          response.setHeader("Content-Disposition", "attachment; filename=PROMO-" + sd.format(dt) + ".xls");
      else if(pageDef == "incomplete" || pageDef.equals("incomplete"))
          response.setHeader("Content-Disposition", "attachment; filename=INCOM-" + sd.format(dt) + ".xls");
      else
           response.setHeader("Content-Disposition", "attachment; filename=ST-" + sd.format(dt) + ".xls");
  
  
      // create a small spreadsheet
       HSSFWorkbook wb = new HSSFWorkbook();
       HSSFSheet sheet = wb.createSheet();
  
       HSSFRow row = null;
       HSSFCell cell = null;
  
       //set default font properties
       //font family: Arial
       //font weight: bold
       Font headerFont = wb.createFont();
       headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
  
       headerFont.setFontHeightInPoints((short)14);
  
       //Cell Style for header
       CellStyle csHeader = wb.createCellStyle();
       csHeader.setFont(headerFont);
       csHeader.setBorderBottom(csHeader.BORDER_THICK);
       csHeader.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
       csHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
       csHeader.setWrapText(true);
  
       //Cell Style for body
       CellStyle csBody = wb.createCellStyle();
       csBody.setWrapText(true);
       csBody.setAlignment(HSSFCellStyle.ALIGN_CENTER);
  
  
       String[] columnHeader = ((ValueObject)columnBody.get(0)).toKeyArray();
       //System.out.println("Header Length: " + columnHeader.length);
  
       row = sheet.createRow(0);
       cell = row.createCell(0);
       cell.setCellValue("No");
       cell.setCellStyle(csHeader);
  
       for(int h = 0; h &lt; columnHeader.length; h++){
           cell = row.createCell(h+1);
           cell.setCellValue(columnHeader[h]);
           cell.setCellStyle(csHeader);
           sheet.autoSizeColumn(h+1);
           //sheet.setColumnWidth(h, 2000);
       }
  
       //System.out.println("header key : " + columnHeader[2]);
      //System.out.println("header value : " + testobj.get(testobj.toKeyArray()[2]));
  
       for(int i = 0; i &lt; columnBody.size(); i++){
           row = sheet.createRow(i+1);
           cell = row.createCell(0);
           cell.setCellValue(i+1);
           cell.setCellStyle(csBody);
           ValueObject column = (ValueObject)columnBody.get(i);
           for(int j = 0; j &lt; column.size(); j++){
               cell = row.createCell(j+1);
               cell.setCellValue(column.get(column.toKeyArray()[j]));
               cell.setCellStyle(csBody);
               sheet.autoSizeColumn(j+1);
           }
       }
  
  
       /*
       // write it as an excel attachment
       ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
       wb.write(outByteStream);
       byte [] outArray = outByteStream.toByteArray();
       response.setContentType("application/ms-excel");
       response.setContentLength(outArray.length);
       response.setHeader("Expires:", "0"); // eliminates browser caching
       if(pageDef == "promo" || pageDef.equals("promo"))
          response.setHeader("Content-Disposition", "attachment; filename=PROMO-" + sd.format(dt) + ".xls");
       else if(pageDef == "incomplete" || pageDef.equals("incomplete"))
          response.setHeader("Content-Disposition", "attachment; filename=INCOM-" + sd.format(dt) + ".xls");
       else
           response.setHeader("Content-Disposition", "attachment; filename=ST-" + sd.format(dt) + ".xls");
  
       OutputStream outStream = response.getOutputStream();
       outStream.write(outArray);
       outStream.flush();
       */
  
  
       ByteArrayOutputStream outByteStream = new ByteArrayOutputStream(wb.getBytes().length);
       wb.write(outByteStream);
  
  
      } catch (Exception e) {
          log.error(e);
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>I tried to surf in google and tried to solve but not ok.</p>
  
  <p>At first, i made these codes in jsp.</p>
  
  <p>when i surf google, people said i've to use in servlet so I moved to servlet but still got errors.</p>
  
  <p>english is not my mother-tongue. sorry if i typed wrong.</p>
  
  <p>Thanks in advance.</p>