ExcelまたはOpenOfficeにハイパーリンクを挿入する
ハイパーリンクの追加
Aspose.Cellsを使用すると、開発者はAPIまたはデザイナースプレッドシート(ハイパーリンクが手動で作成され、Aspose.Cellsが他のスプレッドシートに取り込まれるスプレッドシート)を使用して、Excelファイルにハイパーリンクを追加できます。
Aspose.Cellsは、Microsoft Excelファイルを表すWorkbookクラスを提供しています。Workbookクラスには、Excelファイル内の各ワークシートにアクセスできるWorksheetCollectionが含まれています。ワークシートはWorksheetクラスで表されます。Worksheetクラスには、Excelファイルにさまざまなハイパーリンクを追加するためのさまざまなメソッドが用意されています。
URLへのリンクの追加
WorksheetクラスにはHyperlinksコレクションが含まれています。Hyperlinksコレクションの各アイテムはHyperl linkを表します。HyperlinksコレクションのAddメソッドを呼び出すことで、URLへのハイパーリンクを追加します。Addメソッドは、以下のパラメータを受け取ります。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、URLアドレス。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding a hyperlink to a URL at "A1" cell | |
worksheet.Hyperlinks.Add("A1", 1, 1, "http:// Www.aspose.com"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
同じファイル内のセルへのリンクの追加
同じExcelファイルのセルにハイパーリンクを追加することが可能です。Addメソッドは内部ハイパーリンクと外部ハイパーリンクの両方で動作します。オーバーロードされたメソッドの1つは、以下のパラメータを受け取ります。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、対象セルのアドレス。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
workbook.Worksheets.Add(); | |
// Obtaining the reference of the first (default) worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.Hyperlinks.Add("B3", 1, 1, "Sheet2!B9"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
外部ファイルへのリンクの追加
外部Excelファイルへのハイパーリンクを追加することが可能です。Addメソッドは、以下のパラメータを受け取ります。
- セル名、ハイパーリンクが追加されるセルの名前。
- 行数、このハイパーリンク範囲の行数。
- 列数、このハイパーリンク範囲の列数。
- URL、対象のアドレス、外部のExcelファイル。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Excel object | |
int i = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[i]; | |
// Adding an internal hyperlink to the "B9" cell of the other worksheet "Sheet2" in | |
// The same Excel file | |
worksheet.Hyperlinks.Add("A5", 1, 1, dataDir + "book1.xls"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.out.xls"); |