gpt4 book ai didi

Java POI 未设置上一个单元格超链接

转载 作者:行者123 更新时间:2023-12-01 10:10:58 24 4
gpt4 key购买 nike

我正在尝试从文本文件中读入,当该行包含链接时,我将前一个单元格的超链接设置为此类链接:

while (scanner.hasNextLine()) {

String nextToken = scanner.nextLine();
if (nextToken.startsWith("<")) {

temp = Jsoup.parse(nextToken);
url = temp.select("a").first();
link.setAddress(url.attr("href"));
System.out.println(link.getAddress());
prevCol = currRow.getCell(col - 1);
System.out.println(row + ", " + col -1);
prevCol.setHyperlink(link);

} else {
currCol.setCellValue(nextToken);
col++;
currCol = currRow.createCell(col);
}

if (nextToken.isEmpty()) {
row++;
col = 0;
currRow = sheet.createRow(row);
currCol = currRow.createCell(col);
}

}

我正在将每个链接和单元格坐标打印到控制台,以确保链接被设置在它们应该在的单元格处,并且确实如此。但我的问题是所有数据中只有最后一个单元格附加了超链接。有什么想法吗?

对于那些认为它更有帮助的人来说,完整的代码:

public static void main(String[] args) throws EncryptedDocumentException, InvalidFormatException, IOException {
File file = new File("C:\\Users\\Jester\\Desktop\\data scrap payday\\finishedformat.txt");
FileInputStream fis = new FileInputStream(
new File("C:\\Users\\Jester\\Desktop\\data scrap payday\\Payday 2 Rewards.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(fis);
CreationHelper createHelper = workbook.getCreationHelper();
XSSFSheet sheet = workbook.createSheet("Achievement Rewards");
Document temp;
Element url;
Scanner scanner = new Scanner(file, "UTF-8");
XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
int row = 0;
int col = 0;
XSSFRow currRow = sheet.createRow(row);
XSSFCell currCol = currRow.createCell(col);
XSSFCell prevCol;

XSSFCellStyle hlinkstyle = workbook.createCellStyle();
XSSFFont hlinkfont = workbook.createFont();
hlinkfont.setUnderline(XSSFFont.U_SINGLE);
hlinkfont.setColor(HSSFColor.BLUE.index);
hlinkstyle.setFont(hlinkfont);
while (scanner.hasNextLine()) {

String nextToken = scanner.nextLine();
if (nextToken.startsWith("<")) {

temp = Jsoup.parse(nextToken);
url = temp.select("a").first();
link.setAddress(url.attr("href"));
System.out.println(link.getAddress());
prevCol = currRow.getCell(col - 1);
System.out.println(row + ", " + col);
prevCol.setHyperlink(link);
prevCol.setCellStyle(hlinkstyle);
System.out.println(prevCol.getHyperlink().getAddress()); //This is returning the desired link to the console too, sooooo....

} else {
currCol.setCellValue(nextToken);
col++;
currCol = currRow.createCell(col);
}

if (nextToken.isEmpty()) {
row++;
col = 0;
currRow = sheet.createRow(row);
currCol = currRow.createCell(col);
}

}

fis.close();
FileOutputStream fos = new FileOutputStream(
new File("C:\\Users\\Jester\\Desktop\\data scrap payday\\Payday 2 Rewards.xlsx"));
workbook.write(fos);
fos.close();

}

输入格式如下:

Hail to the King, Baby
In the Golden Grin Casino heist, kill "The King" and complete the heist in stealth
<a href="http://payday.wikia.com/wiki/Golden_Grin_Casino" title="Golden Grin Casino">Golden Grin Casino</a>
"Sports Utility Mask" mask
<a href="http://payday.wikia.com/wiki/Masks_(Payday_2)#The_Golden_Grin_Casino_Heist_DLC" title="Masks (Payday 2)">Sports Utility Mask</a>
"Carpet" material
<a href="http://payday.wikia.com/wiki/Materials#The_Golden_Grin_Casino_Heist_DLC" title="Materials">Carpet</a>
"Dices" pattern
<a href="http://payday.wikia.com/wiki/Patterns#The_Golden_Grin_Casino_Heist_DLC" title="Patterns">Dices</a>

最佳答案

这是一个范围问题。将链接对象创建移至 if 语句内。

            if (nextToken.startsWith("<")) {
XSSFHyperlink link = (XSSFHyperlink) createHelper.createHyperlink(Hyperlink.LINK_URL);
temp = Jsoup.parse(nextToken);
url = temp.select("a").first();
link.setAddress(url.attr("href"));
System.out.println(link.getAddress());
prevCol = currRow.getCell(col - 1);
System.out.println(row + ", " + col);
prevCol.setHyperlink(link);
prevCol.setCellStyle(hlinkstyle);
System.out.println(prevCol.getHyperlink().getAddress()); //This is returning the desired link to the console too, sooooo.

关于Java POI 未设置上一个单元格超链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36118649/

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