Add cells values in excel using apache POI

This example shows you how to add the cells into excel file and put some string values inside those cells using Apache POI framework.

Below is the example code, which is creating the workbook, and creating three cells and putting some string values inside cells.



import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class POIAddNewCellsExample {

	public static void main(String[] args) throws IOException {
		// Create a workbook object.
		Workbook workbook = new HSSFWorkbook();
		// Create a sheet using createSheet method of workbook
		Sheet sheet = workbook.createSheet("Sheet one");
		// Create a row and put some cells in it.
		Row row = sheet.createRow(0);
		// Create a cell and put a value in it.
		Cell cell = row.createCell(0);
		cell.setCellValue("Value 1");
		// Or You can put in one line like this.
		row.createCell(1).setCellValue("Value 2");
		row.createCell(2).setCellValue("Value 3");
		// Create a FileOutputStream by passing the excel file name.
		FileOutputStream outputStream = new FileOutputStream("POIAddNewCellsExample.xls");
		// Write the FileOutputStream to workbook object.
		// Finally close the FileOutputStream.

When you run the above example, it will create an excel file with three cells, the cells value will be as ‘value 1′, ‘value 2′ and ‘value 3′.

Below is the screen shot of created excel file: