当前位置: 首页 > news >正文

Java使用apache.poi生成excel插入word中

加油,新时代打工人!

工作需求,上个文章我们生成好的word,这次将生成好的excel表格数据,插入word中。需要准备好excle数据,然后插入到word中。
最后个需要,就是把这些生成好的word文档转成pdf进行前端下载下来。
Java使用apache.poi生成word

在这里插入图片描述

package com.wh.filedownload.controller;import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;/*** @author wh* @date 2024年05月22日16:12* 将excel表格数据,导入现有的word模板中.*/
public class word2 {public static void main(String[] args) throws IOException, InvalidFormatException {String excelFilePath = "sample.xlsx";String wordFilePath = "output.docx";String tempWordFilePath = "tempWordFile.docx";FileInputStream excelFileInputStream = new FileInputStream(new File(excelFilePath));Workbook workbook = WorkbookFactory.create(excelFileInputStream);Sheet sheet = workbook.getSheetAt(0); // 假设只处理第一个sheetFileInputStream wordFileInputStream = new FileInputStream(new File(wordFilePath));XWPFDocument document = new XWPFDocument(wordFileInputStream);int rowCount = sheet.getLastRowNum();int colCount = sheet.getRow(0).getLastCellNum(); // 假设第一行有最多的列// 创建Word中的表格XWPFTable table = document.createTable(rowCount + 1, colCount); // +1行是为了标题// 获取或创建表格的边框设置对象CTTblBorders tblBorders = table.getCTTbl().getTblPr().getTblBorders();// 设置表格边框样式tblBorders.getTop().setVal(STBorder.SINGLE); // 上边框tblBorders.getLeft().setVal(STBorder.SINGLE); // 左边框tblBorders.getRight().setVal(STBorder.SINGLE); // 右边框tblBorders.getBottom().setVal(STBorder.SINGLE); // 下边框// 填充表格标题Row headerRow = sheet.getRow(0);for (int i = 0; i < colCount; i++) {XWPFTableCell cell = table.getRow(0).getCell(i);cell.setText(headerRow.getCell(i).getStringCellValue());}// 填充表格数据for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++) { // 从1开始,跳过标题行Row dataRow = sheet.getRow(rowIndex);if (dataRow == null) continue; // 跳过空行XWPFTableRow tableRow = table.getRow(rowIndex); // 对应的Word表格行for (int colIndex = 0; colIndex < colCount; colIndex++) {Cell cell = dataRow.getCell(colIndex);if (cell == null) continue; // 如果单元格为空,则跳过switch (cell.getCellType()) {case STRING:tableRow.getCell(colIndex).setText(cell.getStringCellValue());break;case NUMERIC:if (DateUtil.isCellDateFormatted(cell)) {tableRow.getCell(colIndex).setText(cell.getLocalDateTimeCellValue().toString());} else {tableRow.getCell(colIndex).setText(String.valueOf(cell.getNumericCellValue()));}break;// 可以继续添加对其他类型的处理}}}wordFileInputStream.close();FileOutputStream out = new FileOutputStream(tempWordFilePath);document.write(out);out.close();document.close();workbook.close();excelFileInputStream.close();// 这里可以选择将tempWordFilePath移动或重命名为wordFilePath以覆盖原文件// 注意:这一步骤会永久改变原Word文档File srcFile = new File(tempWordFilePath);File destFile = new File(wordFilePath);srcFile.renameTo(destFile);System.out.println("Excel content has been appended to the Word document.");}}

运行结果。
在这里插入图片描述

http://www.lryc.cn/news/356856.html

相关文章:

  • 斯坦福报告解读3:图解有趣的评估基准(上)
  • C语言---扫雷游戏的实现
  • 《征服数据结构》块状链表
  • leetCode.86. 分隔链表
  • Java进阶学习笔记5——Static应用知识:单例设计模式
  • Vue 前端加框 给div加红色框框 js实现
  • Percona Toolkit 神器全攻略(实用类)
  • ARM GIC 和NVIC的区别
  • CSS文本粒子动画特效之爱心粒子文字特效-Canvas
  • 小熊家务帮day5 客户管理模块1 (小程序认证,手机验证码认证等)
  • Blender 学习笔记(一)快捷键记录
  • ubuntu linux (20.04) 源码编译cryptopp库 - apt版本过旧
  • 机器学习-3-特征工程的重要性及常用特征选择方法
  • QGis3.34.5工具软件保存样式,软件无反应问题
  • JavaScript(ES6)入门
  • 深入分析 Android Activity (十)
  • 考试“挂了“用日语怎么说,柯桥商务日语培训
  • 【机器学习300问】103、简单的经典卷积神经网络结构设计成什么样?以LeNet-5为例说明。
  • 【代码随想录算法训练营第37期 第二十一天 | LeetCode530.二叉搜索树的最小绝对差、501.二叉搜索树中的众数、236. 二叉树的最近公共祖先】
  • 2023 年网络等级保护考试题库及答案
  • springboot集成nacos
  • NoSQL数据库技术与应用 教学设计
  • 比较(一)利用python绘制条形图
  • 【面试】Oracle JDK和Open JDK什么关系?
  • 科学技术创新杂志科学技术创新杂志社科学技术创新编辑部2024年第10期目录
  • ES数据导出成csv文件
  • 结构型设计模式之装饰模式
  • Java - 当年很流行,现在已经淘汰的 Java 技术,请不要在继续学了!!!
  • 驻波比VSWR
  • 多线程-线程池