title: Convert .xls to .xlsx format using java code
body: <p>I Need to convert xml spread sheet 2003 to .xlsx format.</p>
  
  <p>in my application one link is there, if i click that link it will download the file in download folder. when i try to open that excel file it gives a message</p>
  
  <p>"The file format and extension of don't match. the file could be corrupted or unsafe. unless you trust its source, don't open it. Do you want open it anyway?"</p>
  
  <p>and it gives the option Yes, No , and Help.</p>
  
  <p>If i click Yes, then i can see the excel sheet content.
  The excel sheet extension is .xls but when i go to File in Excel sheet and click on 'save as' it shows the save as type is XML spread sheet 2003.</p>
  
  <p>I can do it manually save as .xlsx but every time i can't do it. Even i tried with below code</p>
  
  <pre><code>public class xls2xlsx {
  
   public static void main(String[] args) throws InvalidFormatException,
          IOException {
  
      String inpFn = "F:\\Users\\Downloads\\Report.xls"; 
      String outFn = "F:\\Users\\Downloads\\Report.xlsx"; 
  
      InputStream in = new BufferedInputStream(new FileInputStream(inpFn));
      try {
          Workbook wbIn = new HSSFWorkbook(in);
          File outF = new File(outFn);
          if (outF.exists())
              outF.delete();
  
          Workbook wbOut = new XSSFWorkbook();
          int sheetCnt = wbIn.getNumberOfSheets();
          for (int i = 0; i &lt; sheetCnt; i++) {
              Sheet sIn = wbIn.getSheetAt(0);
              Sheet sOut = wbOut.createSheet(sIn.getSheetName());
              Iterator&lt;Row&gt; rowIt = sIn.rowIterator();
              while (rowIt.hasNext()) {
                  Row rowIn = rowIt.next();
                  Row rowOut = sOut.createRow(rowIn.getRowNum());
  
                  Iterator&lt;Cell&gt; cellIt = rowIn.cellIterator();
                  while (cellIt.hasNext()) {
                      Cell cellIn = cellIt.next();
                      Cell cellOut = rowOut.createCell(
                              cellIn.getColumnIndex(), cellIn.getCellType());
  
                      switch (cellIn.getCellType()) {
                      case Cell.CELL_TYPE_BLANK:
                          break;
  
                      case Cell.CELL_TYPE_BOOLEAN:
                          cellOut.setCellValue(cellIn.getBooleanCellValue());
                          break;
  
                      case Cell.CELL_TYPE_ERROR:
                          cellOut.setCellValue(cellIn.getErrorCellValue());
                          break;
  
                      case Cell.CELL_TYPE_FORMULA:
                          cellOut.setCellFormula(cellIn.getCellFormula());
                          break;
  
                      case Cell.CELL_TYPE_NUMERIC:
                          cellOut.setCellValue(cellIn.getNumericCellValue());
                          break;
  
                      case Cell.CELL_TYPE_STRING:
                          cellOut.setCellValue(cellIn.getStringCellValue());
                          break;
                      }
  
                      {
                          CellStyle styleIn = cellIn.getCellStyle();
                          CellStyle styleOut = cellOut.getCellStyle();
                          styleOut.setDataFormat(styleIn.getDataFormat());
                      }
                      cellOut.setCellComment(cellIn.getCellComment());
  
                      // HSSFCellStyle cannot be cast to XSSFCellStyle
                      // cellOut.setCellStyle(cellIn.getCellStyle());
                  }
              }
          }
          OutputStream out = new BufferedOutputStream(new FileOutputStream(
                  outF));
          try {
              wbOut.write(out);
          } finally {
              out.close();
          }
      } finally {
          in.close();
      }
  }
  }
  </code></pre>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Exception in thread "main" java.io.IOException: Invalid header signature; read 0x6576206C6D783F3C, expected 0xE11AB1A1E011CFD0
      at org.apache.poi.poifs.storage.HeaderBlock.&lt;init&gt;(HeaderBlock.java:140)
      at org.apache.poi.poifs.storage.HeaderBlock.&lt;init&gt;(HeaderBlock.java:104)
      at org.apache.poi.poifs.filesystem.POIFSFileSystem.&lt;init&gt;(POIFSFileSystem.java:138)
      at org.apache.poi.hssf.usermodel.HSSFWorkbook.&lt;init&gt;(HSSFWorkbook.java:322)
      at org.apache.poi.hssf.usermodel.HSSFWorkbook.&lt;init&gt;(HSSFWorkbook.java:303)
      at excelSheet.xls2xlsx.main(xls2xlsx.java:42)
  </code></pre>