【poi】java操作office(二)

依赖

 <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
<!--        opencsv-->
        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.6</version>
        </dependency>

读写Word

一、写入word

1、关系:

word中的普通文本
一个文档多个段落,一个段落多个runs 一个runs多个run run是最小单位
getParagraphs() 获取所有段落
getRun() 获取一个段落中的所有片段
get() 一个片段中的一个run

word中的表格
getRows() 获取一个表格中所有行
getTableCells()  获取一行中所有列
getParagraphs() 获取一个格子里面的内容

2、设置页眉页脚:自定义策略 

CTSectPr sectPr = word.getDocument().getBody().addNewSectPr();
//自定义策略
XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(word, sectPr);
policy.createHeader()

页眉/页脚:CTP.Factory.newInstance() 

    public static void main(String[] args) throws IOException {
        //1.创建空白文档
        XWPFDocument word = new XWPFDocument();
        File file = new File("D:\\JavaProject\\test.docx");
        //2.在file中写
        FileOutputStream fos = new FileOutputStream(file);

        //1.添加标题
        XWPFParagraph title = word.createParagraph();
        //1.1设置格式标题居中
        title.setAlignment(ParagraphAlignment.CENTER);
        //1.2设置内容
        XWPFRun titleRun = title.createRun();
        titleRun.setText("标题1");
        titleRun.setColor("696969");
        titleRun.setFontSize(20);

        //2.段落
        XWPFParagraph content = word.createParagraph();
        XWPFRun contentRun = content.createRun();
        contentRun.setText("段落1");
        contentRun.setColor("696969");
        contentRun.setFontSize(16);

        //2.段落背景颜色
        CTShd ctShd = titleRun.getCTR().addNewRPr().addNewShd();
        ctShd.setVal(STShd.CLEAR);
        ctShd.setFill("97FFFF");

        //3.换行
        XWPFParagraph paragraph1 = word.createParagraph();
        XWPFRun run = paragraph1.createRun();
        run.setText("\r");

        //4.建表
        XWPFTable infoTable = word.createTable();
        infoTable.getCTTbl().getTblPr().unsetTblBorders();

        //5.列宽自动分割
        CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
        infoTableWidth.setType(STTblWidth.DXA);
        infoTableWidth.setW(BigInteger.valueOf(9072));

        //表格第一行
        XWPFTableRow row1 = infoTable.getRow(0);
        row1.getCell(0).setText("职位");
        row1.addNewTableCell().setText(":JAVA");

        XWPFTableRow row2 = infoTable.createRow();
        row2.getCell(0).setText("姓名");
        row2.getCell(1).setText("jane");

        //再次换行
        XWPFParagraph paragraph2 = word.createParagraph();
        XWPFRun run1 = paragraph2.createRun();
        run1.setText("\r");

        //新表格
        XWPFTable table2 = word.createTable();
        CTTblWidth ctTblWidth = table2.getCTTbl().addNewTblPr().addNewTblW();
        ctTblWidth.setType(STTblWidth.DXA);
        ctTblWidth.setW(BigInteger.valueOf(9072));

        //表格第一行
        XWPFTableRow table2row1 = table2.getRow(0);
        table2row1.getCell(0).setText("开始时间");
        table2row1.addNewTableCell().setText("结束时间");
        table2row1.addNewTableCell().setText("公司名称");
        table2row1.addNewTableCell().setText("职位");
        //表格第二行
        XWPFTableRow table2row2 = table2.createRow();
        table2row2.getCell(0).setText("2022-08-01");
        table2row2.getCell(1).setText("至今");
        table2row2.getCell(2).setText("家里蹲");
        table2row2.getCell(3).setText("JAVA");

        CTSectPr sectPr = word.getDocument().getBody().addNewSectPr();
        //自定义策略
        XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(word, sectPr);
        //页眉
        CTP ctp = CTP.Factory.newInstance();
        CTR ctrHeader = ctp.addNewR();
        CTText ctText = ctrHeader.addNewT();
        String headText="这个是页眉";
        ctText.setStringValue(headText);
        XWPFParagraph head = new XWPFParagraph(ctp, word);
        //右对齐
        head.setAlignment(ParagraphAlignment.RIGHT);
        XWPFParagraph[] parsHeader = new XWPFParagraph[1];
        parsHeader[0]=head;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT,parsHeader);

        //页脚
        CTP ctpFoot = CTP.Factory.newInstance();
        CTR ctrFoot = ctpFoot.addNewR();
        CTText ctFootText = ctrFoot.addNewT();
        String footText="这个是页脚";
        ctFootText.setStringValue(footText);
        XWPFParagraph foot = new XWPFParagraph(ctpFoot, word);
        //右对齐
        foot.setAlignment(ParagraphAlignment.CENTER);
        XWPFParagraph[] parsFoot = new XWPFParagraph[1];
        parsFoot[0]=foot;
        policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT,parsFoot);

        word.write(fos);
        fos.close();
        System.out.println("成功写完");
    }

3、表格:XWPFTableRow 表格行;XWPFTableCell 表格单元格;CTTcPr 单元格属性

4、合并单元格

将XWPFTableRow的getCell()赋值给XWPFTableCell,用

cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);设定开始
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);设定继续合并的格子

下面例子是指从第2个单元格到后面的所有单元格合并在一起 y=1restart 后面continue 合并

//表格行 
XWPFTableRow row;
//单元格
 XWPFTableCell cell;
if (y==1){
    cell=row.getCell(y);
    cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
    }else{
      cell=row.getCell(y);
      cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
         }

5、添加图片

FileInputStream fis = new FileInputStream(file);   

imageCellRun.addPicture(fis,XWPFDocument.PICTURE_TYPE_JPEG,file.getName(), Units.toEMU(100),Units.toEMU(100));

String imgPath="D:\\个人资料\\pic.jpeg";
for (int y=0;y<4;y++){
if(y==0){
cell=row.getCell(y);
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
//1.paras
List<XWPFParagraph> paragraphs = cell.getParagraphs();
//2.para
XWPFParagraph newPara = paragraphs.get(0);
//3.run
XWPFRun imageCellRun = newPara.createRun();
File file = new File(imgPath);
if (!file.exists()){
continue;}
FileInputStream fis = new FileInputStream(file);                      imageCellRun.addPicture(fis,XWPFDocument.PICTURE_TYPE_JPEG,file.getName(), Units.toEMU(100),Units.toEMU(100));
    }
}

二、读取word模板,把数据写入模板

复制表格行属性:

targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());

复制表格单元格属性:

targetCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());

复制单元格段落属性:

targetCell.getParagraphs().get(0).getCTP().setPPr(cell.getParagraphs().get(0).getCTP().getPPr());
  public static void main(String[] args) throws IOException {
        XWPFDocument word=new XWPFDocument(new FileInputStream(""));
        //1.读取数据
        //数据库查询模板中的用户的数据,然后放入Map中 再放入word中
        HashMap<String, String> map = new HashMap<String, String>();
        //2.读取word模板
        List<XWPFParagraph> paragraphs = word.getParagraphs();
        for (XWPFParagraph run :
                paragraphs) {
            List<XWPFRun> runs = run.getRuns();
            for (XWPFRun text :
                    runs) {
                String content=text.getText(0);
                //判断content是否包含key  (判断字段在word模板的位置,然后用数据库中的数据替换)
                for (String key : map.keySet()) {
                    if (content.contains(key)){
                        text.setText(content.replaceAll(key,map.get(key)),0);
                    }
                }
            }
        }

        //写入模板

        //写入表格
        //XWPFTable xwpfTable = word.getTables().get(0);
        //XWPFTableRow row = xwpfTable.getRow(0);
        //int rowIndex=1;
        //1.遍历要填写的东西
        //2.创建新的行
        //copyRow(xwpfTable,row,rowIndex);
        //XWPFTableRow row1 = xwpfTable.getRow(rowIndex);

    }
    //表格属性拷贝
    private static void copyRow(XWPFTable xwpfTable,XWPFTableRow sourceRow,int rowIndex){
        XWPFTableRow targetRow = xwpfTable.insertNewTableRow(rowIndex);
        //行属性复制 trpr table row property
        targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());
        //单元格属性复制
        List<XWPFTableCell> cells = sourceRow.getTableCells();
        if (CollectionUtils.isEmpty(cells)){
            return;
        }
        XWPFTableCell targetCell=null;
        for (XWPFTableCell cell:cells){
            targetCell = targetRow.addNewTableCell();
            //tcpr table cell property
            targetCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());
            //ppr paragraphs property
            targetCell.getParagraphs().get(0).getCTP().setPPr(cell.getParagraphs().get(0).getCTP().getPPr());
        }
    }

猜你喜欢

转载自blog.csdn.net/kanseu/article/details/124419101