Java将一张excel数据填充到另一张excel表

server/2024/10/22 17:20:14/

需求:根据数据库中excel保存地址url,获取到Excel表格,将其中数据填充到一张新生成的excel表格中

1.设置读取excel和要填充excel的起始行列

java">public void setExcelData(String fileUrl, String downloadPath, String reportFilePath, String endTime) {// 读取excel开始行 14  结束行 322  int readStartRow = 14;int readEndRow = 322;// 读取excel开始列 9 结束列 12int readStartCell = 9;int readEndCell = 12;// 写入excel开始行 1, 开始列 2int writeStartRow = 1;int writeStartCell = 2;// 读取excel的第1个sheet,写入excel的第2个sheetint sheetIndex = 0;int outSheetIndex=2;//获取读取的excel,以及要填充的excelFile file = HttpUtil.downloadFileFromUrl(cmmFileUrl, downloadPath);File reportFile = FileUtil.file(reportFilePath);InputStream inputStream = null;InputStream reportInputStream = null;try {inputStream = FileUtil.getInputStream(file);reportInputStream = FileUtil.getInputStream(reportFile);setData(inputStream, reportInputStream, reportFilePath, sheetIndex, readStartRow, readEndRow, readStartCell, readEndCell, writeStartRow, writeStartCell, outSheetIndex, endTime);} catch (Exception e) {log.error("写入excel数据失败", e);} finally {try {assert inputStream != null;inputStream.close();} catch (IOException e) {log.error("关闭excel文件流失败");}try {assert reportInputStream != null;reportInputStream.close();} catch (IOException e) {log.error("关闭excel文件流失败");}FileUtil.del(file);}}

2.读取表格的文件流,进行数据填充

java">private static void setData(InputStream inputStream, InputStream reportInputStream, String reportFilePath, int sheetIndex, int readStartRow, int readEndRow, int readStartCell, int readEndCell,int writeStartRow, int writeStartCell, int outSheetIndex, String endTime) {Workbook wb = null;Workbook reportWb = null;OutputStream os = null;try {// 1、获取要读取的文件工作簿对象ZipSecureFile.setMinInflateRatio(-1.0d);wb= WorkbookFactory.create(inputStream);reportWb= WorkbookFactory.create(reportInputStream);// 2、获取工作表Sheet s = wb.getSheetAt(sheetIndex);Sheet rs = reportWb.getSheetAt(outSheetIndex);setValue(s, rs, readStartRow, readEndRow, readStartCell, readEndCell, writeStartRow, writeStartCell);// 2.1、工作表填入测量完成时间Sheet firstSheet = reportWb.getSheetAt(0);// 生成一个样式CellStyle style = reportWb.createCellStyle();// 设置字体Font font = reportWb.createFont();font.setFontHeightInPoints((short) 9);// 把字体应用到当前的样式style.setFont(font);//完成时间, 开始行 8, 开始列 112Row row2 = firstSheet.getRow(8);Cell cell2 = row2.createCell(112);if(StrUtil.isNotEmpty(endTime)){cell2.setCellValue(endTime);cell2.setCellStyle(style);}// 输出时通过流的形式对外输出,包装对应的目标文件os = Files.newOutputStream(Paths.get(reportFilePath));// 将内存中的workbook数据写入到流中reportWb.write(os);} catch (Exception e) {throw new RuntimeException(e);} finally {try {assert reportWb != null;reportWb.close();} catch (Exception e) {log.error("关闭ReportWb失败");}try {assert os != null;os.close();} catch (Exception e) {log.error("关闭输出流失败");}try {wb.close();} catch (Exception e) {log.error("关闭Wb失败");}}}

3.将读取excel数据填充到当前excel

java">private static void setValue(Sheet s, Sheet rs, int readStartRow, int readEndRow, int readStartCell, int readEndCell, int writeStartRow, int writeStartCell) {for (int i = readStartRow; i < readEndRow; i++) {int startCell = writeStartCell;for (int j = readStartCell; j < readEndCell; j++) {// 3、获取行  开始行 14  结束行 284  开始列 9 结束列Row row = s.getRow(i);// 4、获取列Cell cell = row.getCell(j);// 5、根据数据的类型获取数据Double data = null;String data1 = null;try {data = cell.getNumericCellValue();}catch (IllegalStateException e) {data1 = cell.getStringCellValue();}catch (NullPointerException e) {break;}// 报告开始行 1, 开始列 2Row rRow = rs.getRow(writeStartRow);if (rRow == null) {rRow = rs.createRow(writeStartRow);}Cell rCell = rRow.createCell(startCell);// 5、在列中写数据if(data != null) {rCell.setCellValue(data);}if(StringUtils.isNotBlank(data1)) {rCell.setCellValue(data1);}startCell++;}writeStartRow++;}}

http://www.ppmy.cn/server/104665.html

相关文章

Redis后台线程之非阻塞删除

当Redis执行删除命令的时候&#xff0c;如果被删除的对象是列表、集合、散列类型&#xff0c;因为这些数据类型包含的元素存放在不同的内存块中&#xff0c;redis需要遍历所有元素来释放其对应的内存块空间&#xff0c;这个耗时操作可能导致redis阻塞&#xff0c;redis4提供的U…

钡铼技术BL196模块化远程IO模块数字输入输出

钡铼技术的BL196模块化远程IO模块是一款专为工业自动化设计的高度灵活的解决方案。特别地&#xff0c;BL196模块支持数字输入/输出功能&#xff0c;这使得它能够在各种工业环境中发挥重要作用。 产品概述 BL196模块化远程IO模块是一种高度灵活的远程IO系统&#xff0c;它支持…

【前端基础篇】CSS基础速通万字介绍(下篇)

文章目录 前言背景属性背景颜色背景图片背景平铺背景位置背景尺寸 圆角矩形生成圆形生成圆角矩形 Chrome调试工具打开方式标签页含义elements标签页使用 元素显示模式块级元素行内元素/内联元素行内元素和块级元素的区别 盒模型边框内边距外边距 块级元素水平居中去除浏览器默认…

Ignition Gateway配置

Config-System backup和restore&#xff1a; backup可以直接备份整个gateway配置&#xff0c;包括所有项目。 restore可以恢复gateway配置&#xff0c;包括所有项目。

node版本管理工具Node Version Manager(nvm)的安装及使用

文章目录 一、nvm安装1.1,下载nvm安装包程序&#xff08;选最高版本安装即可&#xff0c;安装前先把本机电脑node卸载&#xff0c;不然管理不到&#xff09;双击安装包进行安装&#xff08;傻瓜式下一步即可&#xff09; 二、配置镜像2.1、使用管理员运行命令提示符2.2、检查nv…

嵌入式和单片机有什么区别?

目录 &#xff08;1&#xff09;什么是嵌入式&#xff1f; &#xff08;2&#xff09;什么是单片机&#xff1f; &#xff08;3&#xff09;嵌入式和单片机的共同点 &#xff08;4&#xff09;嵌入式和单片机的区别 &#xff08;1&#xff09;什么是嵌入式&#xff1f; 关…

【STM32】C语言基础补充

学习过程中发现自己好些需要用到的C语言语法、特征都不太熟练了&#xff0c;特意记录一下&#xff0c;免得忘记了&#xff0c;以后遇到了新的也会继续更新 目录 1 全局变量 2 结构体 3 静态变量 4 memset()函数 5 使用8位的存储器存16位的数 1 全局变量…