JXLS - 如何在工作簿中创建指向 Excel 个工作表的超链接

JXLS - how to create hyperlink to Excel worksheets in workbook

我正在尝试使用 JXLS 创建一个 Excel 工作簿。我想要一个文本超链接来浏览工作簿中的工作表。我在网上找不到任何有用的信息。请提供任何有助于解决问题的想法或超链接。谢谢

jXLS is a small and easy-to-use Java library for writing Excel files using XLS templates and reading data from Excel into Java objects using XML configuration. If you are trying to create hyerlink, jXLS doen't have low lever excel manupulation capability. But you can to use Apache POI 一个免费图书馆。此代码为该任务创建指向单元格的超链接,如下所示。

        //creating the cell
        Row row = my_sheet.createRow(0);                
        Cell cell = row.createCell(0);

        //creating helper class
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFCreationHelper helper= workbook.getCreationHelper();

        //creating the hyperlink
        link = helper.createHyperlink(HSSFHyperlink.LINK_DOCUMENT);
        link.setAddress("'target_worksheet_name'!A1");

        //optional hyperlink style
        XSSFCellStyle hlinkstyle = workbook.createCellStyle();
        XSSFFont hlinkfont = workbook.createFont();
        hlinkfont.setUnderline(XSSFFont.U_SINGLE);
        hlinkfont.setColor(HSSFColor.BLUE.index);
        hlinkstyle.setFont(hlinkfont);

        //applying the hyperlink to the cell
        cell.setHyperlink(link);

jxls支持参数化公式,你大概可以

使用具有如下公式的单元格 =HYPERLINK("http://test.com/", "Click ME")

在单元格中对其进行参数化 =HYPERLINK(${paramLink}, ${paramDisplay})

将参数传递给 jxls 上下文,它们将被正确呈现 link

http://jxls.sourceforge.net/samples/param_formulas.html

老问题,但另一种可能的解决方案是使用 JXLS 2+ 和 PoiTransformer。它有一个名为 PoiUtil 的实用程序 class,可以将其注入到上下文中。

final var transformer = PoiTransformer.createTransformer(inputStream, outputStream);

// it is important to create the context like this
// or you can manually insert the PoiUtil instance if you wish

final var context = PoiTransformer.createInitialContext();
// setup your context...

JxlsHelper.getInstance().processTemplate(context, transformer);

并且在模板中您可以这样使用它:${util.hyperlink(linkVar, titleVar)} 其中 linkVartitleVar 是上下文中的相应变量。