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 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.hssf.usermodel.examples;
  16. import org.apache.poi.hssf.usermodel.*;
  17. import org.apache.poi.hssf.util.HSSFColor;
  18. import java.io.IOException;
  19. import java.io.FileOutputStream;
  20. /**
  21. * Demonstrates how to create hyperlinks.
  22. *
  23. * @author Yegor Kozlov (yegor at apach.org)
  24. */
  25. public class Hyperlinks {
  26. public static void main(String[] args) throws IOException {
  27. HSSFWorkbook wb = new HSSFWorkbook();
  28. //cell style for hyperlinks
  29. //by default hyperlinks are blue and underlined
  30. HSSFCellStyle hlink_style = wb.createCellStyle();
  31. HSSFFont hlink_font = wb.createFont();
  32. hlink_font.setUnderline(HSSFFont.U_SINGLE);
  33. hlink_font.setColor(HSSFColor.BLUE.index);
  34. hlink_style.setFont(hlink_font);
  35. HSSFCell cell;
  36. HSSFSheet sheet = wb.createSheet("Hyperlinks");
  37. //URL
  38. cell = sheet.createRow(0).createCell(0);
  39. cell.setCellValue("URL Link");
  40. HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
  41. link.setAddress("http://poi.apache.org/");
  42. cell.setHyperlink(link);
  43. cell.setCellStyle(hlink_style);
  44. //link to a file in the current directory
  45. cell = sheet.createRow(1).createCell(0);
  46. cell.setCellValue("File Link");
  47. link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
  48. link.setAddress("link1.xls");
  49. cell.setHyperlink(link);
  50. cell.setCellStyle(hlink_style);
  51. //e-mail link
  52. cell = sheet.createRow(2).createCell(0);
  53. cell.setCellValue("Email Link");
  54. link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
  55. //note, if subject contains white spaces, make sure they are url-encoded
  56. link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
  57. cell.setHyperlink(link);
  58. cell.setCellStyle(hlink_style);
  59. //link to a place in this workbook
  60. //create a target sheet and cell
  61. HSSFSheet sheet2 = wb.createSheet("Target Sheet");
  62. sheet2.createRow(0).createCell(0).setCellValue("Target Cell");
  63. cell = sheet.createRow(3).createCell(0);
  64. cell.setCellValue("Worksheet Link");
  65. link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
  66. link.setAddress("'Target Sheet'!A1");
  67. cell.setHyperlink(link);
  68. cell.setCellStyle(hlink_style);
  69. FileOutputStream out = new FileOutputStream("hssf-links.xls");
  70. wb.write(out);
  71. out.close();
  72. }
  73. }