Java使用POI实现多个excel合并成一个excel
Java使用POI实现多个excel合并成一个excel。
(1)使用技术
- 编程语言:Java
- 依赖jar包:poi
poi组件下载地址:Index of /dist/poi/release/bin
(2)代码实现
实现思路:
- excel操作主要有如下几个步骤:
- 创建新的excel工作簿
- 合并单元格
- 创建行
- 创建列
- 设置单元格样式
- 设置单元格值
实现代码:
package com.gitee.zhuyb.excel.util;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;/*** @类名称: #ExcelUtil.java* @类描述: #Excel工具类* @创建人员: zhuyb* @创建时间: 2021-07-31 8:38:58* ------------------------------------------------------------* @version 1.0.0* @Copyright (C) zhuyb*/
public abstract class ExcelUtil {/*** #合并多个excel文件* @param fileLists excel文件路径 * @param path 目标文件保存目录* @param fileName 目标文件名称*/public static void mergeExcel(List<String> fileLists, String path, String fileName) {// 创建新的excel工作簿XSSFWorkbook newExcelWorkBook = new XSSFWorkbook();// 遍历需要合并的excel文件for (String excelName : fileLists) {try (InputStream in = new FileInputStream(excelName)) {// 创建工作簿XSSFWorkbook tmpWorkBook = new XSSFWorkbook(in);// 获取工作簿中的Sheet个数int len = tmpWorkBook.getNumberOfSheets();if (len <= 1) {XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(0);XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());// 复制sheet内容copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);} else {for (int i = 0; i < len; i++) {XSSFSheet tmpSheet = tmpWorkBook.getSheetAt(i);XSSFSheet newExcelSheet = newExcelWorkBook.createSheet(tmpSheet.getSheetName());// 复制sheet内容copyExcelSheet(newExcelWorkBook, tmpSheet, newExcelSheet);}}// 关闭tmpWorkBook工作簿tmpWorkBook.close();} catch (IOException e) {e.printStackTrace();}}// 新生成的excel文件if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {fileName += ".xlsx";}String excelFileName = path + File.separator + fileName;// 判断文件是否存在File excelFile = new File(excelFileName);if (excelFile.exists()) {// 存在则删除excelFile.delete();}// 使用输出流写出try (FileOutputStream fos = new FileOutputStream(excelFileName)) {newExcelWorkBook.write(fos);fos.flush();} catch (IOException e) {e.printStackTrace();} finally {try {newExcelWorkBook.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("excel文件合并成功,合并后文件路径:" + excelFileName);}/*** #复制sheet到新的excel文件中* @param workbook excel工作簿* @param tmpSheet 来源sheet* @param newExcelSheet 新生成的sheet*/public static void copyExcelSheet(XSSFWorkbook workbook, XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {// 合并单元格mergeSheetAllRegion(tmpSheet, newExcelSheet);// 设置单元格列宽度// 获取最后一个单元格位置int len = tmpSheet.getRow(tmpSheet.getFirstRowNum()).getLastCellNum();for (int i = 0; i < len; i++) {newExcelSheet.setColumnWidth(i, tmpSheet.getColumnWidth(i));}// 复制每行内容Iterator<Row> it = tmpSheet.iterator();while (it.hasNext()) {XSSFRow tmpRow = (XSSFRow) it.next();// 创建新行XSSFRow newExcelRow = newExcelSheet.createRow(tmpRow.getRowNum());// 复制行copyExcelRow(workbook, tmpRow, newExcelRow);}}/*** #合并单元格* @param tmpSheet 来源sheet* @param newExcelSheet 目标sheet*/private static void mergeSheetAllRegion(XSSFSheet tmpSheet, XSSFSheet newExcelSheet) {int num = tmpSheet.getNumMergedRegions();CellRangeAddress cellRange = null;for (int i = 0; i < num; i++) {cellRange = tmpSheet.getMergedRegion(i);newExcelSheet.addMergedRegion(cellRange);}}/*** #复制excel中的行到新的sheet中* @param workbook 目标工作簿* @param tmpRow 来源excel行* @param newExcelRow 目标excel行*/public static void copyExcelRow(XSSFWorkbook workbook, XSSFRow tmpRow, XSSFRow newExcelRow) {// 设置行高newExcelRow.setHeight(tmpRow.getHeight());// 获取所有列Iterator<Cell> it = tmpRow.cellIterator();while (it.hasNext()) {XSSFCell tmpCell = (XSSFCell) it.next();// 创建单元格XSSFCell newExcelCell = newExcelRow.createCell(tmpCell.getColumnIndex());// 复制单元格copyExcelCell(workbook, tmpCell, newExcelCell);}}/*** #复制单元格* @param workbook 目标工作簿* @param tmpCell 来源excel单元格* @param newExcelCell 目标excel单元格*/public static void copyExcelCell(XSSFWorkbook workbook, XSSFCell tmpCell, XSSFCell newExcelCell) {XSSFCellStyle newExcelStyle = workbook.createCellStyle();// 复制单元格样式newExcelStyle.cloneStyleFrom(tmpCell.getCellStyle());// 单元格样式newExcelCell.setCellStyle(newExcelStyle);if (tmpCell.getCellComment() != null) {newExcelCell.setCellComment(tmpCell.getCellComment());}// 不同数据类型处理CellType tmpCellType = tmpCell.getCellType();newExcelCell.setCellType(tmpCellType);if (tmpCellType == CellType.NUMERIC) {if (DateUtil.isCellDateFormatted(tmpCell)) {newExcelCell.setCellValue(tmpCell.getDateCellValue());} else {newExcelCell.setCellValue(tmpCell.getNumericCellValue());}} else if (tmpCellType == CellType.STRING) {newExcelCell.setCellValue(tmpCell.getRichStringCellValue());} else if (tmpCellType == CellType.BLANK) {} else if (tmpCellType == CellType.BOOLEAN) {newExcelCell.setCellValue(tmpCell.getBooleanCellValue());} else if (tmpCellType == CellType.ERROR) {newExcelCell.setCellErrorValue(tmpCell.getErrorCellValue());} else if (tmpCellType == CellType.FORMULA) {newExcelCell.setCellFormula(tmpCell.getCellFormula());} else {}}}
代码测试:
package com.gitee.zhuyb.excel.app;import java.util.Arrays;
import java.util.List;import com.gitee.zhuyb.excel.util.ExcelUtil;/*** @类名称: #ExcelApplication.java* @类描述: #TODO* @创建人员: zhuyb* @创建时间: 2021-07-31 9:29:05* ------------------------------------------------------------* @version 1.0.0* @Copyright (C) zhuyb*/
public class ExcelApplication {public static void main(String[] args) {List<String> fileLists = Arrays.asList(new String[] {"C:\\Users\\zhuyb\\Desktop\\excel\\demo.xlsx","C:\\Users\\zhuyb\\Desktop\\excel\\demo02.xlsx"});String path = "C:\\Users\\zhuyb\\Desktop\\excel";String fileName = "合并单元格的excel";ExcelUtil.mergeExcel(fileLists, path, fileName);}}
在指定目录下面,创建两个excel用作测试文件。
然后运行测试类,查看生成的excel文件,所有sheet多合并。
Java使用POI实现多个excel合并成一个excel相关推荐
- 【python】用python实现多个excel合并成一个excel文件------直接上代码
小虾也是参考了一些大神的代码,然后自己动手实现了一下,然后发布出来,有需要的来学习呀! 小虾在这儿提供2种方法来实现,但小虾比较喜欢第一种,容易而且好理解一点. 第一种实现方法: # 导入需要使用的 ...
- 两个excel合并成一个excel
1.如下图是两份excel工作簿分别为某年级期中和期末考试成绩表,现在我们想要将这两个工作簿合并为一个工作簿. 2.将这个两个文档打开 3.任选一个工作簿,点击下图选项(Excel插件,百度即 ...
- 多个excel合并成一个excel的sheet
1.方案一:使用POI的API 缺点是:数据量稍微大一点生成特别耗时.数据量在3000条记录的样子 优点:不依赖第三方应用,使用POI自己的API完成 建议:小数据量可以满足要求, public cl ...
- python excel合并_Python把多个Excel合并成一个Excel
#! /usr/bin/python3 # -*- coding: utf-8 -*- # @Time : 2018/8/31 16:31 # @File : more_to_one # @Softw ...
- excel几个表合成一张_快速将多个excel表合并成一个excel表
应用场景:有很多张excel,而且excel表里面的结构基本一样,如何快速将这些excel合并在一个excel页面,便于后期分析和统计 技术实现:利用excel表的宏计算实现. 注意:金山的WPS没有 ...
- python 批量读取xlsx并合并_python合并多个excel表格数据-python如何读取多个excel合并到一个excel中...
python如何读取多个excel合并到一个excel中 思路 利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文 ...
- excel合并多个工作表_快速将多个Excel表格合并成一个Excel表格
之前在微信群内有朋友问我如何快速将多个Excel表格合并成一个Excel表格,当时没有讲解清楚,今天专门来告诉大家如何快速合并Excel表格到一个工作表中. 在合并表格中,不外乎以下两种情况: 将多个 ...
- 如何快速把多个excel表合并成一个excel表(不熟悉vba及公式的人)
对于不熟悉Excel公式,不熟悉vba的人,要如何按需要快速把多个excel表合并成一个excel表? 对于Excel的合并,很多需要通过vba变成,但对于不熟悉vba编程的人,如何合并呢?给大家推荐 ...
- excel如何把多张表合并成一个表_如何快速把多个excel表格合并成一个excel表
如何快速把多个excel表格合并成一个excel表呢? 首先,我们需要把多个excel表都放在同一个文件夹里面,并在这个文件夹里面新建一个excel文件. 用microsoft excel打开新建的e ...
- java实现多个mav文件拼接合并成一个mav文件
java实现多个mav文件拼接合并成一个mav文件,绝对有效 解决方法: import java.io.File; import java.io.IOException; import java.io ...
最新文章
- String和常量池
- 90 后利用平台漏洞薅羊毛,获利 45 万被抓捕!网友们却争论不休……
- JavaScript中“ =gt;”(等于或大于的箭头)的含义是什么?
- 探究C/C++可变参数
- 【PM模块】维护处理的控制和报告
- python保存几位小数 format
- Node Sass does not yet support your current environment
- java对象序列化并存储到文件和数据库
- [zz]为小米创建虚拟机路由器
- kpi权重设置原则_绩效指标确定权重的原则
- Unity 3D 如何获取鼠标移动事件
- 计算机报名503,503错误,教您网页出现503错误怎么解决
- Android 点击空白位置并且隐藏软键盘
- 找出列表中最大或最小的元素-python3
- uefi启动 多硬盘gtp_关于UEFI启动+GPT分区的一些经验
- 4. 同步方式(增量和全量)
- win10远程登录Ubuntu14.04图形化界面
- 华为云数字资产链,构建新型数字经济价值
- android8.0应用图标适配调整_Android应用图标微技巧,8.0系统中应用图标的适配
- vue路由传参的三种方式区别(params,query)