XWPF POI word文档操作

运用Poi的XWPF操作word文档


Word文档基础知识的简介

word文档包括 页眉、页脚、脚注、批注、链接、正文部分,在XWPF中都有与之对应的类:
.docx文件的文档的根 XWPFDocument
页眉 XWPFHeader
页脚 XWPFFooter
脚注 XWPFFootnote
批注 XWPFComment
链接 XWPFHyperlink
内容主体
页眉,页脚,脚注,内容主体部分由 段落(XWPFParagraph)、表格(XWPFTable)、图片(XWPFPicture)部分组成.每个类都有相应丰富的方法。了解了这些结构,对于掌握XWPF有很大帮助。每一个XWPF类型,都有与之对应的CT*,个人认为CT*更接近底层操作.

XWPFParagraph 文档、表格、标题等中的段落

CTP ctp = para.getCTP();段落可以设置对齐方式,字体,设置标签等操作。

//遍历段落
XWPFParagraph para = ...;
//通过ctp直接获取相应的段落
XWPFParagraph paraOther = document.getParagraph(ctp)
List  runs = para.getRuns(); 
    for (XWPFRun run : runs) {
    CTR ctr = run.getCTR();
    System.out.println(run.getText(0));
}
//删除段落
para.getDocument().removeBodyElement(para.getDocument().getPosOfParagraph(para));

XWPFRun 相同属性设置的文本区域

CTR ctr = run.getCTR(); XWPFRun是组成XWPFParagraph等元素,对于文本的操作更细致化的在这一个类,区分段落中的XWPFRun非常不容易,因为每个XWPFRun不同体现在很多因素。

XWPFRun run = ..
XWPFRun runOther = para.getRun(ctr);
run.setText(value, pos);
run.getText(0)
//删除
para.removeRun(index)

XWPFTable 文档中的表格

CTTbl ct = table.getCTTbl();表格存在行(XWPFTableRow), 每行存在单元格(XWPFTableCell), 每个单元格有段落(XWPFParagraph) 遍历表格:

List rows = table.getRows();
    CTTbl ct = table.getCTTbl();
    System.out.println("table text\t" + table.getText());
    System.out.println("Table rowBandSize:\t" + table.getRowBandSize());
    for (XWPFTableRow row : rows) {
    CTRow ctrow = row.getCtRow();
    XWPFTableRow rowother = table.getRow(ctrow);
    List cells = row.getTableICells();
    List xcells = row.getTableCells();
    for (XWPFTableCell cell: xcells) {
        CTTc cttc = cell.getCTTc();
        XWPFTableCell otherCell = row.getTableCell(cttc);
        System.out.println("cell text\t" + cell.getText());
    }
}

合并单元格

//跨行跨列合并
public void mergeCells(XWPFTable table, int fromrow, int endrow, int fromcell, int endcell) {
        //先合并行
    for (int rowindex = fromrow; rowindex <= endrow; rowindex++) {
        XWPFTableRow row = table.getRow(rowindex);
        if (rowindex == fromrow) {          
                row.getCell(fromcell).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
            } else {
                row.getCell(fromcell).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
            }
        }
        //合并列
    for (int rowindex = fromrow; rowindex <= endrow; rowindex++) {
        for (int cellindex = fromcell; cellindex <= endcell; cellindex++) {
            XWPFTableCell cell = table.getRow(rowindex).getCell(cellindex);
            if (cellindex == fromcell) {
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
            } else {
                cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
            }
        }
    }
}


//跨列合并
public void mergeCellsByCol(XWPFTable table, int rowindex, int fromcell, int endcell) {
    for (int cellindex = fromcell; cellindex <= endcell; cellindex++) {
        XWPFTableCell cell = table.getRow(rowindex).getCell(cellindex);
        if (cellindex == fromcell) {
            cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
        } else {
            cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
        }
    }
}


//跨行合并
public void mergeCellByRow(XWPFTable table,  int cellindex, int fromrow, int endrow) {
    for (int rowindex = fromrow; rowindex <= endrow; rowindex++) {
        XWPFTableRow row = table.getRow(rowindex);
        if (rowindex == fromrow) {
            row.getCell(cellindex).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
        } else {
            row.getCell(cellindex).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
        }
    }
}

XWPFPictureData 文档中的图片

List pictureDatas = document.getAllPictures();
public void watchPicture(XWPFPictureData pdata) {
    System.out.println("图片名称:\t" + pdata.getFileName());
    System.out.println("图片类型:\t" + pdata.getPictureType());
    byte[] data = pdata.getData();
}

word转HTML

public void wordToHtml() throws XWPFConverterException, IOException {
    //图片存储
    File imageFolderFile = new File("d:/test/media");

    XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
    options.setExtractor(new FileImageExtractor(imageFolderFile));
    OutputStream out = new FileOutputStream("E:/test/wth.html");
    XHTMLConverter.getInstance().convert(document, out, options);
    out.close();
}

你可能感兴趣的:(java,工具类)