简体   繁体   English

Apache POI Word教程。

[英]Apache POI Word tutorial.

Does someone know good tutorials about working with MS Word using Apache POI library? 有人知道使用Apache POI库使用MS Word的好教程吗? I want to understand how I can create word documents .doc (or may be I need to create template .dot ) to create variables like ${customer.name} inside docs(templates), and then replace it via Range.replaceText("${customer.name}","Microsoft CO"); 我想了解如何创建word文档.doc (或者我可能需要创建模板.dot )来创建文档(模板)中的${customer.name}类的变量,然后通过Range.replaceText("${customer.name}","Microsoft CO");替换它Range.replaceText("${customer.name}","Microsoft CO"); Or maybe ${customer.name} is not a variable and is only plain text (which is named like variable name for more clarity)? 或者${customer.name}可能不是变量而只是纯文本(为了更清晰起见,它被命名为变量名)? Also I didn't find tutorials about working with Tables in POI. 此外,我没有找到有关在POI中使用表格的教程。

In fact, I have .doc document where I need to replace some variables like Name, Surname, and also I need to populate table with some values. 事实上,我有.doc文件,我需要替换一些变量,如Name,Surname,我还需要用一些值填充表。

You can use bookmarks inside Word documents using Microsoft Word. 您可以使用Microsoft Word在Word文档中使用书签。 I have done it one week ago. 我已经在一周前完成了。 I inserted bookmarks in the document by selecting the text that will be replaced then insert -> bookmark and type a new bookmark name. 我通过选择要替换的文本然后插入 - >书签并键入新的书签名称,在文档中插入书签。 After that in your code you should implement something like this in your util classes : 在你的代码中,你应该在你的util类中实现这样的东西:

/** 
 * Inserts a value at a location within the Word document specified by a 
 * named bookmark. 
 * 
 * @param bookmarkName An instance of the String class that encapsulates 
 *        the name of the bookmark. Note that case is important and the case 
 *        of the bookmarks name within the document and that of the value 
 *        passed to this parameter must match. 
 * @param bookmarkValue An instance of the String class that encapsulates 
 *        the value that should be inserted into the document at the location 
 *        specified by the bookmark. 
 */ 
public final void insertAtBookmark(String bookmarkName, String bookmarkValue, Style style) { 
    List<XWPFTable> tableList = null; 
    Iterator<XWPFTable> tableIter = null; 
    List<XWPFTableRow> rowList = null; 
    Iterator<XWPFTableRow> rowIter = null; 
    List<XWPFTableCell> cellList = null; 
    Iterator<XWPFTableCell> cellIter = null; 
    XWPFTable table = null; 
    XWPFTableRow row = null; 
    XWPFTableCell cell = null; 

    // Firstly, deal with any paragraphs in the body of the document. 
    this.procParaList(this.document.getParagraphs(), bookmarkName, bookmarkValue, style); 

    // Then check to see if there are any bookmarks in table cells. To do this 
    // it is necessary to get at the list of paragraphs 'stored' within the 
    // individual table cell, hence this code which get the tables from the 
    // document, the rows from each table, the cells from each row and the 
    // paragraphs from each cell. 
    tableList = this.document.getTables(); 
    tableIter = tableList.iterator(); 
    while(tableIter.hasNext()) { 
        table = tableIter.next(); 
        rowList = table.getRows(); 
        rowIter = rowList.iterator(); 
        while(rowIter.hasNext()) { 
            row = rowIter.next(); 
            cellList = row.getTableCells(); 
            cellIter = cellList.iterator(); 
            while(cellIter.hasNext()) { 
                cell = cellIter.next(); 
                this.procParaList(cell.getParagraphs(), 
                        bookmarkName, 
                        bookmarkValue, style); 
            } 
        } 
    } 
}

/** 
 * Inserts text into the document at the position indicated by a specific 
 * bookmark. Note that the current implementation does not take account 
 * of nested bookmarks, that is bookmarks that contain other bookmarks. Note 
 * also that any text contained within the bookmark itself will be removed. 
 * 
 * @param paraList An instance of a class that implements the List interface 
 *        and which encapsulates references to one or more instances of the 
 *        XWPFParagraph class. 
 * @param bookmarkName An instance of the String class that encapsulates the 
 *        name of the bookmark that identifies the position within the 
 *        document some text should be inserted. 
 * @param bookmarkValue An instance of the AString class that encapsulates 
 *        the text that should be inserted at the location specified by the 
 *        bookmark. 
 */ 
private final void procParaList(List<XWPFParagraph> paraList, 
        String bookmarkName, String bookmarkValue,Style style) { 
    Iterator<XWPFParagraph> paraIter = null; 
    XWPFParagraph para = null; 
    List<CTBookmark> bookmarkList = null; 
    Iterator<CTBookmark> bookmarkIter = null; 
    CTBookmark bookmark = null; 
    XWPFRun run = null; 
    Node nextNode = null; 

    // Get an Iterator to step through the contents of the paragraph list. 
    paraIter = paraList.iterator(); 
    while(paraIter.hasNext()) { 
        // Get the paragraph, a llist of CTBookmark objects and an Iterator 
        // to step through the list of CTBookmarks. 
        para = paraIter.next(); 
        bookmarkList = para.getCTP().getBookmarkStartList(); 
        bookmarkIter = bookmarkList.iterator(); 

        while(bookmarkIter.hasNext()) { 
            // Get a Bookmark and check it's name. If the name of the 
            // bookmark matches the name the user has specified... 
            bookmark = bookmarkIter.next(); 
            if(bookmark.getName().equals(bookmarkName)) { 
                // ...create the text run to insert and set it's text 
                // content and then insert that text into the document. 
                run = para.createRun(); 
                run.setText(bookmarkValue); 
                //run.set
                /*CTR ctr = run.getCTR(); 
                CTRPr ctrPr = ctr.getRPr(); 

                if(ctrPr == null) { 
                    ctrPr = ctr.addNewRPr(); 
                } */
                if(defaultStyle != null || style != null){
                //ctrPr.addNewRFonts().setAscii("Calibri");
                    if(style != null){
                        applyStyleToRun(style, run);
                        if(style.isCenter()) para.setAlignment(ParagraphAlignment.CENTER);
                    }else{
                        if(defaultStyle.isCenter()) para.setAlignment(ParagraphAlignment.CENTER);
                        applyStyleToRun(defaultStyle, run);
                    }

                }

                // The new Run should be inserted between the bookmarkStart 
                // and bookmarkEnd nodes, so find the bookmarkEnd node. 
                // Note that we are looking for the next sibling of the 
                // bookmarkStart node as it does not contain any child nodes 
                // as far as I am aware. 
                nextNode = bookmark.getDomNode().getNextSibling(); 
                // If the next node is not the bookmarkEnd node, then step 
                // along the sibling nodes, until the bookmarkEnd node 
                // is found. As the code is here, it will remove anything 
                // it finds between the start and end nodes. This, of course 
                // comepltely sidesteps the issues surrounding boorkamrks 
                // that contain other bookmarks which I understand can happen. 
                while(nextNode != null &&  nextNode.getNodeName() != null &&  !(nextNode.getNodeName().contains("bookmarkEnd"))) { 
                    para.getCTP().getDomNode().removeChild(nextNode); 
                    nextNode = bookmark.getDomNode().getNextSibling(); 
                } 

                // Finally, insert the new Run node into the document 
                // between the bookmarkStrat and the bookmarkEnd nodes. 
                para.getCTP().getDomNode().insertBefore( 
                        run.getCTR().getDomNode(), 
                        nextNode); 
            } 
        } 
    } 
}

/**
 * Applique un style sur un XWPFRun 
 * @param style
 * @param run
 */
private void applyStyleToRun(Style style, XWPFRun run) {
    run.setFontSize(style.getFontSize());
    if(style.getFontFamily() != null){
        run.setFontFamily(style.getFontFamily());
    }
    run.setBold(style.isBold());
    run.setItalic(style.isItalic());
    if(style.getColorCode() != null){
        run.getCTR().addNewRPr().addNewColor().setVal(style.getColorCode());
    }

} 

Original code found here : http://apache-poi.1045710.n5.nabble.com/Replacing-the-value-of-the-bookmarks-td5710052.html 原始代码在此处: http//apache-poi.1045710.n5.nabble.com/Replacing-the-value-of-the-bookmarks-td5710052.html

Did you get a chance to try 你有机会尝试吗?

http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/ http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hwpf/

I would suggest checkout the source as a separate project in your IDE and refer to the relevant testcases. 我建议将源代码作为IDE中的单独项目签出,并参考相关的测试用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM