easypoi插入超链接

今天遇到个问题,在excel中插入超链接,用于直接打开表格链接的文件

1. 类似这种,ctrl+鼠标左键能直接打开,下面用easypoi来实现

image.png

2. 查看easypoi文档,字段@Excel注解属性 isHyperlink = true 打开超链接插入功能

easypoi超链接

3. 实际代码实现
  1. 实体类字段属性开启超链接
@Excel(name = "参数(input)", orderNum = "12", isHyperlink = true)
private String input;
@Excel(name = "返回值(returnInfo)", orderNum = "13", isHyperlink = true)
private String returnInfo;
  1. 编写属性拦截器
/**
 * @author :yepk
 * @version :1.0
 * @apiNote :用于导出
 * @date :2019-04-24-9:25
 */

public class ExcelDataHandler extends ExcelDataHandlerDefaultImpl<ShakerRequestLogInfo> {
    @Override
    public Hyperlink getHyperlink(CreationHelper creationHelper, ShakerRequestLogInfo obj, String name, Object value) {
        Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.FILE);
        hyperlink.setLabel(name);
        hyperlink.setAddress((String) value);
        return hyperlink;
    }
}
  1. 导出参数设置数据拦截器
ExportParams params = new ExportParams(String.format("%s-日志", date), last);
params.setDataHandler(new ExcelDataHandler());
  1. 给对应实体类字段添加 超链接
// 本例子采用绝对路径
String inputPath = "txt/" + System.currentTimeMillis() + "-input.txt";
// 具体逻辑自行书写
info.setInput(inputPath);

4、完整版测试用例

/**
 * @author :yepk
 * @version :1.0
 * @apiNote :测试类
 * @date :2023-07-28-15:52
 */
@Data
public class TempBean implements Serializable {

    @Excel(name = "序号", orderNum = "0")
    private Integer id;

    @Excel(name = "标题", orderNum = "1")
    private String title;

    @Excel(name = "链接", orderNum = "2", isHyperlink = true)
    private String path;


    public static void main(String[] args) {
        List<TempBean> beanList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            TempBean temp = new TempBean();
            temp.setId(i + 1);
            temp.setTitle("标题" + (i + 1));
            temp.setPath("https://www.baidu.com/s?wd=" + i);
            beanList.add(temp);
        }

        ExportParams params = new ExportParams("测试", "测试");
        params.setDataHandler(new ExcelDataHandler());
        Workbook workbook = ExcelExportUtil.exportExcel(params, TempBean.class, beanList);
        try {
            OutputStream os = new FileOutputStream("C:\\Users\\yepk\\Desktop\\test.xls");
            workbook.write(os);
            workbook.close();
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class ExcelDataHandler extends ExcelDataHandlerDefaultImpl<TempBean> {
    @Override
    public Hyperlink getHyperlink(CreationHelper creationHelper, TempBean obj, String name, Object value) {
        Hyperlink hyperlink = creationHelper.createHyperlink(HyperlinkType.URL);
        hyperlink.setLabel(name);
        hyperlink.setAddress((String) value);
        return hyperlink;
    }
}

5、导出的效果图
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值