gpt4 book ai didi

java - 将超链接放入 Excel 中的图像中(Apache POI)

转载 作者:行者123 更新时间:2023-12-01 21:56:46 27 4
gpt4 key购买 nike

我可以在 Apache POI 中为单元格设置超链接,但我不知道如何将超链接放入图像中(我正在使用 XSSF)

这里是放置单元格超链接的功能:

/**
* Helper function for putting hyperlink into specified cell
* @param label
* @param value
* @param col
* @param row
* @param sheet
*/
private static void putImageHyperlink(Cell cell, CellStyle hyperlinkStyle, String value, Workbook wb) {
try {
CreationHelper createHelper = wb.getCreationHelper();
Hyperlink link = createHelper.createHyperlink(Hyperlink.LINK_URL);
link.setAddress(value);
cell.setHyperlink(link);
cell.setCellStyle(hyperlinkStyle);

// Put hyperlink value
cell.setCellValue(value);
} catch (Exception e) {
log.severe("Can't create hyperlink : " + Utils.exceptionToString(e));
}
}

这是将图像放入指定单元格的函数:

    /**
* Put image into sheet at position [row,col]
* @param sheet
* @param col
* @param row
* @param imgData
* @throws Exception
*/
private static void putImage(Workbook wb, Sheet sheet, int col, int row, byte[] imgData) throws Exception {
try {
Drawing drawing = sheet.createDrawingPatriarch();
int pictureIdx = wb.addPicture(imgData, Workbook.PICTURE_TYPE_PICT);

CreationHelper helper = wb.getCreationHelper();
ClientAnchor anchor4 = helper.createClientAnchor();

//set top-left corner of the picture,
//subsequent call of Picture#resize() will operate relative to it
anchor4.setCol1(col);
anchor4.setRow1(row);
anchor4.setCol2(col+1);
anchor4.setRow2(row+1);

drawing.createPicture(anchor4, pictureIdx);
} catch (Exception ex) {
log.severe("Exception : " + Utils.exceptionToString(ex));
}
}

=> 如何将超链接放入命令 drawing.createPicture(anchor4, pictureIdx); 创建的图片中?

提前致谢!

最佳答案

当查看 Excel 如何存储它时,xl\drawings\_rels\drawing1.xml.rels 中的图像的存储方式似乎有所不同。和xl\drawings\drawing1.xml XLSX 文件的一部分:

<Relationship Id="rId1" Target="http://poi.apache.org" TargetMode="External" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"/>

<xdr:cNvPr descr="Picture" id="2" name="Picture 1">
<a:hlinkClick xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1"/>
</xdr:cNvPr>

POI 尚不支持通过其 API 添加此类超链接,但您可以使用底层底层 API,如下所示,首先创建超链接的关系,然后在图片对象中设置与超链接的关系:

    PackageRelationship rel = ((XSSFDrawing)patriarch).getPackagePart().addRelationship(
new URI("http://poi.apache.org"),
TargetMode.EXTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink");
((XSSFDrawing)patriarch).addRelation(rel.getId(),new POIXMLDocumentPart());

CTPictureNonVisual nvPicPr = ((XSSFPicture)picture).getCTPicture().getNvPicPr();
CTHyperlink hLinkClick = nvPicPr.getCNvPr().addNewHlinkClick();
hLinkClick.setId(rel.getId());

关于java - 将超链接放入 Excel 中的图像中(Apache POI),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34171084/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com