我正在尝试从文本文件中读取,当该行包含链接时,我将前一个单元格的超链接设置为这样的链接:
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>
答案 0 :(得分:1)
这是一个范围问题。在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.