在XWPFTable中插入新行

时间:2014-12-01 11:52:52

标签: java apache-poi

我尝试制作一个申请,以便在参考中插入新的“工作经验”;

public class TestWordPOI {

    public static void main(String[] args) throws InvalidFormatException, IOException {
        boolean add = false;

        XWPFDocument document = new XWPFDocument(OPCPackage.open(new java.io.File("C:\\Users\\luca\\Desktop\\CV_Europass_WvWinden.docx")));

        for (XWPFTable tbl : document.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                for (XWPFTableCell cell : row.getTableCells()) {
                    for (XWPFParagraph p : cell.getParagraphs()) {
                        for (XWPFRun r : p.getRuns()) {
                            String text = r.getText(0);
                            System.out.println("riga? :"+text);

                            if ((text!=null)&&text.contains("Work experience") ) {
//when i find "work experience" i need insert the new work experience
                           add=true;
                            }
                            if( add ) 
                                break;
                        }
                        if( add ) 
                            break;
                    }
                    if( add ) 
                        break;
                }
                if( add ) {

                    break;}         
            }
            if( add ) {

                XWPFTableRow row =tbl.createRow();   //
                row.addNewTableCell();
                XWPFTableCell cell = row.createCell();
                cell.setText("New work");

             row.addNewTableCell().setText("Code monkey");
                break;}


        }

        File output = new File("output.docx");
        document.write(new FileOutputStream(output)); 

应用程序在页面底部插入新的“工作经验”,我需要在最后的经验和标题工作经验中插入这个。 谁知道我的错误?

1 个答案:

答案 0 :(得分:1)

for (XWPFTable tbl : document.getTables()) {
            for (XWPFTableRow row : tbl.getRows()) {
                y++;
                for (XWPFTableCell cell : row.getTableCells()) {                
                   .
                   .
                   .
                   .

            if( add ) {
                XWPFTableRow row =tbl.createRow();

                 row.addNewTableCell().setText("some thing");

                tbl.addRow(row, y);

                break;}

我使用1个计数器来计算行数,当我找到工作经验时,我使用计数器插入新行中的计数器