2010-08-04 8 views
2

Apache POI를 사용하여 템플릿에서 docx 파일을 생성하고 있습니다. 단락의 모든 텍스트를 대체하는 분명한 방법이없는 것 같고 문서가 거의 없다. 지금은 단락을 반복하면서 각 단락의 실행을 반복 한 다음 각 실행의 텍스트를 반복하여 문서를 읽을 수 있습니다 ... 이것은 잘 작동하며 실행중인 텍스트의 내용을 바꿀 수는 있지만 템플릿 자리 표시 자 (예 : <>)는 여러 번 실행으로 나뉠 수 있으므로 실제로 일치하고 바꾸기가 복잡합니다. XWPFParagraph의 내용을 설정하는 방법이 있습니까? 또는 적어도 단락에서 모든 실행을 잠그고 내 자신의 실행을 만드는 방법은?Apache POI : 단락 텍스트 바꾸기

public static void main(String[] args) { 

    InputStream fs = null; 
    try { 
     fs = new FileInputStream("C:\\sample1.docx"); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    XWPFDocument doc = null; 
    try { 
     doc = new XWPFDocument(fs); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    for (int i = 0; i < doc.getParagraphs().length; i++) { 
     XWPFParagraph paragraph = doc.getParagraphs()[i]; 
     paragraph.getCTP().getRArray(). 

     // This will output the paragraph's contents. 
     System.out.println(paragraph.getParagraphText()); 

     for (int j = 0; j < paragraph.getCTP().getRArray().length; j++) { 
      CTR run = paragraph.getCTP().getRArray()[j]; 

      for (int k = 0; k < run.getTArray().length; k++) { 
       CTText text = run.getTArray()[k]; 

       // This will output the text contents 
       System.out.println(text.getStringValue()); 

       // And this will set its contents 
       text.setStringValue("Success!"); 
      } 
     } 
    } 

    try { 
     doc.write(new FileOutputStream("C:\\output.docx")); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

답변

5

나는 그것이 3.7 베타 2 작업 있어요 : 여기

는 내가 지금까지 가지고있는 것입니다. 이 솔루션은 이상적이지 않고 조금 뒤죽박죽이지만 사용 사례로도 사용되었습니다.

http://tkgospodinov.com/writing-microsoft-word-documents-in-java-with-apache-poi/

희망하는 데 도움이 : tkgospodinov.com을 - 트릭을 할 것입니다 XWPFDocument 번호의 setParagraph 방법은 지금이 ... 내 블로그에 POI를 사용하여 Word 문서를 작성의 예를 몇 가지있다.

관련 문제