用Java在excel单元格中设置超链接

(一)问题引入

  有时候我们在导入数据到excel中时可能要给某个文件或图片设置超链接,例如链接到外网或者是本地的某个目录。我们可以通过Java代码来实现,借助POI库。

(二)解决方案

  下面直接给出参考代码示例:

File file;
Workbook wb = new XSSFWorkbook(file);
Sheet sheet = wb.getSheet("sheet名称");
Row row = sheet.getRow(行号);
Cell cell = row.getCell(列号);
String name = "超链接";
cell.setCellValue(name); CreationHelper creationHelper
= workbook.getCreationHelper(); Hyperlink link = creationHelper.createHyperlink(HyperlinkType.FILE);
String url = "http://www.baidu.com"; link.setAddress(url); cell.setHyperlink(link); Font font
= workbook.createFont(); font.setColor(IndexedColors.BLUE.getIndex()); CellStyle cellStyle = workbook.createCellStyle(); cellStyle.cloneStyleFrom(cell.getCellStyle()); cellStyle.setFont(font); cell.setCellStyle(cellStyle);
20191114闪

猜你喜欢

转载自www.cnblogs.com/bien94/p/11861012.html