1.引入word导出所需依赖包

工作常用的excel,word导出,需要引入下面的6个主要包,和主要包依赖的其他包 ,可以看下面的表格进行依赖下载引入

这下面的两张图是主要包对应涉及到功能,可以按需要进行引入,有些真的用不到的主要包可以不引入

2.引入工具类

1.util类

package com.management.util;import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;/*** Created by wfg 2020-08-12*/
public class ExportWordUtil {/*** 根据模板生成word文档* @param inputUrl 模板路径* @param textMap 需要替换的文本内容* @param mapList 需要动态生成的内容* @return*/public static CustomXWPFDocument changWord(String inputUrl, Map<String, Object> textMap, List<Object> mapList,int[] placeList) {CustomXWPFDocument document = null;try {//获取docx解析对象document = new CustomXWPFDocument(POIXMLDocument.openPackage(inputUrl));//解析替换文本段落对象ExportWordUtil.changeText(document, textMap);//解析替换表格对象ExportWordUtil.changeTable(document, textMap, mapList,placeList);} catch (IOException e) {e.printStackTrace();}return document;}/*** 替换段落文本* @param document docx解析对象* @param textMap 需要替换的信息集合*/public static void changeText(CustomXWPFDocument document, Map<String, Object> textMap){//获取段落集合List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {//判断此段落时候需要进行替换String text = paragraph.getText();if(checkText(text)){List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {//替换模板原来位置Object ob = changeValue(run.toString(), textMap);System.out.println("段落:"+run.toString());if (ob instanceof String){run.setText((String)ob,0);}}}}}/*** 替换表格对象方法* @param document docx解析对象* @param textMap 需要替换的信息集合* @param mapList 需要动态生成的内容*/public static void changeTable(CustomXWPFDocument document, Map<String, Object> textMap, List<Object> mapList,int[] placeList){//获取表格对象集合List<XWPFTable> tables = document.getTables();//循环所有需要进行替换的文本,进行替换for (int i = 0; i < tables.size(); i++) {System.out.println("1111");XWPFTable table = tables.get(i);if(checkText(table.getText())){System.out.println("222");List<XWPFTableRow> rows = table.getRows();System.out.println("简单表格替换:"+rows);//遍历表格,并替换模板eachTable(document,rows, textMap);}}int index=0;//操作word中的表格for (int i = 0; i < tables.size(); i++) {//只处理行数大于等于2的表格,且不循环表头XWPFTable table = tables.get(i);if(placeList[index]==i){List<String[]> list = (List<String[]>) mapList.get(index);//第二个表格使用daList,插入数据if (null != list && 0 < list.size()){insertTable(table, null,list,2);List<Integer[]> indexList = startEnd(list);for (int c=0;c<indexList.size();c++){//合并行mergeCellVertically(table,0,indexList.get(c)[0]+1,indexList.get(c)[1]+1);}}index++;}}}/*** 遍历表格* @param rows 表格行对象* @param textMap 需要替换的信息集合*/public static void eachTable(CustomXWPFDocument document, List<XWPFTableRow> rows , Map<String, Object> textMap){for (XWPFTableRow row : rows) {List<XWPFTableCell> cells = row.getTableCells();for (XWPFTableCell cell : cells) {//判断单元格是否需要替换if(checkText(cell.getText())){List<XWPFParagraph> paragraphs = cell.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {Object ob = changeValue(run.toString(), textMap);if (ob instanceof String){run.setText((String)ob,0);}else if (ob instanceof Map){run.setText("",0);Map pic = (Map)ob;int width = Integer.parseInt(pic.get("width").toString());int height = Integer.parseInt(pic.get("height").toString());int picType = getPictureType(pic.get("type").toString());byte[] byteArray = (byte[]) pic.get("content");ByteArrayInputStream byteInputStream = new ByteArrayInputStream(byteArray);try {//String ind = document.addPictureData(byteInputStream,picType);//document.createPicture(ind, width , height,paragraph);String ind = document.addPictureData(byteInputStream, picType);int id = document.getNextPicNameNumber(picType);document.createPicture(ind, id, width, height, paragraph);} catch (Exception e) {e.printStackTrace();}}}}}}}}/*** 为表格插入数据,行数不够添加新行* @param table 需要插入数据的表格* @param tableList 第四个表格的插入数据* @param daList 第二个表格的插入数据* @param type 表格类型:1-第一个表格 2-第二个表格 3-第三个表格 4-第四个表格*/public static void insertTable(XWPFTable table, List<String> tableList,List<String[]> daList,Integer type){if (2 == type){//创建行和创建需要的列for(int i = 1; i < daList.size(); i++){//添加一个新行XWPFTableRow row = table.insertNewTableRow(1);/*System.out.println("333");System.out.println(table.getRow(1).getHeight());*/for(int k=0; k<daList.get(0).length;k++){row.createCell();//根据String数组第一条数据的长度动态创建列}row.setHeight(400);//设置行高度}//创建行,根据需要插入的数据添加新行,不处理表头for(int i = 0; i < daList.size(); i++){table.getRow(i+1).setHeight(400);//设置行高度List<XWPFTableCell> cells = table.getRow(i+1).getTableCells();for(int j = 0; j < cells.size(); j++){XWPFTableCell cell02 = cells.get(j);cell02.setText(daList.get(i)[j]);/** 设置水平居中 */CTTc cttc = cell02.getCTTc();CTTcPr ctPr = cttc.addNewTcPr();ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);}}}else if (4 == type){//插入表头下面第一行的数据for(int i = 0; i < tableList.size(); i++){XWPFTableRow row = table.createRow();row.setHeight(400);//设置行高度List<XWPFTableCell> cells = row.getTableCells();cells.get(0).setText(tableList.get(i));}}}/*** 判断文本中时候包含$* @param text 文本* @return 包含返回true,不包含返回false*/public static boolean checkText(String text){boolean check  =  false;if(text.indexOf("$")!= -1){check = true;}return check;}/*** 匹配传入信息集合与模板* @param value 模板需要替换的区域* @param textMap 传入信息集合* @return 模板需要替换区域信息集合对应值*/public static Object changeValue(String value, Map<String, Object> textMap){Set<Map.Entry<String, Object>> textSets = textMap.entrySet();Object valu = "";for (Map.Entry<String, Object> textSet : textSets) {//匹配模板与替换值 格式${key}String key = textSet.getKey();if(value.indexOf(key)!= -1){valu = textSet.getValue();}}return valu;}/*** 将输入流中的数据写入字节数组* @param in* @return*/public static byte[] inputStream2ByteArray(InputStream in, boolean isClose){byte[] byteArray = null;try {int total = in.available();byteArray = new byte[total];in.read(byteArray);} catch (IOException e) {e.printStackTrace();}finally{if(isClose){try {in.close();} catch (Exception e2) {System.out.println("关闭流失败");}}}return byteArray;}/*** 根据图片类型,取得对应的图片类型代码* @param picType* @return int*/private static int getPictureType(String picType){int res = CustomXWPFDocument.PICTURE_TYPE_PICT;if(picType != null){if(picType.equalsIgnoreCase("png")){res = CustomXWPFDocument.PICTURE_TYPE_PNG;}else if(picType.equalsIgnoreCase("dib")){res = CustomXWPFDocument.PICTURE_TYPE_DIB;}else if(picType.equalsIgnoreCase("emf")){res = CustomXWPFDocument.PICTURE_TYPE_EMF;}else if(picType.equalsIgnoreCase("jpg") || picType.equalsIgnoreCase("jpeg")){res = CustomXWPFDocument.PICTURE_TYPE_JPEG;}else if(picType.equalsIgnoreCase("wmf")){res = CustomXWPFDocument.PICTURE_TYPE_WMF;}}return res;}/*** 合并行* @param table* @param col 需要合并的列* @param fromRow 开始行* @param toRow 结束行*/public static void mergeCellVertically(XWPFTable table, int col, int fromRow, int toRow) {for(int rowIndex = fromRow; rowIndex <= toRow; rowIndex++){CTVMerge vmerge = CTVMerge.Factory.newInstance();if(rowIndex == fromRow){vmerge.setVal(STMerge.RESTART);} else {vmerge.setVal(STMerge.CONTINUE);}XWPFTableCell cell = table.getRow(rowIndex).getCell(col);CTTcPr tcPr = cell.getCTTc().getTcPr();if (tcPr != null) {tcPr.setVMerge(vmerge);//cell.getCTTc().setTcPr(tcPr);} else {tcPr = CTTcPr.Factory.newInstance();tcPr.setVMerge(vmerge);cell.getCTTc().setTcPr(tcPr);}}}/*** 获取需要合并单元格的下标* @return*/public static List<Integer[]> startEnd(List<String[]> daList){List<Integer[]> indexList = new ArrayList<Integer[]>();List<String> list = new ArrayList<String>();for (int i=0;i<daList.size();i++){list.add(daList.get(i)[0]);}Map<Object, Integer> tm = new HashMap<Object, Integer>();for (int i=0;i<daList.size();i++){if (!tm.containsKey(daList.get(i)[0])) {tm.put(daList.get(i)[0], 1);} else {int count = tm.get(daList.get(i)[0]) + 1;tm.put(daList.get(i)[0], count);}}for (Map.Entry<Object, Integer> entry : tm.entrySet()) {String key = entry.getKey().toString();String value = entry.getValue().toString();if (list.indexOf(key) != (-1)){Integer[] index = new Integer[2];index[0] = list.indexOf(key);index[1] = list.lastIndexOf(key);indexList.add(index);}}return indexList;}}

2.自定义XWPFDocument类

package com.management.util;import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlToken;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;import java.io.IOException;
import java.io.InputStream;/*** Created by wfg 2020-08-12.* 自定义 XWPFDocument,并重写 createPicture()的方法*/
public class CustomXWPFDocument extends XWPFDocument{public CustomXWPFDocument(InputStream in) throws IOException {super(in);}public CustomXWPFDocument() {super();}public CustomXWPFDocument(OPCPackage pkg) throws IOException {super(pkg);}/*** @param id* @param width 宽* @param height 高* @param paragraph  段落*/public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {final int EMU = 9525;width *= EMU;height *= EMU;CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();String picXml = "" +"<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +"   <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +"      <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +"         <pic:nvPicPr>" +"            <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +"            <pic:cNvPicPr/>" +"         </pic:nvPicPr>" +"         <pic:blipFill>" +"            <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +"            <a:stretch>" +"               <a:fillRect/>" +"            </a:stretch>" +"         </pic:blipFill>" +"         <pic:spPr>" +"            <a:xfrm>" +"               <a:off x=\"0\" y=\"0\"/>" +"               <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +"            </a:xfrm>" +"            <a:prstGeom prst=\"rect\">" +"               <a:avLst/>" +"            </a:prstGeom>" +"         </pic:spPr>" +"      </pic:pic>" +"   </a:graphicData>" +"</a:graphic>";inline.addNewGraphic().addNewGraphicData();XmlToken xmlToken = null;try {xmlToken = XmlToken.Factory.parse(picXml);} catch (XmlException xe) {xe.printStackTrace();}inline.set(xmlToken);CTPositiveSize2D extent = inline.addNewExtent();extent.setCx(width);extent.setCy(height);CTNonVisualDrawingProps docPr = inline.addNewDocPr();docPr.setId(id);docPr.setName("图片" + id);docPr.setDescr("头像");}
}

3.创建测试类

注意使用的word模板,要有一个表头和一行空数据,要不运行会报错空指针,找不到有效行,运行下面的类后,即可看到效果

package com.management.web;import com.management.util.CustomXWPFDocument;
import com.management.util.ExportWordUtil;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class TestWord {public static void main(String[] args) throws Exception {//需要进行文本替换的信息Map<String, Object> data = new HashMap<String, Object>();data.put("${date}", "(2020年08月12日)");data.put("${name}", "测试一下");//图片,如果是多个图片,就新建多个map/* Map<String,Object> picture1 = new HashMap<String, Object>();picture1.put("width", 100);picture1.put("height", 150);picture1.put("type", "jpg");picture1.put("content", ExportWordUtil.inputStream2ByteArray(new FileInputStream("D:/timg.jpg"), true));data.put("${picture1}",picture1);
*///需要进行动态生成的信息List<Object> mapList = new ArrayList<Object>();//第一个动态生成的数据列表List<String[]> list01 = new ArrayList<String[]>();list01.add(new String[]{"A","11111111111","22","22","22","22","22"});list01.add(new String[]{"A","22222222222","33","22","22","22","22"});list01.add(new String[]{"B","33333333333","44","22","22","22","22"});list01.add(new String[]{"B","44444444444","55","22","22","22","22"});list01.add(new String[]{"B","33333333333","44","22","22","22","22"});list01.add(new String[]{"C","44444444444","55","22","22","22","22"});list01.add(new String[]{"C","33333333333","44","22","22","22","22"});list01.add(new String[]{"C","44444444444","55","22","22","22","22"});list01.add(new String[]{"C","44444444444","55","22","22","22","22"});//第二个动态生成的数据列表List<String[]> list02 = new ArrayList<String[]>();list02.add(new String[]{"A","11111111111","22","22","22","22","22"});list02.add(new String[]{"d","22222222222","33","22","22","22","22"});list02.add(new String[]{"B","33333333333","44","22","22","22","22"});list02.add(new String[]{"C","44444444444","55","22","22","22","22"});mapList.add(list01);//mapList.add(list02);//需要动态改变表格的位置;第一个表格的位置为0,1int[] placeList = {0,1};CustomXWPFDocument doc = ExportWordUtil.changWord("D:/word.docx",data,mapList,placeList);FileOutputStream fopts = new FileOutputStream("D:/合并表格.docx");doc.write(fopts);fopts.close();}}

java poi 动态导出word表格带合并效果相关推荐

  1. java poi生成的word表格在wps中的显示问题

    java poi生成的word表格在wps中的显示问题 问题描述: poi导出的word文档使用office打开效果正常: poi导出的word文档使用wps打开效果异常: 问题分析: 1.怀疑是合并 ...

  2. java导出word表格 行列合并

    生成word表格 新建模板如下(使用offic,不要用wps) 另存为xm格式 将文件放入resource/template目录下,复制一份,将后缀名改为ftl,也可以不复制直接改后缀名(这里忽视我其 ...

  3. Java中利用freemarker导出word表格并合并单元格

    1.word表格的模板 另存为xml格式: 将保存的xml改成.ftl 格式化一下xml,看看文件中的带有${}是否正确 如果出现这种情况,手动修改下(可复制上一个正确的改下名字) 2.Java代码 ...

  4. Java使用XDocReport导出Word(带图片)

    XDocReport导出Word 简介 示例 实现方式 Maven依赖 Word模板制作 Word模板文字(四步) Word模板图片(五步) 具体实现 注意事项 结论 简介 这次的内容是关于Java实 ...

  5. java读取word文档的复杂表格_poi读取word表格 java POI 如何读取word的表格中的表格...

    poi 操作word 2007 (如何删除word中的某一个表格)小编忘了哪年哪月的哪日小编在哪面墙上刻下张脸张微笑着忧伤着凝望小编的脸. public static void changeTable ...

  6. poi导出word文件(带表格)

    poi导出word文件(带表格) 一.背景介绍 现有业务需求根据前端页面上所选的时间和列,来生成word表格,方便打印. 二.POM <dependency><groupId> ...

  7. Java导出word表格|poi-tl

    目标 多级表头.分页.动态数据 实现 依赖: <!-- poi工具类--><dependency><groupId>com.deepoove</groupId ...

  8. POI导出word表格 office打开没问题 wps打开列有问题

    POI导出word表格 office打开没问题 wps打开列有问题 模板样式 导出文档office打开 wps打开文件 1.给表格设置宽,指定宽度 2.将布局固定 3.动态设置单元格的宽度 4.重点设 ...

  9. poi设置word表格单元格宽度_poi导出word表格的操作讲解

    一.效果如下 二.js代码 function export_word(){ //导出word var url = "czzsca/exportWord.do"; this.expo ...

最新文章

  1. AI新作品:照片迅速被画成艺术画
  2. Navisworks API 简单二次开发 (自定义工具条)
  3. python字符串类型str_python数据类型之字符串类型str
  4. 电脑格式化后需要重装系统吗_电脑经常重装系统对电脑有影响吗
  5. C++知识总结(2)--字符串和数组
  6. iperf测试带宽linux,iperf3-网络带宽性能测试工具
  7. Nosql进阶笔记之redis MongoDB
  8. MATLAB文件操作及读txt文件(fopen,fseek,fread,fclose)
  9. 1.Entity Framework Core 5.0教程,概述和准备
  10. C语言库函数——string.h
  11. HNOI 2009 图的同构记数 题解
  12. 解决因卸载WPS导致Excel表格打开提示“xls文件格式和扩展名不匹配,文件可能已损坏或不安全”
  13. 认识JNCIE认证(学习方法经验总结 )
  14. 圣笛数控联手国促会数外委打造OID物联新高地
  15. 尚学堂1811期python视频_尚学堂1811期Java+架构全套视频教程 下载
  16. 算法(Java)——双指针
  17. contiki学习笔记(二)CTK图形界面
  18. 区块链中节点和区块的关系区块链的基本概念
  19. 输出三位数的个十百位数
  20. 买二手房和买新房-----这几年的是没差别的

热门文章

  1. allegro制作通孔焊盘封装-flash热风焊盘-图文并茂的Allegro 通孔焊盘制作教程
  2. BIOS和UEFI区别,以及Boot Loader【上】
  3. Selenium Python2.7
  4. 【APP本地化】提高APP下载量的 5 个技巧
  5. Vim统计中文字数和英文单词数(附针对LaTeX的特殊处理)
  6. V语言(Vlang)初探
  7. 已知信码序列为1011_某一个数据通信系统采用CRC校验方式,其中:生成多项式G(X)=...
  8. 【解题】核电站问题(SGOI)
  9. 【C++登山之路之语法高山 】—— 命名空间+缺省参数+函数重载(万字详解,图片演示,结构原理)
  10. 为视图或函数指定的列名比其定义中的列多。