2016-03-30 21 views
0

2つのdocxファイルを読み込み、最初のセルのテキストを2番目のテキストのテキストに置き換えます。置換されるべき細胞はそれらの中にマーカーを有する([#指示番号])。 テキストは、マーカーが存在するテーブルのセルに書き込まれます。私の問題は、セルに書いた後にセルの先頭に空きスペースがあるということです。画像empty space at topに「Rechnerische Ergebnisse:」という単語があります。 セルの先頭に書き込む方法、または空のスペースを削除する方法はありますか?その内容を変更しようと、このよう段落を削除しないXWPFParagraphは、1つまたは複数のXWPFRunインスタンスで構成されてDOCXでPOIを使用してセルの上から書き込む方法

private void insertText(final XWPFDocument docxErsetzt) throws FileNotFoundException, IOException { 
    FileInputStream fis = new FileInputStream("src\\main\\resources\\WordTemplate\\text.docx"); 
    XWPFDocument textInhalt = new XWPFDocument(fis); 
    List<XWPFParagraph> paragraphsInhat = textInhalt.getParagraphs(); 
    List<XWPFTable> table = docxErsetzt.getTables(); 
    for (XWPFTable xwpfTable : table) { 
     List<XWPFTableRow> row = xwpfTable.getRows(); 
     for (XWPFTableRow xwpfTableRow : row) { 
      List<XWPFTableCell> cell = xwpfTableRow.getTableCells(); 
      for (XWPFTableCell c : cell) { 
       if (c.getText().contains("[#indicator#]")) { 
        // remove [#indicator#] 
        c.removeParagraph(0); 
        for (XWPFParagraph para : paragraphsInhat) { 
         XWPFRun newRun = c.addParagraph().createRun(); 
         newRun.removeBreak(); 
         newRun.removeCarriageReturn(); 
         newRun.setText(para.getText(), 0); 

         for (XWPFRun run : para.getRuns()) { 
          String textInRun = run.getText(0); 
          if (textInRun == null || textInRun.isEmpty()) { 
           continue; 
          } 
          newRun.setFontSize(run.getFontSize()); 
          newRun.setFontFamily(run.getFontFamily()); 
          newRun.setBold(run.isBold()); 
          newRun.setItalic(run.isItalic()); 
          newRun.setStrikeThrough(run.isStrikeThrough()); 
          newRun.setUnderline(run.getUnderline()); 
          newRun.setColor(run.getColor()); 
         } 
        } 
       } 
      } 
     } 
    } 
    docxErsetzt.write(new FileOutputStream(OUTPUTPATH + "textreplaced.docx")); 
    docxErsetzt.close(); 
    textInhalt.close(); 
} 

答えて

2

、::

public void changeText(XWPFParagraph p, String newText) { 
    List<XWPFRun> runs = p.getRuns(); 
    for(int i = runs.size() - 1; i > 0; i--) { 
     p.removeRun(i); 
    } 
    XWPFRun run = runs.get(0); 
    run.setText(newText, 0); 
} 
+0

を削除しません。ここ コードです段落もその実行を削除しますか? – Iman

+1

@Iman段落には多くのスタイル情報が含まれています。削除するとビューに影響します –

関連する問題