POI 實現Excel文件中點擊超鏈接跳轉到某sheet頁某列某行的功能


POI 實現Excel文件中點擊超鏈接跳轉到某sheet頁某列某行的功能(poi的jar包見附件文件  3.12.rar)

描述:
生成在Excel文件中設置超鏈接實現點擊某單元格時跳轉到某sheet頁某列某行的功能,比如

在匯總的頁面點擊數字(表示總個數)跳轉到詳細頁面,查看每一行數據

 

 

 

public static void main(String[] args) throws IOException {

/* !使用POI版本:3.10-FINAL*/

 

/* 建立新HSSFWorkbook對象*/

HSSFWorkbook wb = new HSSFWorkbook();

 

                /* 建立新的sheet對象*/

HSSFSheet sheet = wb.createSheet("匯總頁面");

HSSFRow row = sheet.createRow((short)0);

 

/* 連接跳轉*/

HSSFCell likeCell = row.createCell((short)0);      

Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);

        // "#"表示本文檔    "明細頁面"表示sheet頁名稱  "A10"表示第幾列第幾行

hyperlink.setAddress("#明細頁面!A10");

likeCell.setHyperlink(hyperlink);

// 點擊進行跳轉

likeCell.setCellValue("1");

 

/* 設置為超鏈接的樣式*/

HSSFCellStyle linkStyle = wb.createCellStyle();

HSSFFont cellFont= wb.createFont();

cellFont.setUnderline((byte) 1);

cellFont.setColor(HSSFColor.BLUE.index);

linkStyle.setFont(cellFont);

likeCell.setCellStyle(linkStyle);

 

/* 建立第二個sheet對象*/

HSSFSheet sheet2 = wb.createSheet("明細頁面");  //建立新的sheet對象

 

for (int i = 0; i < 30; i++) {

HSSFRow row2 = sheet2.createRow((short)i);

HSSFCell cell2 = row2.createCell((short)0);

cell2.setCellValue("測試第"+(i+1)+"行");

}

 

/* 輸出文件*/

FileOutputStream fileOut = new FileOutputStream("D:\\匯總和明細.xls");

wb.write(fileOut);

fileOut.close();

}

  1. public static void main(String[] args) throws IOException {  
  2.         /* !使用POI版本:3.10-FINAL*/  
  3.           
  4.         /* 建立新HSSFWorkbook對象*/  
  5.         HSSFWorkbook wb = new HSSFWorkbook();  
  6.           
  7.                 /* 建立新的sheet對象*/  
  8.         HSSFSheet sheet = wb.createSheet("匯總頁面");  
  9.         HSSFRow row = sheet.createRow((short)0);  
  10.           
  11.         /* 連接跳轉*/  
  12.         HSSFCell likeCell = row.createCell((short)0);        
  13.         Hyperlink hyperlink = new HSSFHyperlink(Hyperlink.LINK_DOCUMENT);  
  14.             // "#"表示本文檔    "明細頁面"表示sheet頁名稱  "A10"表示第幾列第幾行  
  15.         hyperlink.setAddress("#明細頁面!A10");  
  16.         likeCell.setHyperlink(hyperlink);  
  17.         // 點擊進行跳轉  
  18.         likeCell.setCellValue("1");  
  19.           
  20.         /* 設置為超鏈接的樣式*/  
  21.         HSSFCellStyle linkStyle = wb.createCellStyle();  
  22.         HSSFFont cellFont= wb.createFont();  
  23.         cellFont.setUnderline((byte1);  
  24.         cellFont.setColor(HSSFColor.BLUE.index);  
  25.         linkStyle.setFont(cellFont);  
  26.         likeCell.setCellStyle(linkStyle);  
  27.           
  28.         /* 建立第二個sheet對象*/  
  29.         HSSFSheet sheet2 = wb.createSheet("明細頁面");  //建立新的sheet對象  
  30.           
  31.         for (int i = 0; i < 30; i++) {  
  32.             HSSFRow row2 = sheet2.createRow((short)i);  
  33.             HSSFCell cell2 = row2.createCell((short)0);  
  34.             cell2.setCellValue("測試第"+(i+1)+"行");  
  35.         }  
  36.           
  37.         /* 輸出文件*/  
  38.         FileOutputStream fileOut = new FileOutputStream("D:\\匯總和明細.xls");  
  39.         wb.write(fileOut);  
  40.         fileOut.close();  
  41.     }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM