Read and write Excel file using Apache POI

/**
 * Example showing how to read and write Excel file(i.e *.xls or *.xlsx).
 * JAR files needed:
 *  poi-3.6-20091214.jar
 *  poi-ooxml-3.6-20091214.jar
 * If you only need to handle Excel 2007 OOXML (.xlsx) file format, then you can use XSSF* classes.
 * If you only need to handle Excel '97(-2007) file format, then you can use HSSF* classes.
 * @author Xuan Ngo
 */
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory; // This is included in poi-ooxml-3.6-20091214.jar
import org.apache.poi.ss.usermodel.Workbook;
 
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 
public class PoiExample
{
 
  public static void main(String[] args)
  {
    try
    {
      // Read from original Excel file.
      Workbook workbook = WorkbookFactory.create(new FileInputStream("my_original_excel.xls") );
 
      // Get the first sheet.
      Sheet sheet = workbook.getSheetAt(0);
 
      // Set value of the first cell.
      Row row = sheet.getRow(0);
      Cell cell = row.getCell(0);
      cell.setCellValue("Xuan");
 
      // Write newly modified workbook to a file.
      FileOutputStream fileOut = new FileOutputStream("new_workbook.xls");
      workbook.write(fileOut);
      fileOut.close();
    }
    catch(FileNotFoundException e)
    {
      System.out.println(e);
    }
    catch(IOException e)
    {
      System.out.println(e);
    }
    catch(InvalidFormatException e)
    {
      System.out.println(e);
    }
 
  }
}