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

リンク データへのハイパーリンクの追加

ハイパーリンクの追加

Aspose.Cells を使用して、3 種類のハイパーリンクをセルに追加できます。

Aspose.Cells を使用すると、開発者は API またはデザイナー スプレッドシート(ハイパーリンクが手動で作成され、他のスプレッドシートにインポートするために Aspose.Cells が使用されるスプレッドシート)。

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

URL へのリンクの追加

ワークシートクラスにはハイパーリンクコレクション。の各項目ハイパーリンクコレクションはハイパーリンク.を呼び出して URL にハイパーリンクを追加します。ハイパーリンクコレクションの[追加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。の[追加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String)メソッドは、次のパラメーターを取ります。

  • Cell name、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲の列数
  • 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");

上記の例では、空のセルの URL にハイパーリンクが追加され、A1.このような場合、セルが空の場合、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");

同じファイルに Cell へのリンクを追加する

を呼び出して、同じ Excel ファイル内のセルにハイパーリンクを追加することができます。ハイパーリンクコレクションの[追加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。の追加メソッドは、内部ハイパーリンクと外部ハイパーリンクの両方で機能します。オーバーロードされたメソッドの 1 つのバージョンは、次のパラメーターを取ります。

  • Cell name、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲内の列数。
  • 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 ファイルにハイパーリンクを追加することができます。ハイパーリンクコレクションの[追加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String))方法。の[追加](https://reference.aspose.com/cells/java/com.aspose.cells/HyperlinkCollection#add(int,%20int,%20int,%20int,%20java.lang.String)メソッドは、次のパラメーターを取ります。

  • Cell name、ハイパーリンクが追加されるセルの名前。
  • 行数、このハイパーリンク範囲の行数。
  • 列数、このハイパーリンク範囲内の列数。
  • 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");

先行トピック