目录

1 Maven依赖

2 copy()(第一种方法)

2.1 调试代码

2.2 调试结果

3 手动复制行(第二种方法推荐)

3.1 调试代码

3.2 调试结果

注:


1 Maven依赖

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.17</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml-schemas</artifactId><version>3.17</version></dependency>

2 copy()(第一种方法)

2.1 调试代码

/*** 复制行*/@GetMapping("/copyRow1")public void copyRow1(HttpServletResponse response) {String templateFileName = "D:/test/模板.docx";try {InputStream inputStream = new FileInputStream(templateFileName);XWPFDocument document = new XWPFDocument(inputStream);XWPFTable table = document.getTableArray(0);XWPFTableRow twoRow = table.getRow(1);//将第二行复制到第三行XmlObject copy = twoRow.getCtRow().copy();table.addRow(new XWPFTableRow((CTRow) copy, table));twoRow.getCell(0).setText("第二行");//获取复制的第三行XWPFTableRow threeRow = table.getRow(2);threeRow.getCell(0).setText("第三行");//保存文件String fileName="模板.docx";response.setHeader("content-type", "application/octet-stream");response.setContentType("application/octet-stream;charset=UTF-8");response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"),"ISO-8859-1"));OutputStream outputStream=response.getOutputStream();document.write(outputStream);outputStream.flush();outputStream.close();} catch (Exception e) {e.printStackTrace();}}

2.2 调试结果

3 手动复制行(第二种方法推荐)

3.1 调试代码

    /*** 复制行*/@GetMapping("/copyRow2")public void copyRow2(HttpServletResponse response) {String templateFileName = "D:/test/模板.docx";try {InputStream inputStream = new FileInputStream(templateFileName);XWPFDocument document = new XWPFDocument(inputStream);XWPFTable table = document.getTableArray(0);XWPFTableRow twoRow = table.getRow(1);twoRow.getCell(0).setText("第二行");//创建第三行XWPFTableRow threeRow = table.insertNewTableRow(2);//将第二行复制到第三行createCellsAndCopyStyles(threeRow, twoRow);threeRow.getCell(0).setText("第三行");//保存文件String fileName = "模板.docx";response.setHeader("content-type", "application/octet-stream");response.setContentType("application/octet-stream;charset=UTF-8");response.setHeader("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("utf-8"), "ISO-8859-1"));OutputStream outputStream = response.getOutputStream();document.write(outputStream);outputStream.flush();outputStream.close();} catch (Exception e) {e.printStackTrace();}}/*** 复制单元格和样式** @param targetRow 要复制的行* @param sourceRow 被复制的行*/public static void createCellsAndCopyStyles(XWPFTableRow targetRow, XWPFTableRow sourceRow) {targetRow.getCtRow().setTrPr(sourceRow.getCtRow().getTrPr());List<XWPFTableCell> tableCells = sourceRow.getTableCells();if (CollectionUtils.isEmpty(tableCells)) {return;}for (XWPFTableCell sourceCell : tableCells) {XWPFTableCell newCell = targetRow.addNewTableCell();newCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr());List sourceParagraphs = sourceCell.getParagraphs();if (CollUtil.isEmpty(sourceParagraphs)) {continue;}XWPFParagraph sourceParagraph = (XWPFParagraph) sourceParagraphs.get(0);List targetParagraphs = newCell.getParagraphs();XWPFParagraph targetParagraph = CollUtil.isEmpty(targetParagraphs) ? newCell.addParagraph() : (XWPFParagraph) targetParagraphs.get(0);targetParagraph.getCTP().setPPr(sourceParagraph.getCTP().getPPr());XWPFRun targetRun = targetParagraph.getRuns().isEmpty()? targetParagraph.createRun() : targetParagraph.getRuns().get(0);List<XWPFRun> sourceRunList=sourceParagraph.getRuns();if (CollUtil.isNotEmpty(sourceRunList)) {XWPFRun sourceRun=sourceRunList.get(0);//字体名称targetRun.setFontFamily(sourceRun.getFontFamily());//字体大小targetRun.setFontSize(sourceRun.getFontSize());//字体颜色targetRun.setColor(sourceRun.getColor());//字体加粗targetRun.setBold(sourceRun.isBold());//字体倾斜targetRun.setItalic(sourceRun.isItalic());}}}

3.2 调试结果

注:

(1)第一种方法使用copy()复制的单元格无法写入内容,推荐使用第二种方法手动复制行。

(2)源码请查看Gitee。

xudongbase: 主要是项目中可以用到的共通方法https://gitee.com/xudong_master/xudongbase

POI Word表格复制行2种方式(copy()、手动复制行)相关推荐

  1. 表格拆分的两种方式 拆分成多个excel工作表或多个excel文件

    表格拆分的两种方式 拆分成多个excel工作表或多个excel文件 拆分Excel,可以分为3种层次:拆分excel单元格:拆分成多个excel工作表:拆分成多个excel文件 其中,第1种拆分是无法 ...

  2. 复制百度文库的文字加什么后缀_10种方式教你复制网页中不能复制的文字

    10 种方式教你复制网页中不能复制的文字 很多 seoer 写软文都会复制网页中的文字,但是很多网页中的文字都 不能复制,于是综合自己平时遇到的问题,收集了 10 种方式,仅供 大家参考,有的我自己没 ...

  3. MySQL 8.0 异步复制的三种方式

    本实验中分别针对空库.脱机.联机三种方式,配置一主两从的mysql标准异步复制.只做整服务器级别的复制,不考虑对个别库表或使用过滤复制的情况. 实验环境 [root@slave2 ~]# cat /e ...

  4. Java实现文件复制的四种方式

    背景:有很多的Java初学者对于文件复制的操作总是搞不懂,下面我将用4中方式实现指定文件的复制. 实现方式一:使用FileInputStream/FileOutputStream字节流进行文件的复制操 ...

  5. java直接调用复制文件,java中文件复制的4种方式,java文件的复制

    java中文件复制的4种方式,java文件的复制 今天一个同事问我文件复制的问题,他一个100M的文件复制的指定目录下竟然成了1G多,吓我一跳,后来看了他的代码发现是自己通过字节流复制的,定义的字节数 ...

  6. layui根据条件显示列_templet渲染layui表格数据的三种方式

    layui前端框架是我一直在使用,也很好用. 今天记录一下,templet渲染layui表格数据的三种方式. 第一种:直接渲染(对于表格数据样式要求不高) 直接在动态表格字段声明,添加templet属 ...

  7. java 复制文本内容_基于java文本复制的7种方式总结

    如下所示: package copy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...

  8. layui 表格内容写temple函数_templet渲染layui表格数据的三种方式

    layui前端框架是我一直在使用,也很好用. 今天记录一下,templet渲染layui表格数据的三种方式. 第一种:直接渲染(对于表格数据样式要求不高) 直接在动态表格字段声明,添加templet属 ...

  9. apache poi word表格样式(表格固定间隔,表格字体颜色)、段落样式(段落字体颜色,格式)

    Apache POI [1] 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程序对Microsoft Office格式档案读和写的功能.POI为&qu ...

  10. 6.1_6 Python3.x入门 P7 【字符串格式化】四种方式(手动、%-formatting、str.format()、f-String)

    相关链接 目录 Mac M1 Python环境搭建 Python3.x入门 P1 [基础]基础语法.注释.标识符.变量.数据类型.键盘录入input Python3.x入门 P2 [基础]运算符 Py ...

最新文章

  1. 短信验证码的登录流程
  2. java.lang.RuntimeException: setParameters failed
  3. Java黑皮书课后题第10章:*10.10(Queue类)10.6节给出一个Stock类。设计一个名为Queue的类用于存储整数。像栈一样,队列保存元素。在栈中,元素后进先出。队列中元素先进先出
  4. ASP.NET登录以及注册
  5. GDB调试教程:1小时玩转Linux gdb命令
  6. 计算机a类论文汇报,计算机学院2014年度发表和录用CCFA类、B类论文统计(初稿.xls...
  7. linux查看svn信息,SVN 查看历史信息
  8. centos升级内核之后修改内核启动顺序
  9. Java面试题总结系列 Servlet
  10. checkbox属性checked=checked已有,但却不显示打勾的解决办法
  11. K3救砖,梅林刷回官方
  12. matlab做简单的信号处理分析
  13. 新版标准日本语中级_第六课
  14. LintCode 1144.范围加法之二
  15. EOS Royale游戏遭受黑客攻击,黑客目前共获利18000eos
  16. 朴素贝叶斯算法原理以及python实现
  17. MIT 6.824涉及的部分论文翻译
  18. <数据结构>倒拔二叉树
  19. Java操作ffmpeg为视频添加音乐
  20. Spring文件上传和连接重置问题

热门文章

  1. 数据库操作:更新数据update
  2. 华为手机android是什么意思,华为手机里的文件夹表示什么意思?
  3. ButterWorth滤波器学习(参照博主链接——https://blog.csdn.net/cjsh_123456/article/details/79342300)
  4. csdn网友提出关于expdp exclude及impdp问题解答
  5. 滑动窗口(java)
  6. XTU OJ 1352 Fraction
  7. 前端需要了解的色彩知识
  8. 15个提高编程技巧的JavaScript工具
  9. Stata:工具变量法(两阶段最小二乘法2SLS)——解决模型内生性
  10. http://blog.csdn.net/lwj103862095/article/details/7860648