how to create a sheet in jexcel
how to create a sheet in jexcel
In this example, we will see how to create a excel worksheet using JExcel Api. The worksheet is contained in a workbook which is created, and all the data is contained in this sheet itself.
In this example, for creating a worksheet we have import the jxl.write.WritableSheet class. Also we have imported jxl.write.WritableWorkboook to create a workbook in which this sheet is going to reside.
In the example we have used the createSheet(String str, int arg) method contained in jxl.write.WritableWorkbook class to create a sheet in the workbook. Furthermore, we have called the getName() method on the sheet thus created, to retrieve its name.
The following code shows how can we create a worksheet using jexcel. We have created a Workbook name workbook and then we have created a sheet named mysheet at the 0th index in this workbook. We have included all the code in the try-catch block so as to handle various exception that are caused during the writting in the workbook.
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
public class CreateSheet {
/**
* @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"));
WritableSheet sheet=workbook.createSheet("mysheet", 0);
System.out.println("The sheet created is named as :"+sheet.getName());
workbook.write();
workbook.close();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(WriteException e)
{
e.printStackTrace();
}
}
}