gpt4 book ai didi

java - Excel 超链接 URI 片段未编码

转载 作者:行者123 更新时间:2023-12-02 08:43:06 24 4
gpt4 key购买 nike

我有一个像这样的字符串

String uriString = "#'Tab1 Tab2'!A8";
URI uri = new URI(uriString);

此操作失败,因为它不是格式正确的 URI(有空格)。所以我改为

String uriString = "#'Tab1%20Tab2'!A8";
URI uri = new URI(uriString);

编码正确,但片段变为“'Tab1%20Tab2'!A8”

然后,在 Excel 中,链接(附加到图像)指向不存在的“Tab1%20Tab2'!A8”。

我尝试将构造函数更改为

URI uri = new URI(null,null,uriString,null,null)

结果,现在 Excel 上的链接是“%23'Tab1 Tab2'!A8”,该链接现在不起作用,因为它是现在已转换的主题标签。这让我发疯。

这是构建 URI 并设置链接的代码

      String hyperlinkAddress = "#'"+destinationTab + "'!" + destinationCell + "";
hyperlinkAddress = hyperlinkAddress.replaceAll(" ", "%20");
URI hyperLinkUri;
try {
hyperLinkUri = new URI(hyperlinkAddress);
PackageRelationship packagerelationship = drawing.getPackagePart().addRelationship(hyperLinkUri,TargetMode.EXTERNAL, PackageRelationshipTypes.HYPERLINK_PART);
String rid = packagerelationship.getId();
if (cthyperlink == null) cthyperlink = ctnonvisualdrawingprops.addNewHlinkClick();
cthyperlink.setId(rid);
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

如何才能实现超链接中的空格只是一个空格并且 # 不会转换为 %23?

更新

显然,在 Office 365 中它显示 %23,而在 Office 2013 中则显示正确的名称文件

更新这是导出 Excel 时超链接之一在绘图 xml 中显示的方式。

 <Relationship Id="rId28" Target="%23Coverage%20Summary%20(Pool)!A8" TargetMode="External" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"/>

但是,当我将链接悬停在 Excel 中时,链接显示为

file:///C:\Users\myname\download\%23Coverage Summary (Pool)!A8

因此,空格被正确替换,但 # URI 片段未被替换。在 Office 2013 中,同一文件中,%23 被替换为正确打开超链接的文件名

最佳答案

您似乎想要创建 HyperlinkType.DOCUMENT 的超链接它链接到工作表“Tab1 Tab2”中的单元格 A8

https://svn.apache.org/repos/asf/poi/tags/REL_3_17_FINAL/src/examples/src/org/apache/poi/xssf/usermodel/examples/中有一个如何创建不同类型超链接的示例。对于apache poi 3.17https://svn.apache.org/repos/asf/poi/tags/REL_4_1_2/src/examples/src/org/apache/poi/xssf/usermodel/examples/对于 apache poi 4.1.2 和 https://svn.apache.org/repos/asf/poi/tags/REL_5_0_0/src/examples/src/org/apache/poi/examples/xssf/usermodel/对于apache poi 5.0.0

以下摘录仅显示适用于您的用例的完整代码示例:

import java.io.FileOutputStream;

import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

//See https://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xssf/usermodel/examples/HyperlinkExample.java

public class HyperlinkTypeDocumentExample {

public static void main(String[]args) throws Exception {
try (Workbook wb = new XSSFWorkbook()) { //or new HSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();

//cell style for hyperlinks
//by default hyperlinks are blue and underlined
CellStyle hlink_style = wb.createCellStyle();
Font hlink_font = wb.createFont();
hlink_font.setUnderline(Font.U_SINGLE);
hlink_font.setColor(IndexedColors.BLUE.getIndex());
hlink_style.setFont(hlink_font);

Cell cell;
Sheet sheet = wb.createSheet("Hyperlinks");
//URL
cell = sheet.createRow(0).createCell(0);
cell.setCellValue("URL Link");

//link to a place in this workbook

//create a target sheet and cell
Sheet sheet2 = wb.createSheet("Tab1 Tab2");
sheet2.createRow(7).createCell(0).setCellValue("Target Cell");

cell = sheet.createRow(1).createCell(0);
cell.setCellValue("Worksheet Link");
Hyperlink link = createHelper.createHyperlink(HyperlinkType.DOCUMENT);
link.setAddress("'" + sheet2.getSheetName() + "'!A8");
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);

try (FileOutputStream out = new FileOutputStream("Hyperinks.xlsx")) {
wb.write(out);
}
}
}
}
<小时/>

结果来自问题作者的评论,即超链接应位于图像上。但是,只有在工作表名称不包含任何空格的情况下,才可以将指向同一工作簿中另一个工作表的超链接插入到图像中。此处显示:Apache poi putting hyperlink in images .

对于图片(图像)上的链接,目标需要是 URI,并且 URI 必须是 URI 编码的。因此,如果工作表名称包含空格,则需要将其编码为 %20。因此 #'Tab1 Tab2'!A8 需要是 #'Tab1%20Tab2'!A8。但 Excel 本身违反了这些规则,因为它期望目标为 #'Tab1 Tab2'!A8。我看不到任何使用 apache poi 生成错误 URI 的方法。

关于java - Excel 超链接 URI 片段未编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61251131/

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