简体   繁体   English

使用Aspose.Cells中的超链接

[英]Working with hyperlinks in Aspose.Cells

I am generating an excel document using Aspose.Cells, and inside the document some fields will contain a URL. 我正在使用Aspose.Cells生成一个excel文档,并且在文档内部某些字段将包含一个URL。 I would like to make the URL a clickable link. 我想将URL设为可点击的链接。 I have tried adding the link to the hyperlinks collection using 我尝试使用以下方式将链接添加到超链接集合中

workSheet.Hyperlinks.Add(cell.Name, rowCount, columnCount, content);

This will add the link as desired but if the user sorts the document in Excel, the link will remain attached to the cell it originated from. 这将根据需要添加链接,但是如果用户在Excel中对文档进行排序,则该链接将保持附加到其原始单元格。 The ability to sort in Excel is a requirement. 必须具有Excel排序功能。

I have also tried 我也尝试过

cell.Formula = $"=HYPERLINK(\"{content}\",\"{content}\")";

and then styling the cell as a hyperlink. 然后将单元格样式设置为超链接。 This works okay, and the links will follow the cell with a sort. 这项工作正常,链接将以某种形式跟随单元格。 The downside is that until the user enables editing in Excel, the link will not be displayed. 缺点是,除非用户启用Excel中的编辑功能,否则将不会显示该链接。

Is there a better way to add hyperlinks to Excel with Aspose.Cells? 是否有更好的方法通过Aspose.Cells将超链接添加到Excel?

Please try the following sample code and read its comments. 请尝试以下示例代码并阅读其注释。 It works fine. 工作正常。 Please see the screenshot that shows the output Excel file generated by the code. 请查看屏幕截图,其中显示了由代码生成的输出Excel文件。 If you sort the links, they will sort correctly. 如果对链接进行排序,它们将正确排序。

C# C#

//Create workbook
Workbook wb = new Workbook();

//Access first worksheet
Worksheet ws = wb.Worksheets[0];

//Add hyperlink in cell A1
int idx = ws.Hyperlinks.Add("A1", 1, 1, "https://forum.aspose.com/c/cells");
ws.Hyperlinks[idx].TextToDisplay = "Cells Link No: 11";

//Add hyperlink in cell A2
idx = ws.Hyperlinks.Add("A2", 1, 1, "https://forum.aspose.com/c/total");
ws.Hyperlinks[idx].TextToDisplay = "Total Link No: 16";

//Add hyperlink in cell A3
idx = ws.Hyperlinks.Add("A3", 1, 1, "https://forum.aspose.com/c/words");
ws.Hyperlinks[idx].TextToDisplay = "Words Link No: 29";

//Add hyperlink in cell A4
idx = ws.Hyperlinks.Add("A4", 1, 1, "https://forum.aspose.com/c/pdf");
ws.Hyperlinks[idx].TextToDisplay = "Pdf Link No: 13";

//Add hyperlink in cell A5
idx = ws.Hyperlinks.Add("A5", 1, 1, "https://forum.aspose.com");
ws.Hyperlinks[idx].TextToDisplay = "Forum Link No: 20";

//Add hyperlink in cell A6
idx = ws.Hyperlinks.Add("A6", 1, 1, "https://forum.aspose.com");
ws.Hyperlinks[idx].TextToDisplay = "Forum Link No: 15";

//Autofit columns
ws.AutoFitColumns();

//Save the output Excel file
wb.Save("output.xlsx");

Screenshot: 屏幕截图: 在此处输入图片说明

Note: I am working as Developer Evangelist at Aspose 注意: 我在Aspose担任开发人员布道者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM