You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Hyperlinks.java 4.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.examples.hssf.usermodel;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import org.apache.poi.common.usermodel.HyperlinkType;
  19. import org.apache.poi.hssf.usermodel.HSSFCell;
  20. import org.apache.poi.hssf.usermodel.HSSFCellStyle;
  21. import org.apache.poi.hssf.usermodel.HSSFCreationHelper;
  22. import org.apache.poi.hssf.usermodel.HSSFFont;
  23. import org.apache.poi.hssf.usermodel.HSSFHyperlink;
  24. import org.apache.poi.hssf.usermodel.HSSFSheet;
  25. import org.apache.poi.hssf.usermodel.HSSFWorkbook;
  26. import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
  27. import org.apache.poi.ss.usermodel.Font;
  28. /**
  29. * Demonstrates how to create hyperlinks.
  30. */
  31. public class Hyperlinks {
  32. public static void main(String[] args) throws IOException {
  33. try (HSSFWorkbook wb = new HSSFWorkbook()) {
  34. HSSFCreationHelper helper = wb.getCreationHelper();
  35. //cell style for hyperlinks
  36. //by default hyperlinks are blue and underlined
  37. HSSFCellStyle hlinkStyle = wb.createCellStyle();
  38. HSSFFont hlinkFont = wb.createFont();
  39. hlinkFont.setUnderline(Font.U_SINGLE);
  40. hlinkFont.setColor(HSSFColorPredefined.BLUE.getIndex());
  41. hlinkStyle.setFont(hlinkFont);
  42. HSSFCell cell;
  43. HSSFSheet sheet = wb.createSheet("Hyperlinks");
  44. //URL
  45. cell = sheet.createRow(0).createCell(0);
  46. cell.setCellValue("URL Link");
  47. HSSFHyperlink link = helper.createHyperlink(HyperlinkType.URL);
  48. link.setAddress("https://poi.apache.org/");
  49. cell.setHyperlink(link);
  50. cell.setCellStyle(hlinkStyle);
  51. //link to a file in the current directory
  52. cell = sheet.createRow(1).createCell(0);
  53. cell.setCellValue("File Link");
  54. link = helper.createHyperlink(HyperlinkType.FILE);
  55. link.setAddress("link1.xls");
  56. cell.setHyperlink(link);
  57. cell.setCellStyle(hlinkStyle);
  58. //e-mail link
  59. cell = sheet.createRow(2).createCell(0);
  60. cell.setCellValue("Email Link");
  61. link = helper.createHyperlink(HyperlinkType.EMAIL);
  62. //note, if subject contains white spaces, make sure they are url-encoded
  63. link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
  64. cell.setHyperlink(link);
  65. cell.setCellStyle(hlinkStyle);
  66. //link to a place in this workbook
  67. //create a target sheet and cell
  68. HSSFSheet sheet2 = wb.createSheet("Target Sheet");
  69. sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
  70. cell = sheet.createRow(3).createCell(0);
  71. cell.setCellValue("Worksheet Link");
  72. link = helper.createHyperlink(HyperlinkType.DOCUMENT);
  73. link.setAddress("'Target Sheet'!A1");
  74. cell.setHyperlink(link);
  75. cell.setCellStyle(hlinkStyle);
  76. try (FileOutputStream out = new FileOutputStream("hssf-links.xls")) {
  77. wb.write(out);
  78. }
  79. }
  80. }
  81. }