2013-04-24 41 views
0

使用JExcel創建的電子表格中的某些列是使用相鄰單元格的超鏈接。藉此例如:如何在Excel中寫入JExcel後激活超鏈接?

|A   |B   |C   |D 
|RowNumber |Unicorn Name|Unicorn ID|Link to Unicorn Profile 
|000000001 |Jefficorn |001  |=hyperlink(concatenate("www.stackoverflow.com/unicornfinder/",C1,"/profile.html"),"Unicorn Profile") 

然而,當這是由JExcel輸出到電子表格中,用戶看到的是式I的純文本(因爲它上面顯示),而不是超鏈接與文本獨角獸資料

如何以編程方式激活它?我不能改變輸入,所以我不能跟着做一個超鏈接,其示(半僞代碼)的標準方法如下:

sheet.addHyperlink("www.stackoverflow.com/unicornfinder/"+cell.getNeighbourCell.getContent()+"/profile.html); 
sheet.addLabel("Unicorn Profile"); 

有一種簡單的方法來「激活」我超鏈接?

+0

,我不與Java在所有的工作但是在你的「半僞代碼」中,你正在獲取單元格cell.getNeighbourCell.getContent()的值。相反,爲什麼不創建一個公式然後添加它。就像[THIS](http://bethecoder.com/applications/tutorials/excel/jexcel-api/how-to-add-sum-formula-to-excel-spreadsheet2.html) – 2013-04-24 15:03:54

+0

@SiddharthRout可能工作!我找到了www.BethEcode.com,但它是一篇不同的文章。好答案! – Pureferret 2013-04-24 15:09:10

回答

1

下面的代碼,建議在comment似乎工作,但在細胞中給出了一個奇怪的價值,雖然我不知道爲什麼。

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 

import jxl.CellFormat; 
import jxl.Workbook; 
import jxl.biff.CellReferenceHelper; 
import jxl.biff.DisplayFormat; 
import jxl.read.biff.BiffException; 
import jxl.write.Formula; 
import jxl.write.Label; 
import jxl.write.Number; 
import jxl.write.WritableCellFormat; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import jxl.write.WriteException; 

public class Launcher { 

    /** 
    * @param args 
    * @throws IOException 
    * @throws IOException 
    * @throws WriteException 
    * @throws BiffException 
    */ 
    public static void main(String[] args) throws IOException, WriteException { 
    //Creates a writable workbook with the given file name 
    WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\Documents and Settings\\castone\\My Documents\\Formula.xls")); 

    WritableSheet sheet = workbook.createSheet("My Sheet", 0); 

    ArrayList<Label> labelList = new ArrayList<Label>(); 
    //create the filler text 
    labelList.add(new Label(0, 0, "UnicornName")); 
    labelList.add(new Label(0, 1, "Pureferret")); 
    labelList.add(new Label(2, 0, "Unicorn ID")); 
    labelList.add(new Label(2, 1, "1075247")); 
    labelList.add(new Label(1, 0, "Hyperlink")); 
    for(Label label:labelList){ 
     sheet.addCell(label); 
    }  
    //Create a formula for adding cells 
    String formulaText ="HYPERLINK(CONCATENATE(\"https://stackoverflow.com/users/\","+ 
      CellReferenceHelper.getCellReference(2, 1).toString()+"),\"Link\""; 
    Formula link = new Formula(1, 1,formulaText); 
    sheet.addCell(link); 

    //Writes out the data held in this workbook in Excel format 
    workbook.write(); 

    //Close and free allocated memory 
    workbook.close(); 
    } 

} 

這是什麼出現在單元格=CONCATENATE("https://stackoverflow.com/users/",C2) HYPERLINK("Link")


另外,我已經使用了這種設置搶地址的相關部分,並形成新的超鏈接出來的:

import java.io.File; 
import java.io.IOException; 
import java.net.URL; 
import java.util.ArrayList; 

import jxl.Workbook; 
import jxl.read.biff.BiffException; 
import jxl.write.Label; 
import jxl.write.WritableHyperlink; 
import jxl.write.WritableSheet; 
import jxl.write.WritableWorkbook; 
import jxl.write.WriteException; 

public class Launcher { 

    /** 
    * @param args 
    * @throws IOException 
    * @throws IOException 
    * @throws WriteException 
    * @throws BiffException 
    */ 
    public static void main(String[] args) throws IOException, WriteException { 
    //Creates a writable workbook with the given file name 
    WritableWorkbook workbook = Workbook.createWorkbook(new File("D:\\Documents and Settings\\castone\\My Documents\\Formula.xls")); 

    WritableSheet sheet = workbook.createSheet("My Sheet", 0); 

    ArrayList<Label> labelList = new ArrayList<Label>(); 
    //create the filler text 
    labelList.add(new Label(0, 0, "UnicornName")); 
    labelList.add(new Label(0, 1, "Pureferret")); 
    labelList.add(new Label(2, 0, "Unicorn ID")); 
    labelList.add(new Label(2, 1, "1075247")); 
    labelList.add(new Label(1, 0, "Hyperlink")); 
    for(Label label:labelList){ 
     sheet.addCell(label); 
    }  
    //Create a formula for adding cells 
    String formulaText ="HYPERLINK(CONCATENATE(Overview$B$15,\"users\",C2),\"Link\")"; 
    String[] linkBits = formulaText.substring(formulaText.lastIndexOf("(")+1,formulaText.indexOf(")")).split(","); 
    String baseURL = "http://www.stackoverflow.com/"; 
    String linkURL = baseURL+linkBits[1].replace("\"","")+"/"+sheet.getCell(linkBits[2]).getContents(); 
    String linkDesc = "Profile"; 
    WritableHyperlink link = new WritableHyperlink(1, 1, new URL(linkURL)); 
    link.setDescription(linkDesc); 
    sheet.addHyperlink(link); 

    //Writes out the data held in this workbook in Excel format 
    workbook.write(); 

    //Close and free allocated memory 
    workbook.close(); 
    } 

}