java - 在 JExcel 中写入后,如何在 Excel 中显示 'activate' 超链接?

标签 java excel jexcelapi

我的电子表格中使用 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 将其输出到电子表格中时,用户看到的是公式的纯文本(如上所示),而不是带有文本 Unicorn Profile 的超链接。

如何以编程方式激活它?我无法更改条目,因此无法遵循制作超链接的标准方法,如下所示(以半伪代码形式):

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

有没有一种简单的方法来“激活”我的超链接?

最佳答案

下面的代码,在 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(); 
  }

}

关于java - 在 JExcel 中写入后,如何在 Excel 中显示 'activate' 超链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16195140/

相关文章:

java - 如何从JAVA中的变量数组插入sql表

vba - 基于值的慢速宏隐藏行

java - 无法获得 JExcel/jxl 的正确编码

excel - 通过电子邮件发送事件工作簿的压缩版本

Java Excel Api字符编码问题

java - 包含 & 符号的公式中存在词汇错误

java - nextLine() 扫描仪 java 无法正常工作(可能是因为 Unicode)

java - 无法初始化类 org.jfree.chart.JFreeChart

java - OSHI:获取给定路径的 HWDiskStore

excel - 使用 VBA 将数据透视表设置为基于另一个字段的特定日期