First Excel Spreadsheets Using JExcel API

First Excel Spreadsheets Using JExcel API

Previous Home Next

 

In this we are going to create first example of JExcel API.In this we are going to create a work space and excel sheet . 

JExcel API provides jxl.write.* package to create an excel spread sheet. This package have many number of classes by using this we can make excel spread sheet, give sheet name, add labels etc.
 

If you are new then read first http://r4r.co.in/java/apis/jexcel/basic/tutorial  It covers basics of Jexcel API Step by Step. To create excel spread sheet we have create an object of WritableWorkbook .To create it Jexcel API provides a static method createWorkbook() of WorkBook class .In this createWorkbook() method we have pass file name with .xls extension.After that we have create a sheet .For this WritableWorkbook class has a method createSheet(String sheetname,int sheetnumner) which return type is WritableSheet. We store this object into a variable of WritableSheet .To add some value in cell we can use addCell(Label label) method provided in WritableSheet class. Label class is used to create label and add on cell.Finally we have write() method of WritableWorkbook class to write on workbook and at last close the workbook.

 

package r4r.co.in.java.jexcel;

import java.io.File;
import java.io.IOException;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class FirstR4RJExcelExample {

	public static void main(String[] args) {
		WritableWorkbook workbook;
		try {
			workbook = Workbook.createWorkbook(new File("myfile.xls"));
			WritableSheet sheet = workbook.createSheet("First Sheet", 0);
			Label label = new Label(0, 2, "R4R JExel API Example");
			sheet.addCell(label);
			workbook.write();
			workbook.close();

		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}


Previous Home Next