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.XSSFHyperlink
	XSSFHyperlink 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、测试结果:

POI操作Excel---给单元格添加超链接(HSSF & XSSF & SXSSF)_第1张图片

https://github.com/550690513


你可能感兴趣的:(Apache,POI)