Save This Page
Home » POI-3.5 » org.apache.poi.xssf » usermodel » examples » [javadoc | source]
    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   
    9          http://www.apache.org/licenses/LICENSE-2.0
   10   
   11      Unless required by applicable law or agreed to in writing, software
   12      distributed under the License is distributed on an "AS IS" BASIS,
   13      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14      See the License for the specific language governing permissions and
   15      limitations under the License.
   16   ==================================================================== */
   17   package org.apache.poi.xssf.usermodel.examples;
   18   
   19   import java.io.FileOutputStream;
   20   
   21   import org.apache.poi.ss.usermodel;
   22   import org.apache.poi.xssf.usermodel.XSSFWorkbook;
   23   import org.apache.poi.ss.usermodel.IndexedColors;
   24   
   25   /**
   26    * Demonstrates how to create hyperlinks.
   27    */
   28   public class HyperlinkExample {
   29   
   30   
   31       public static void main(String[]args) throws Exception{
   32           Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
   33           CreationHelper createHelper = wb.getCreationHelper();
   34   
   35           //cell style for hyperlinks
   36           //by default hypelrinks are blue and underlined
   37           CellStyle hlink_style = wb.createCellStyle();
   38           Font hlink_font = wb.createFont();
   39           hlink_font.setUnderline(Font.U_SINGLE);
   40           hlink_font.setColor(IndexedColors.BLUE.getIndex());
   41           hlink_style.setFont(hlink_font);
   42   
   43           Cell cell;
   44           Sheet sheet = wb.createSheet("Hyperlinks");
   45           //URL
   46           cell = sheet.createRow(0).createCell((short)0);
   47           cell.setCellValue("URL Link");
   48   
   49           Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
   50           link.setAddress("http://poi.apache.org/");
   51           cell.setHyperlink(link);
   52           cell.setCellStyle(hlink_style);
   53   
   54           //link to a file in the current directory
   55           cell = sheet.createRow(1).createCell((short)0);
   56           cell.setCellValue("File Link");
   57           link = createHelper.createHyperlink(Hyperlink.LINK_FILE);
   58           link.setAddress("link1.xls");
   59           cell.setHyperlink(link);
   60           cell.setCellStyle(hlink_style);
   61   
   62           //e-mail link
   63           cell = sheet.createRow(2).createCell((short)0);
   64           cell.setCellValue("Email Link");
   65           link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);
   66           //note, if subject contains white spaces, make sure they are url-encoded
   67           link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
   68           cell.setHyperlink(link);
   69           cell.setCellStyle(hlink_style);
   70   
   71           //link to a place in this workbook
   72   
   73           //create a target sheet and cell
   74           Sheet sheet2 = wb.createSheet("Target Sheet");
   75           sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
   76   
   77           cell = sheet.createRow(3).createCell((short)0);
   78           cell.setCellValue("Worksheet Link");
   79           Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);
   80           link2.setAddress("'Target Sheet'!A1");
   81           cell.setHyperlink(link2);
   82           cell.setCellStyle(hlink_style);
   83   
   84           FileOutputStream out = new FileOutputStream("hyperinks.xlsx");
   85           wb.write(out);
   86           out.close();
   87   
   88       }
   89   }

Save This Page
Home » POI-3.5 » org.apache.poi.xssf » usermodel » examples » [javadoc | source]