本文主要是介绍POI操作Excel---给单元格添加超链接(HSSF XSSF SXSSF),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1、xls格式---(HSSF)
HSSFWorkbook workbook = new HSSFWorkbook();HSSFSheet sheet = workbook.createSheet("添加超链接");HSSFRow row = sheet.createRow(0);HSSFCell cell = row.createCell(0);HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);link.setAddress("https://github.com/550690513");cell.setHyperlink(link);// 设置超链接cell.setCellValue("Fork me on Github");
2、xlsx格式---(XSSF & SXSSF)
2.1:错误写法:
XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet();XSSFRow row = sheet.createRow(0);XSSFCell cell = row.createCell(0);// 此处报'XSSFHyperlink(int)' has protected access in 'org.apache.poi.xssf.usermodel.XSSFHyperlinkXSSFHyperlink link = new XSSFHyperlink(XSSFHyperlink.LINK_URL);// 无法实例化XSSFHyperlink对象link.setAddress("https://github.com/550690513");cell.setHyperlink(link);cell.setCellValue("Fork me on Github");
2.2:正确写法:
XSSFWorkbook workbook = new XSSFWorkbook();XSSFSheet sheet = workbook.createSheet();XSSFRow row = sheet.createRow(0);XSSFCell cell = row.createCell(0);// 使用creationHelpper来创建XSSFHyperlink对象CreationHelper createHelper = workbook.getCreationHelper();XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);link.setAddress("https://github.com/550690513");cell.setHyperlink(link);cell.setCellValue("Fork me on Github");
3、测试结果:
https://github.com/550690513
这篇关于POI操作Excel---给单元格添加超链接(HSSF XSSF SXSSF)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!