public static void hyperLink() throws Exception{
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Hyper Links");
XSSFCell cell;
CreationHelper createHelper = workbook.getCreationHelper();
XSSFCellStyle hlinkstyle = workbook.createCellStyle();
XSSFFont hlinkfont = workbook.createFont();
hlinkfont.setUnderline(XSSFFont.U_SINGLE);
hlinkfont.setColor(HSSFColor.BLUE.index);
hlinkstyle.setFont(hlinkfont);
//URL Link
cell = sheet.createRow(1).createCell((short)1);
cell.setCellValue("URL Link");
XSSFHyperlink hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
hyperLink.setAddress("http://www.yiibai.com");
cell.setHyperlink(hyperLink);
cell.setCellStyle(hlinkstyle);
//Hyperlink to a file in the current directory
cell = sheet.createRow(2).createCell(1);
cell.setCellValue("File Link");
hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_FILE);
hyperLink.setAddress("cellstyle.xlsx");
cell.setHyperlink(hyperLink);
cell.setCellStyle(hlinkstyle);
//e-mail link
cell = sheet.createRow(3).createCell((short) 1);
cell.setCellValue("Email Link");
hyperLink = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
hyperLink.setAddress("mailto:contact@yiibai.com?"
+"subject=Hyperlink");
cell.setHyperlink(hyperLink);
cell.setCellStyle(hlinkstyle);
FileOutputStream fos = new FileOutputStream(new File("hyperLink.xlsx"));
workbook.write(fos);
fos.close();
System.out.println("hyperlink.xlsx written successfully");
}
Apache POI(6):超链接(HyperLink)
最新推荐文章于 2024-04-15 18:58:54 发布