1. 程式人生 > >poi操作execl如何在cell裡做一個超連結訪問當前路徑資料夾或檔案

poi操作execl如何在cell裡做一個超連結訪問當前路徑資料夾或檔案

  1. 這個是設定一個超連結彈出email地址, 其他類似  
  2. import java.io.FileOutputStream;  
  3. import org.apache.poi.ss.usermodel;  
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  5. import org.apache.poi.ss.usermodel.IndexedColors;  
  6. /** 
  7. * @author lance 
  8. */  
  9. public class HyperlinkExample {  
  10.   public static void main(String[]args) throws
     Exception{  
  11.       Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();  
  12.       CreationHelper createHelper = wb.getCreationHelper();  
  13.       CellStyle hlink_style = wb.createCellStyle();  
  14.       Font hlink_font = wb.createFont();  
  15.       hlink_font.setUnderline(Font.U_SINGLE);  
  16.       hlink_font.setColor(IndexedColors.BLUE.getIndex());  
  17.       hlink_style.setFont(hlink_font);  
  18.       Cell cell;  
  19.       Sheet sheet = wb.createSheet("Hyperlinks");  
  20.       //URL  
  21.       cell = sheet.createRow(0).createCell((short)0);  
  22.       cell.setCellValue("URL Link");  
  23.       Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);  
  24.       link.setAddress("http://poi.apache.org/"
    );  
  25.       cell.setHyperlink(link);  
  26.       cell.setCellStyle(hlink_style);  
  27.       //link to a file in the current directory  
  28.       cell = sheet.createRow(1).createCell((short)0);  
  29.       cell.setCellValue("File Link");  
  30.       link = createHelper.createHyperlink(Hyperlink.LINK_FILE);  
  31.       link.setAddress("link1.xls");  
  32.       cell.setHyperlink(link);  
  33.       cell.setCellStyle(hlink_style);  
  34.       //e-mail link  
  35.       cell = sheet.createRow(2).createCell((short)0);  
  36.       cell.setCellValue("Email Link");  
  37.       link = createHelper.createHyperlink(Hyperlink.LINK_EMAIL);  
  38.       //設定路徑  
  39.       link.setAddress("mailto:[email protected]?subject=Hyperlinks");  
  40.       cell.setHyperlink(link);  
  41.       cell.setCellStyle(hlink_style);  
  42.       //create a target sheet and cell  
  43.       Sheet sheet2 = wb.createSheet("Target Sheet");  
  44.       sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");  
  45.       cell = sheet.createRow(3).createCell((short)0);  
  46.       cell.setCellValue("Worksheet Link");  
  47.       Hyperlink link2 = createHelper.createHyperlink(Hyperlink.LINK_DOCUMENT);  
  48.       link2.setAddress("'Target Sheet'!A1");  
  49.       cell.setHyperlink(link2);  
  50.       cell.setCellStyle(hlink_style);  
  51.       FileOutputStream out = new FileOutputStream("hyperinks.xlsx");  
  52.       wb.write(out);  
  53.       out.close();  
  54.   }  
  55. }