Adding different types of cells into excel using apache POI

This example shows you adding different types of cells into excel file using Apace POI.

There are different type of cells you can add into excel file like, Boolean, date, Blank, String etc.

Below is the example code which adds different type of cells and their values into excel file.

package com.java.connect.poi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class POIAddDifferentTypeCellsExample {
	public static void main(String[] args) throws IOException {
		// Create a workbook
		Workbook workbook = new HSSFWorkbook();
		// Create new sheet for workbook
		Sheet sheet = workbook.createSheet("Sheet one");
		// Create row at 3
		Row row = sheet.createRow(3);
		// Add d bouble value
		row.createCell(0).setCellValue(1346.787);
		// Add a date value
		row.createCell(1).setCellValue(new Date());
		// Add a calendar instance
		row.createCell(2).setCellValue(Calendar.getInstance());
		// Add some text
		row.createCell(3).setCellValue("Some Text!");
		// Add Blank cell
		row.createCell(4).setCellType(HSSFCell.CELL_TYPE_BLANK);
		// Add boolean type
		row.createCell(5).setCellType(HSSFCell.CELL_TYPE_BOOLEAN);
		// Add Error type
		row.createCell(6).setCellType(HSSFCell.CELL_TYPE_ERROR);
		// Add numeric type cell
		row.createCell(7).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
		// Add string type
		row.createCell(8).setCellType(HSSFCell.CELL_TYPE_STRING);

		// Write the output to a file
		FileOutputStream fileOut = new FileOutputStream(
				"POIAddDifferentTypeCellsExample.xls");
		workbook.write(fileOut);
		fileOut.close();
	}
}

The above example will create an excel file with different type of cells with their values.

Below is the screen shot of created excel doc: