2016-03-20 10 views
0

Ich versuche, aus einer Textdatei zu lesen in, und wenn die Leitung einen Link enthält, habe ich die Hyperlink vorherige Zelle zu solchen Link:Java POI Einstellung nicht vorherige Zelle Hyperlink

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); 
    } 

} 

Ich bin Drucken aller Verbindungen und Zellkoordinaten auf die Konsole, um sicherzustellen, dass die Verbindungen auf die Zelle gesetzt werden, die sie sein sollten, und sie sind es auch. Aber mein Problem ist nur die letzte Zelle in allen Daten bekommt einen Hyperlink daran angehängt. Irgendwelche Ideen warum?

Voll Code für diejenigen, die es hilfreich sein:

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(); 

} 

Eingabeformat ist wie so:

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> 

Antwort

1

Dies ist ein Umfang Thema. Verschieben Sie die Erstellung des Verknüpfungsobjekts in die if-Anweisung.

  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. 
+0

Das hat den Trick, danke! – JesterXIII

Verwandte Themen