Jexcel

Java Excel API Projects

Java Excel API Project 1

how to insert an hyperlink in an excel sheet using jexcel
Previous Home Next

 

The following example will show you how to insert a hyperlink in an excel worksheet using the JExcel Api. By clicking on the hyperlink contained in the sheet you can access the desired URL that is contained in the excel
worsheet as a hyperlink.

To create a hyperlink in the excel worksheet you have to import jxl.write.WritableHyperlink class. To create a new URL you also have to import java.net.URL class. There are several constructors avaiable for creating different types of hyperlinks viz. 
WritableHyperlink(Hyperlink hlk, WritableSheet wksht)
It is the constructor used internally by the worksheet whie making the copy of worksheet

WritableHyperlink(int col, int row, File file)
It is the constructor which creates a file hyperlink in a single cell.

WritableHyperlink(int col, int row, File file, String description)
This constructor also constructs a file hyperlink in a single cell but giving a descriptive name.

WritableHyperlink(int col, int row, int lstcol, int lstrow, File file)
It constructs a File hyperlink to a range of cells.

WritableHyperlink(int col, int row, int lstcol, int lstrow, File file, String description)
This constructor constructs a File hyperlink to a range of cells by giving a descriptive name to it.

WritableHyperlink(int col, int row, int lstcol, int lstrow, String description, WritableSheet wksht, int descol, int desrow, int lstdescol, int lstdesrow)
This constructor constructs a hyperlink to some cells within the current workbook

WritableHyperlink(int col, int row, int lsstcol, int lsstrow, URL url)
It constructs a url hyperlink to a range of cells

WritableHyperlink(int col, int row, int lstcol, int latrow, URL url, String description)
It constructs a url hyperlink to a range of cells with having description on it.

WritableHyperlink(int col, int row, String desc, WritableSheet wksht, int descol, int desrow)
This constructor constructs a hyperlink to some cells within the current workbook

WritableHyperlink(int col, int row, URL url)
It creates a URL hyperlink in a single cell
 
 We have used the constructor of the WritableHyperlink class to created the object of the WritableHyperlink class in the main method of the InsertHyperLinkInWorkbook class.

In this program we have created a class InsertHyperLinkInWorkbook, in which we have created various labels and also included a field called Hyperlinks. Then we called the constructor of theWritableHyperlink to include the hyperlink in the sheet of the workbook.

 

package r4r.co.in;

import java.io.IOException;
import java.io.File;
import jxl.Workbook;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Label;
import jxl.format.*;
import jxl.write.Formula;
import jxl.write.Number;
import jxl.write.WritableHyperlink;

public class InsertHyperLinkInWorkbook {

	/**
	 * @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);
        WritableFont wfobj=new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false,UnderlineStyle.SINGLE);
	    WritableCellFormat cfobj=new WritableCellFormat(wfobj);
	    cfobj.setBackground(Colour.PINK);
	    cfobj.setAlignment(Alignment.RIGHT);
	    cfobj.setWrap(true);
	    Label lbl=new Label(0,0,"Sum", cfobj);
	    sheet.addCell(lbl);
	    Number n1=new Number(0,1,10);
	    sheet.addCell(n1);
	    n1=new Number(0,2,15);
	    sheet.addCell(n1);
	    Formula f1=new Formula(0,3,"SUM(A2:A3)", cfobj);
	    sheet.addCell(f1);
	    Label lblhyperlink=new Label(1,0,"Hyperlinks");   //created a label in sheet that will contain all the hyper links
	    sheet.addCell(lblhyperlink);
	    WritableHyperlink hlk =new WritableHyperlink(1,1, new File("F:/JAVA PROJECTS/Jexcel"),"r4r projects");          //creating an instance of WritableHyperlink
	   sheet.addHyperlink(hlk);                                                                   // adding hyperlink to the sheet
	   Formula f=new Formula(1,2,"HYPERLINK(\"http://www.r4r.co.in\","+"\"goto r4r\")");         // creating formula for moving on to web throught hyperlinks using Formula class
	   sheet.addCell(f);
	    
	  
	    
	   
	    
	   
	    workbook.write();
	    workbook.close();
	}
	catch(IOException e)
	{
		e.printStackTrace();
	}
	catch(WriteException e)
	{
		e.printStackTrace();
	}
	}

}



Previous Home Next