Line 0
Link Here
|
|
|
1 |
package org.apache.poi.xssf.usermodel.examples; |
2 |
|
3 |
import java.io.FileOutputStream; |
4 |
|
5 |
import org.apache.poi.hssf.usermodel.HSSFFont; |
6 |
import org.apache.poi.hssf.util.HSSFColor; |
7 |
import org.apache.poi.ss.usermodel.Cell; |
8 |
import org.apache.poi.ss.usermodel.CellStyle; |
9 |
import org.apache.poi.ss.usermodel.CreationHelper; |
10 |
import org.apache.poi.ss.usermodel.Font; |
11 |
import org.apache.poi.ss.usermodel.Hyperlink; |
12 |
import org.apache.poi.ss.usermodel.Sheet; |
13 |
import org.apache.poi.ss.usermodel.Workbook; |
14 |
import org.apache.poi.xssf.usermodel.XSSFHyperlink; |
15 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
16 |
|
17 |
public class HyperlinkExample { |
18 |
|
19 |
|
20 |
public static void main(String[]args) throws Exception{ |
21 |
Workbook wb = new XSSFWorkbook(); |
22 |
|
23 |
//cell style for hyperlinks |
24 |
//by default hypelrinks are blue and underlined |
25 |
CellStyle hlink_style = wb.createCellStyle(); |
26 |
Font hlink_font = wb.createFont(); |
27 |
hlink_font.setUnderline(HSSFFont.U_SINGLE); |
28 |
hlink_font.setColor(HSSFColor.BLUE.index); |
29 |
hlink_style.setFont(hlink_font); |
30 |
|
31 |
Cell cell; |
32 |
Sheet sheet = wb.createSheet("Hyperlinks"); |
33 |
CreationHelper createHelper = wb.getCreationHelper(); |
34 |
//URL |
35 |
cell = sheet.createRow(0).createCell((short)0); |
36 |
cell.setCellValue("URL Link"); |
37 |
|
38 |
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL); |
39 |
link.setAddress("http://poi.apache.org/"); |
40 |
cell.setHyperlink(link); |
41 |
cell.setCellStyle(hlink_style); |
42 |
|
43 |
//link to a file in the current directory |
44 |
cell = sheet.createRow(1).createCell((short)0); |
45 |
cell.setCellValue("File Link"); |
46 |
link = createHelper.createHyperlink(Hyperlink.LINK_FILE); |
47 |
link.setAddress("link1.xls"); |
48 |
cell.setHyperlink(link); |
49 |
cell.setCellStyle(hlink_style); |
50 |
|
51 |
//e-mail link |
52 |
cell = sheet.createRow(2).createCell((short)0); |
53 |
cell.setCellValue("Email Link"); |
54 |
link = createHelper.createHyperlink(Hyperlink.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 |
|
60 |
//link to a place in this workbook |
61 |
|
62 |
//create a target sheet and cell |
63 |
Sheet sheet2 = wb.createSheet("Target Sheet"); |
64 |
sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell"); |
65 |
|
66 |
cell = sheet.createRow(3).createCell((short)0); |
67 |
cell.setCellValue("Worksheet Link"); |
68 |
XSSFHyperlink link2 = (XSSFHyperlink)createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT); |
69 |
link2.setAddress("'Target Sheet'!A1"); |
70 |
link2.setLocation("'Target Sheet'!A1"); |
71 |
cell.setHyperlink(link2); |
72 |
cell.setCellStyle(hlink_style); |
73 |
|
74 |
FileOutputStream out = new FileOutputStream("hyperinks.xlsx"); |
75 |
wb.write(out); |
76 |
out.close(); |
77 |
|
78 |
} |
79 |
} |