How to Add Hyperlinks to Link Data in Excel Worksheet inside Android Apps
This technical tip show how developers can add hyperlinks to link data inside their Android applications using Aspose.Cell for Android. A hyperlink links two entities. Everybody is familiar with hyperlinks because they are used extensively on the Internet. Using Aspose.Cells, developers can create different kinds of hyperlinks in Excel files. This article discusses what types of hyperlinks are supported in Aspose.Cells and how to use them. There are three kinds of hyperlinks which can be added into a cell using Aspose.Cells, a link to a URL, a link to another cell in the same file and a link to an external file.
//A link to a URL
File sdDir = Environment.getExternalStorageDirectory();
String sdPath = sdDir.getCanonicalPath();
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
//Adding a hyperlink to a URL at "A1" cell
hyperlinks.add("A1",1,1,"http://www.aspose.com");
//Saving the Excel file
workbook.save(sdPath + "/book1.xls");
//In the above example, a URL is added to an empty cell, A1, as a hyperlink. When the cell is empty, the URL also becomes the link text. If a URL is added as a link to a cell that already contains text, then the hyperlink is added but the value of the cell looks like plain text. To make it look like a hyperlink, apply formatting on the cell.
File sdDir = Environment.getExternalStorageDirectory();
String sdPath = sdDir.getCanonicalPath();
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
//Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
//Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
//Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
//Adding a hyperlink to a URL at "A1" cell
hyperlinks.add("A1",1,1,"http://www.aspose.com");
//Saving the Excel file
workbook.save(sdPath + "/book1.xls");
//Adding a Link to another Cell in the Same File
File sdDir = Environment.getExternalStorageDirectory();
String sdPath = sdDir.getCanonicalPath();
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
//Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
//Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
//Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
//Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in
//the same Excel file
hyperlinks.add("B3",1 ,1, "Sheet2!B9");
//Saving the Excel file
workbook.save(sdPath + "/book1.xls");
//Adding a Link to an External File
File sdDir = Environment.getExternalStorageDirectory();
String sdPath = sdDir.getCanonicalPath();
//Instantiating a Workbook object
Workbook workbook = new Workbook();
//Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet sheet = worksheets.get(0);
//Setting a value to the "A1" cell
Cells cells = sheet.getCells();
Cell cell = cells.get("A1");
cell.setValue("Visit Aspose");
//Setting the font color of the cell to Blue
Style style = cell.getStyle();
style.getFont().setColor(Color.getBlue());
//Setting the font of the cell to Single Underline
style.getFont().setUnderline(FontUnderlineType.SINGLE);
cell.setStyle(style);
HyperlinkCollection hyperlinks = sheet.getHyperlinks();
//Adding a link to the external file
hyperlinks.add("A5", 1, 1, "C:\\book1.xls");
//Saving the Excel file
workbook.save(sdPath + "/book2.xls");
Url: http://www.aspose.com/java/excel-component.aspx
Language: Java | User: Sheraz Khan | Created: Sep 24, 2014 | Tags: Add Hyperlinks to Link Data Create hyperlinks in Excel files add link to a URL add link to another cell in same excel file add link to an external file add hyperlinks to Excel files Android Excel API