class="MsoNormal">超链接即内容链接,通过给特定对象设置超链接,可实现与特定网页、文件、邮件、网络等的链接,点击链接载体可打开链接目标,在文档处理中是一种比较常用的功能。本文将介绍通过Java程序来操作Excel中的超链接,包括添加超链接、编辑超链接、读取超链接以及删除超链接。
程序环境:导入Spire.Xls.jar 免费版本:3.9.1;JDK1.8.0
具体代码参考以下示例:
1. 添加超链接
给Excel文档添加超链接的方法,这里支持添加多种不同类型的链接,如URL网页链接、Workbook工作簿链接、File文档链接、Unc网络路径链接等。
import com.spire.xls.*; public class AddHyperlink { public static void main(String[] args) { //加载Excel文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //获取工作表 Worksheet sheet = wb.getWorksheets().get(0); //添加URL链接,链接到网页地址 HyperLink UrlLink = sheet.getHyperLinks().add(sheet.getCellRange("B3")); UrlLink.setTextToDisplay(sheet.getCellRange("B3").getText()); UrlLink.getType().compareTo(HyperLinkType.Url); UrlLink.setAddress("https://www.baidu.com/"); //添加URL链接,链接到邮件地址 HyperLink MailLink = sheet.getHyperLinks().add(sheet.getCellRange("D3")); MailLink.setTextToDisplay(sheet.getCellRange("D3").getText()); MailLink.getType().compareTo(HyperLinkType.Url); MailLink.setAddress("mailto: support @ e-iceblue.com"); //添加File文档链接,链接到指定文档 HyperLink FileLink = sheet.getHyperLinks().add(sheet.getCellRange("B7")); FileLink.setTextToDisplay(sheet.getCellRange("B7").getText()); FileLink.getType().compareTo(HyperLinkType.File); FileLink.setAddress("E:\\IDEA\\sample.docx"); // 添加Workbook文档链接,链接到工作簿文档(这里链接到测试文档中的指定sheet中的指定单元格) HyperLink WbLink = sheet.getHyperLinks().add(sheet.getCellRange("D7")); WbLink.getType().compareTo(HyperLinkType.Workbook); WbLink.setTextToDisplay(sheet.getCellRange("D7").getText()); WbLink.setAddress("Sheet2!B5"); //添加Unc链接,链接到网络路径 HyperLink UncLink = sheet.getHyperLinks().add(sheet.getCellRange("B11")); UncLink.getType().compareTo(HyperLinkType.Unc); UncLink.setTextToDisplay(sheet.getCellRange("B11").getText()); UncLink.setAddress("\\192.168.1.108"); //保存文档 wb.saveToFile("AddHyperlink.xlsx",ExcelVersion.Version2010); wb.dispose(); } }
?
2. 编辑Excel中的超链接
import com.spire.xls.*; import com.spire.xls.collections.HyperLinksCollection; public class ModifyHyperlink { public static void main(String[] args) { //加载Excel测试文档 Workbook wb = new Workbook(); wb.loadFromFile("test.xlsx"); //获取指定工作表 Worksheet sheet = wb.getWorksheets().get(0); //获取超链接,修改超链接本文、链接地址 HyperLinksCollection link = sheet.getHyperLinks(); link.get(0).setTextToDisplay("新超链接显示文本"); link.get(0).setAddress("https://cn.bing.com/"); //保存文档 wb.saveToFile("ModifyHyperlink.xlsx"); wb.dispose(); } }
?3. 获取Excel中的超链接
import com.spire.xls.*; import com.spire.xls.collections.HyperLinksCollection; public class GetHyperlink { public static void main(String[] args) { //加载包含超链接的Excel工作簿 Workbook wb = new Workbook(); wb.loadFromFile("AddHyperlink.xlsx"); //获取工作表 Worksheet sheet = wb.getWorksheets().get(0); //获取超链接集合 HyperLinksCollection hyperLinks = sheet.getHyperLinks(); //遍历所有超链接 for (int i = 0; i< hyperLinks.getCount();i++) { //获取包含超链接的单元格行、列坐标 int row = hyperLinks.get(i).getRange().getRow(); int column = hyperLinks.get(i).getRange().getColumn(); //获取超链接文本、地址 String text = hyperLinks.get(i).getTextToDisplay(); String address = hyperLinks.get(i).getAddress(); //输出超链接获取结果 System.out.println(String.format("单元格[%d, %d] 显示文本:" ,row ,column)+ text +"\n"+ " 链接地址:"+ address); } } }
?4. 删除Excel中的超链接
import com.spire.xls.*; public class RemoveHyperlink { public static void main(String[] args) { //加载包含超链接的Excel文档 Workbook workbook = new Workbook(); workbook.loadFromFile("AddHyperlink.xlsx"); //获取第一个工作表 Worksheet sheet = workbook.getWorksheets().get(0); //移除超链接,并保留文本信息 sheet.getHyperLinks().removeAt(0); //保存文档 workbook.saveToFile("RemoveHyperlink.xlsx", ExcelVersion.Version2013); workbook.dispose(); } }
?
?