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.
Excel version : Microsoft Office Excel 2007 and Microsoft Office Excel 2003 (tried both)
Apache POI Version: 3.6
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");
}
}
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.
Could you please help?