how to create a workbook using jexcel

how to create a workbook using jexcel

Previous Home Next

 

Here we will know how to create a workbook using the jexcel Api in java. The jexcel Api allow us to create, modify the excel sheets according to our requirements.

For creating a workbook we have to import the jxl package. For creating a workbook we hava use the class ,
jxl.write.WritableWorkbook;
Its syntax is,
public abstract class WritableWorkbook extends java.lang.Object
This class contains various method and fields which helps us in creating  workbook using the jexcel Api.
 The jxl.write.WritableWorkbook class contains various methods, some of them are as follows,
abstract  void  close()
This method closes the current workbook that's been created, and makes the memory, which has been allocated,  available for the garbage collection.
 
abstract  void  copySheet(int i, String name, int index)
This method copies a sheet within the same workbook. It takes three parameters viz,
int i -> it defines the index of the sheet that is to be copied.
String name -> It is the name of the new sheet created.
int index -> It is the index  position at which new sheet is created.

abstract  void  copySheet(String s, String name, int index)
This method copies the sheet within the same workbook. It takes three parameters viz,
String s-> It gives the name of the sheet to be copied.
String name-> It is the name of the new sheet created.
int index-> It is the index position at which new sheet is created.

abstract  WritableSheet createSheet(String name, int index)
This method creates a Sheet and returns a worksheet at the specified index with the specified name If the index number of the sheet specified is less than or equal to zero, then the  new sheet is created at the beginning of the workbook.

abstract  WritableCell  findCellByName(String name)
It returns the cell specified by the particular name.

abstract  int getNumberOfSheets()
It returns the  number of sheets that are contained by the workbook.

abstract  WritableSheet getSheet(int index)
It gets the specified sheet pointed by the index in the workbook.

abstract  WritableSheet  getSheet(String name)
It gets the specified sheet by name within the workbook.

abstract  String[]  getSheetNames()
It returns an String array of  names of all the sheets conatined in within the workbook.

abstract WritableSheet[]   getSheets()
It returns the array of all the sheets contained in the workbook.

abstract  WritableCell   getWritableCell(String location)
It returns the cell for the specifieced location

abstract  WritableSheet  importSheet(String name, int index, Sheet s)
It imports a sheet from  the some another workbook and does a deep copy on all elements within that sheet. It takes three parameters viz,
String name-> name of the new sheet.
int index-> index of the new sheet
Sheet s-> sheet to be imported.

abstract  WritableSheet  moveSheet(int fromIndex, int toIndex)
This method is used for moving the specified sheet from  one index position to another index position within the same workbook.

abstract  void removeSheet(int index)
This method removes the sheet at the specified index from  workbook

abstract  void   setColourRGB(Colour c, int r, int g, int b)
This method sets the RGB values for the specified colour for  workbook

abstract  void  write()
This method writes all the data to the workbook in the excel format.

In the source code given below, we have created a class Workbook which contains the method how to create an   excel worksheet. For creating a workbook we have to pass an object of File which contains  the location of the directory where you want to store your workbook.
After creating a workbook we called the write() method, for writting the data to the workbook in the excel format. After that we have called the close() method to close the current instance of the workbook. Here you should note that writting to a workbook causes the WriteException, so it should be handled properly, by using try-catch blocks.

 

package r4r.co.in;

import java.io.IOException;
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CreateWorkbook {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException, WriteException{
		// TODO Auto-generated method stub

	try
	{
		WritableWorkbook workbook=Workbook.createWorkbook(new File("F:/JAVA PROJECTS/Jexcel/src/r4r/co/in/firstsheet.xls"));
	    workbook.createSheet("mysheet", 0);
	    workbook.write();
	    workbook.close();
	}
	catch(IOException e)
	{
		e.printStackTrace();
	}
	catch(WriteException e)
	{
		e.printStackTrace();
	}
	}

}


Previous Home Next