Android 使用 poi 笔记

word内标记  建议用txt  写完复制进去,不要直接在word内写

使用的docx格式对应  XWPFDocument

XWPFDocument--文档
XWPFParagraph-段落
XWPFRun-文本对象(不同样式:加粗、颜色、等)每种格式一个XWPFRun
XWPFTable-表格     XWPFTableRow-表格行    XWPFTableCell-表格-单元格

InputStream inputStream = mContext.getAssets().open(name);
XWPFDocument xwpfDocument = new XWPFDocument(inputStream);

表格复制

//可快速复制表格,但内容不可替换
xwpfTable.getCTTbl().set(table.getCTTbl());
//xwpfTable.getCTTbl().set(table.getCTTbl().copy());

第二种逐层手动复制(表格创建时默认一行一个单元格-不删除,新建行时只需要统一移除index = 0 的单元格,表格新行创建时默认包含上一行的单元格数量)

XWPFTable -> XWPFTableRow -> XWPFTableCell -> XWPFParagraph -> XWPFRun
xwpfTableRow.getCtRow().setTrPr(row.getCtRow().getTrPr());

for-->
//remove顺序不可乱
xwpfTableRow.removeCell(0);
xwpfTableRow.getCtRow().removeTc(0);

for -->
XWPFTableCell xwpfTableCell = xwpfTableRow.addNewTableCell();
xwpfTableCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());

for....
xwpfRun

插入表格

xwpfTable = XWPFDocument.insertNewTbl(xmlCursor);//插入空白表格,默认一个单元格
private XmlCursor getXmlCursor(String mark){
for (int i = 0; i < documentOut.getParagraphs().size(); i++) {
//不使用正则
if (document.getParagraphs().get(i).getText().equals(mark)){
XmlCursor cursorTb = documentOut.getParagraphs().get(i).getCTP().newCursor();
                return cursorTb;
}
}
return null;
}

插入行和表格复制行差不多。

标记内容替换

不使用正则,直接用.contains判断,replace替换,最后xwpfRun.setText(text,0);设置文本内容
StringBuffer stringBuffer = new StringBuffer();
Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
matcher.appendReplacement(stringBuffer,value);
xwpfRun.setText(matcher.appendTail(stringBuffer).toString(),0);

段落替换

遍历document.getParagraphs()

表格内标记内容替换:手动遍历

XWPFTable -> XWPFTableRow -> XWPFTableCell -> XWPFParagraph -> XWPFRun  (替换XWPFRun 的文本)

插入图片

XWPFRun.addPicture(InputStream pictureData, int pictureType, String filename, int width, int height)
public static int getImageFormat(String imgFile){
        int format = Document.PICTURE_TYPE_JPEG;//默认jpg
        if (imgFile.endsWith(".emf")) {
            format = Document.PICTURE_TYPE_EMF;
        } else if (imgFile.endsWith(".wmf")) {
            format = Document.PICTURE_TYPE_WMF;
        } else if (imgFile.endsWith(".pict")) {
            format = Document.PICTURE_TYPE_PICT;
        } else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg")) {
            format = Document.PICTURE_TYPE_JPEG;
        } else if (imgFile.endsWith(".png")) {
            format = Document.PICTURE_TYPE_PNG;
        } else if (imgFile.endsWith(".dib")) {
            format = Document.PICTURE_TYPE_DIB;
        } else if (imgFile.endsWith(".gif")) {
            format = Document.PICTURE_TYPE_GIF;
        } else if (imgFile.endsWith(".tiff")) {
            format = Document.PICTURE_TYPE_TIFF;
        } else if (imgFile.endsWith(".eps")) {
            format = Document.PICTURE_TYPE_EPS;
        } else if (imgFile.endsWith(".bmp")) {
            format = Document.PICTURE_TYPE_BMP;
        } else if (imgFile.endsWith(".wpg")) {
            format = Document.PICTURE_TYPE_WPG;
        } else {
            System.err.println("Unsupported picture: " + imgFile +
                    ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
        }
        return format;
    }

正则  例:${NAME}

matcher.group(1)获取正则中间值
matcher.group("scope")需要sdk>O
   Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(str);
for (int i = 0; i < documentOut.getParagraphs().size(); i++) {
            String str = documentOut.getParagraphs().get(i).getText();
            if (TextUtils.isEmpty(str)){
                continue;
            }
            Matcher matcher = getMatcher(str);
            while (matcher.find()){
                if (mark.equals(matcher.group(1))){
                    XmlCursor cursorTb;
                    cursorTb = documentOut.getParagraphs().get(i).getCTP().newCursor();
                }
            }
        }

posted @ 2022-07-18 20:44  西瓜皮不甜  阅读(545)  评论(0编辑  收藏  举报