ExcelまたはOpenOfficeにハイパーリンクを挿入する

データにリンクを追加する

ハイパーリンクの追加

Aspose.Cellsを使用してセルには3種類のハイパーリンクを追加することができます:

Aspose.Cellsを使用すると、APIを使用するかデザイナースプレッドシート(ハイパーリンクが手動で作成され、Aspose.Cellsが他のスプレッドシートにインポートされるスプレッドシート)を使用して、Excelファイルにハイパーリンクを追加できます。

Aspose.Cellsは、Microsoft Excelファイルを表すクラスであるWorkbookを提供します。Workbookクラスには、Excelファイルの各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスによって表されます。Worksheetクラスには、Excelファイルにさまざまなハイパーリンクを追加するための異なるメソッドが用意されています。

URLへのリンクの追加

WorksheetクラスにはHyperlinksコレクションが含まれています。Hyperlinksコレクションの各アイテムはHyperlinkを表しています。HyperlinksコレクションのAddメソッドを呼び出すことで、URLへのハイパーリンクを追加します。Addメソッドは以下のパラメーターを取ります:

  • セル名、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲の列数。
  • URL、URLアドレス。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLinkToURL.class) + "data/";
// 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(dataDir + "AddingLinkToURL_out.xls");
// Print message
System.out.println("Process completed successfully");

上記の例では、空のセルA1にURLへのハイパーリンクが追加されています。このような場合、セルが空の場合、その空のセルにもURLアドレスが値として追加されます。セルが空でなく、ハイパーリンクが追加された場合、セルの値はプレーンテキストのようになります。ハイパーリンクのように見えるようにするには、そのセルに適切な書式設定を適用してください。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLinkToURLNotEmpty.class) + "data/";
// 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(dataDir + "AddingLinkToURLNotEmpty_out.xls");

同じファイル内のセルへのリンクの追加

同じExcelファイル内のセルにハイパーリンクを追加することが可能であり、HyperlinksコレクションのAddメソッドを呼び出します。Addメソッドは内部および外部のハイパーリンクの両方に対して機能します。オーバーロードされたメソッドのバージョンの1つは以下のパラメーターを取ります:

  • セル名、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲の列数。
  • URL、対象セルのアドレス。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLinkToAnotherCell.class) + "data/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet.
WorksheetCollection worksheets = workbook.getWorksheets();
workbook.getWorksheets().add();
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(dataDir + "ALinkTACell_out.xls");
// Print message
System.out.println("Process completed successfully");

外部ファイルへのリンクの追加

外部のExcelファイルにハイパーリンクを追加することが可能であり、HyperlinksコレクションのAddメソッドを呼び出します。Addメソッドは以下のパラメーターを取ります:

  • セル名、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲の列数。
  • URL、対象のアドレス、外部のExcelファイル。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AddingLinkToExternalFile.class) + "data/";
// 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, dataDir + "book1.xls");
// Saving the Excel file
workbook.save(dataDir + "ALToEFile_out.xls");
// Print message
System.out.println("Process completed successfully");

高度なトピック