发布网友 发布时间:2022-05-24 16:44
共2个回答
热心网友 时间:2023-10-21 15:57
//设置超链接就可以了,可以参考下面的代码
//如何设置超链接
HSSFWorkbook wb = new HSSFWorkbook();
//超链接的单元格风格
//超链接默认的是蓝色底边框
HSSFCellStyle hlink_style = wb.createCellStyle();
HSSFFont hlink_font = wb.createFont();
hlink_font.setUnderline(HSSFFont.U_SINGLE);
hlink_font.setColor(HSSFColor.BLUE.index);
hlink_style.setFont(hlink_font);
HSSFCell cell;
HSSFSheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell((short)0);
cell.setCellValue("URL Link");
HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
link.setAddress("");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//链接到当前路径的一个文件
cell = sheet.createRow(1).createCell((short)0);
cell.setCellValue("File Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
link.setAddress("link1.xls");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//链接到e-mail
cell = sheet.createRow(2).createCell((short)0);
cell.setCellValue("Email Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
//note, if subject contains white spaces, make sure they are url-encoded
link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
//链接到 workbook的某个地方
//创建一个目标Sheet和单元格
HSSFSheet sheet2 = wb.createSheet("Target Sheet");
sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
cell = sheet.createRow(3).createCell((short)0);
cell.setCellValue("Worksheet Link");
link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
link.setAddress("'Target Sheet'!A1");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
FileOutputStream out = new FileOutputStream("c://hssf-links.xls");
wb.write(out);
out.close();
热心网友 时间:2023-10-21 15:58
如果是http://开头的地址, 打开excel文件,会自动识别为可链接的状态