java poi生成word 插入表格,图片,自动合并单元格,并且可以在已存在的word上追加

poi版本选3.10以上的 要不然插入图片 word会打不开 

		
		    org.apache.poi
		    poi
		    3.11
		
		
		
		    org.apache.poi
		    poi-ooxml
		    3.11
			
		    	
		    		stax-api
		    		stax
		    	
		    
		

 我下面代码中imgurl是base64编码,让后转的图片,你们使用的话,直接替换成图片路径地址就可以了

headList参数表示表格头部需要合并的集合,如果相邻左右或者上下位置的内容相同,就会自动合并单元格

        List> headList = new ArrayList<>();
        headList.add(Arrays.asList(new String[]{"1","2","2","3","3","4","4","5"}));
        headList.add(Arrays.asList(new String[]{"1","6","7","8","9","10","11","5"}));
        headList.add(Arrays.asList(new String[]{"1","12","13","14","15","16","17","5"}));

dataList参数表示表格下方的数据集合,会根据headList中第一行的长度进行自动换行,传入 dataList参数的长度只要是headList(0)的长度的倍数即可.

其余的一些参数是我项目中使用到的,你们看着删一删就行了.

需要追加功能的话,创建一个WordExport 对象,然后一直调它的makeWord方法即可,需要生成新的word的话,重新创建对象.

package com.runstone.rsflow.word;

import com.runstone.system.util.ConstantParam;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class WordExport {

    private XWPFDocument document;
    private FileOutputStream out;
    private File file;

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    /**
     *
     * @param title  pdf的标题
     * @param imgUrl 图片base64码
     * @param filePath  要保存word的路径  例:d:/
     * @param fileName  word的文件名   例:1.pdf
     * @param headList  表格头部的list数据
     * @param dataList  表格下方的内容数据   按顺序填入集合中即可
     * @param log
     */
    public void makeWord (List title, List imgUrl, String filePath, String fileName,
                         List> headList, List dataList, String note,File file, Logger log){

        fileName = fileName.replaceAll("/", "");
        fileName = fileName.replaceAll("\\\\","");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(new Date());
        filePath += format+File.separator;
        String imgName = filePath+ UUID.randomUUID().toString()+".png";
        File f = new File(filePath);
        if (!f.exists()){
            f.mkdirs();
        }
        OutputStream imgOut = null;
        try {
            if (file.exists()){
                //添加标题
                XWPFParagraph titleParagraph2 = document.createParagraph();
                //设置段落居中
                titleParagraph2.setAlignment(ParagraphAlignment.CENTER);
                XWPFRun titleParagraphRun2 = titleParagraph2.createRun();
                titleParagraphRun2.setText("  ");
                titleParagraphRun2.setFontSize(50);
            }

            /*对于每个new的OutputStream对象,只能被document.write()执行一次保存操作*/
            /*所以每次写入新的段落都要新建OutputStream,否则执行第二次会报错*/
            out = new FileOutputStream(file);

            for (int i=0;i0){
                //基本信息表格
                XWPFTable imgTable = document.createTable((int)Math.ceil(Double.parseDouble(String.valueOf(imgUrl.size()))/2),imgUrl.size()>1?2:1);
                imgTable.getCTTbl().getTblPr().unsetTblBorders();

                int width = imgUrl.size()>1?190:505;
                int height = imgUrl.size()>1?75:205;
                XWPFParagraph paragraph = document.createParagraph();
                int row = 0;
                int col = 0;

                for (int i=0;i> list,List dataList) throws Exception{
        int length = list.get(0).size();
        List>> aa = new ArrayList<>();

        for (int i=0;i strings = list.get(i);
            int colNum = 1;
            List> bb = new ArrayList<>();
            for (int j=0;j  map = new HashMap<>();
                        map.put("value",strings.get(j));
                        map.put("colspan",colNum+"");

                        bb.add(map);

                        for (int a=1;a map = new HashMap<>();
                    map.put("value",strings.get(j));
                    map.put("colspan",colNum+"");
                    bb.add(map);
                    for (int a=1;a>> mergeList = new ArrayList<>();
        for (int i=0;i> list1 = new ArrayList<>();
            for (int j=0;j map1 = aa.get(i).get(j);

                if (map1!=null&&map1.size()>=3){
                    int rowSpan = Integer.parseInt(map1.get("rowSpan"));
                    int colspan = Integer.parseInt(map1.get("colspan"));

                    if (colspan>1){

                        aa.get(i).get(j).put("isColStart","true");

                        for (int a=1;a map = new HashMap<>();
                            map.put("isColContinue","true");
                            aa.get(i).set(j+a,map);
                        }
                    }
                    if (rowSpan>1){

                        aa.get(i).get(j).put("isRowStart","true");

                        if (colspan>1){
                            for (int a=1;a map = new HashMap<>();
                                map.put("isRowContinue","true");
                                aa.get(i+a).set(j,map);

                                for (int b=1;b map2 = new HashMap<>();
                                    map2.put("isColContinue","true");
                                    map2.put("isRowContinue","true");
                                    aa.get(i+a).set(j+b,map2);
                                }
                            }
                        }else{
                            for (int a=1;a map = new HashMap<>();
                                map.put("isRowContinue","true");
                                aa.get(i+a).set(j,map);
                            }
                        }
                    }

                }
            }
            mergeList.add(list1);
        }

        //基本信息表格
        XWPFTable infoTable = document.createTable(dataList==null?list.size():list.size()+dataList.size()/list.get(0).size(),list.get(0).size());

        for (int i=0;i map = aa.get(i).get(j);

                if (map.get("isColStart")!=null){
                    infoTableRowOne.getCell(j).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);//这是起点
                }
                if (map.get("isRowStart")!=null){
                    infoTableRowOne.getCell(j).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);//这是起点
                }
                if (map.get("isColContinue")!=null){
                    infoTableRowOne.getCell(j).getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);//这是起点
                }
                if (map.get("isRowContinue")!=null){
                    infoTableRowOne.getCell(j).getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);//这是起点
                }
                if (map.get("value")!=null){
                    infoTableRowOne.getCell(j).setText(map.get("value"));
                }

                infoTableRowOne.getCell(j).getParagraphs().get(0).setAlignment(ParagraphAlignment.CENTER);  //设置水平居中

                infoTableRowOne.getCell(j).setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);  //设置垂直居中
            }
        }
        int row = list.size()-1;
        int col = 0;
        for (int i=0;i

 

你可能感兴趣的:(java poi生成word 插入表格,图片,自动合并单元格,并且可以在已存在的word上追加)