赞
踩
今天遇到个问题,在excel中插入超链接,用于直接打开表格链接的文件
@Excel(name = "参数(input)", orderNum = "12", isHyperlink = true)
private String input;
@Excel(name = "返回值(returnInfo)", orderNum = "13", isHyperlink = true)
private String returnInfo;
/** * @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; } }
ExportParams params = new ExportParams(String.format("%s-日志", date), last);
params.setDataHandler(new ExcelDataHandler());
// 本例子采用绝对路径
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、导出的效果图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。