title: Writing to an excel using Apache POI corrupts the excel file
body: <p>I am trying to write to excel using Apache POI. The code (below) executes fine, but when I am trying to open the excel, it is showing that the data inside the excel has been corrupted and it cannot be opened. </p>
  
  <p>Excel version : Microsoft Office Excel 2007 and Microsoft Office Excel 2003 (tried both)</p>
  
  <p>Apache POI Version: 3.6</p>
  
  <pre><code>import java.io.FileOutputStream;
  import java.io.IOException;
  
  import org.apache.poi.xssf.usermodel.XSSFCell;
  import org.apache.poi.xssf.usermodel.XSSFRow;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class WriteExcel{
  
      public String FilePath;
      XSSFWorkbook wb= null;
      XSSFSheet ws= null;
      XSSFRow xr=null;
      XSSFCell xc=null;
      FileOutputStream fout = null;
  
      public WriteExcel(String FilePath) throws IOException
  
      {
          this.FilePath=FilePath;
          fout=new FileOutputStream(FilePath);
          wb=new XSSFWorkbook();
          wb.write(fout);
  
      }
  
      //Write to a specific Cell
  
      public void writeToCell(String SheetName, int RowNum, int ColNum, String Data) throws IOException
      {
          ws=wb.createSheet(SheetName);
          xr=ws.createRow(RowNum);
          xc=xr.createCell(ColNum);
          xc.setCellValue(Data);
          fout.close();
      }
  
  
      public static void main(String[] args) throws IOException {
  
          WriteExcel WE= new WriteExcel("E://Learning//Learning//SoapUI//TestFiles//Write.xls");
          WE.writeToCell("Sheet1", 2, 2, "Pi");
          System.out.println("Written");
  
      }
  
  
  } 
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/KFw6s.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KFw6s.png" alt="Image1"></a>; <a href="https://i.stack.imgur.com/pHUIk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pHUIk.png" alt="Image2"></a>; <a href="https://i.stack.imgur.com/OnJfp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OnJfp.png" alt="Image3"></a></p>
  
  <p>I believe the  the code is fine because every time I am executing the code, the "Date Modified" shows the latest time stamp; so I suspect there might be something with versioning of either Microsoft Office (Excel) or Apache POI. </p>
  
  <p>Could you please help?</p>