This technical tip show how developers can add hyperlinks in Excel File 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.
·
A link to an external file.
Aspose.Cells makes it possible to add hyperlinks to Excel
files either using the API or designer spreadsheet (where hyperlinks are
created manually and Aspose.Cells is then used to import them). Aspose.Cells provides a class, Workbook that
represents an Excel file. The Workbook class contains a WorksheetCollection
that allows access to each worksheet in the Excel file. A worksheet is
represented by the Worksheet class. The Worksheet class provides different
methods for adding different types of hyperlinks to Excel files.
//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");
Overview:
Aspose.Cells for Android
Aspose.Cells
for Android is a MS Excel spreadsheet component that allows programmer to
develop android applications for reading, writing & manipulate Excel
spreadsheets (XLS, XLSX, XLSM, SpreadsheetML, CSV, tab delimited) and HTML file
formats without needing to rely on Microsoft Excel. It supports robust formula
calculation engine, pivot tables, VBA, workbook encryption, named ranges,
custom charts, spreadsheet formatting, drawing objects like images, OLE objects
& importing or creating charts.