java poi 如何合并多个sheet 为一个sheet_Java POI组件实现多个Excel文件整合成一个多Sheet的Excel文件...
代码:
package com.weichai;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.DateUtil;
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;
public class Test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// 将所有类型的尽调excel文件合并成一个excel文件
String Path = "D:\\ExcelTest";
File file = new File(Path);
File[] tempList = file.listFiles();
String TmpList [] = new String [2];
System.out.println("该目录下对象个数:" + tempList.length);
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
TmpList[i] = tempList[i].toString();
System.out.println("文件:"+TmpList[i]+" 待处理");
}
}
XSSFWorkbook newExcelCreat = new XSSFWorkbook();
for (String fromExcelName : TmpList) { // 遍历每个源excel文件,TmpList为源文件的名称集合
InputStream in = new FileInputStream(fromExcelName);
XSSFWorkbook fromExcel = new XSSFWorkbook(in);
int length = fromExcel.getNumberOfSheets();
if(length<=1){ //长度为1时
XSSFSheet oldSheet = fromExcel.getSheetAt(0);
XSSFSheet newSheet = newExcelCreat.createSheet(oldSheet.getSheetName());
copySheet(newExcelCreat, oldSheet, newSheet);
}else{
for (int i = 0; i < length; i++) {// 遍历每个sheet
XSSFSheet oldSheet = fromExcel.getSheetAt(i);
XSSFSheet newSheet = newExcelCreat.createSheet(oldSheet.getSheetName());
copySheet(newExcelCreat, oldSheet, newSheet);
}
}
}
String allFileName = Path+ "\\New.xlsx"; //定义新生成的xlx表格文件
FileOutputStream fileOut = new FileOutputStream(allFileName);
newExcelCreat.write(fileOut);
fileOut.flush();
fileOut.close();
删除各个源文件
//for (String fromExcelName : TmpList) {// 遍历每个源excel文件
//File Existfile = new File(fromExcelName);
//if (Existfile.exists()) {
//Existfile.delete();
//}
//}
System.out.println("运行结束!");
}
public static void copyCellStyle(XSSFCellStyle fromStyle, XSSFCellStyle toStyle) {
toStyle.cloneStyleFrom(fromStyle);// 此一行代码搞定
// 下面统统不用
/*
* //对齐方式 toStyle.setAlignment(fromStyle.getAlignment()); //边框和边框颜色
* toStyle.setBorderBottom(fromStyle.getBorderBottom());
* toStyle.setBorderLeft(fromStyle.getBorderLeft());
* toStyle.setBorderRight(fromStyle.getBorderRight());
* toStyle.setBorderTop(fromStyle.getBorderTop());
* toStyle.setTopBorderColor(fromStyle.getTopBorderColor());
* toStyle.setBottomBorderColor(fromStyle.getBottomBorderColor());
* toStyle.setRightBorderColor(fromStyle.getRightBorderColor());
* toStyle.setLeftBorderColor(fromStyle.getLeftBorderColor()); //背景和前景
* //toStyle.setFillPattern(fromStyle.getFillPattern());
* //填充图案,不起作用,转为黑色
* toStyle.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
* //不起作用
* toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
* toStyle.setDataFormat(fromStyle.getDataFormat()); //数据格式
* //toStyle.setFont(fromStyle.getFont()); //不起作用
* toStyle.setHidden(fromStyle.getHidden());
* toStyle.setIndention(fromStyle.getIndention());//首行缩进
* toStyle.setLocked(fromStyle.getLocked());
* toStyle.setRotation(fromStyle.getRotation());//旋转
* toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
* //垂直对齐 toStyle.setWrapText(fromStyle.getWrapText()); //文本换行
*/
}
/**
* 合并单元格
* @param fromSheet
* @param toSheet
*/
public static void mergeSheetAllRegion(XSSFSheet fromSheet, XSSFSheet toSheet) {
int num = fromSheet.getNumMergedRegions();
CellRangeAddress cellR = null;
for (int i = 0; i < num; i++) {
cellR = fromSheet.getMergedRegion(i);
toSheet.addMergedRegion(cellR);
}
}
/**
* 复制单元格
* @param wb
* @param fromCell
* @param toCell
*/
public static void copyCell(XSSFWorkbook wb, XSSFCell fromCell, XSSFCell toCell) {
XSSFCellStyle newstyle = wb.createCellStyle();
copyCellStyle(fromCell.getCellStyle(), newstyle);
// toCell.setEncoding(fromCell.getStringCelllValue());
// 样式
toCell.setCellStyle(newstyle);
if (fromCell.getCellComment() != null) {
toCell.setCellComment(fromCell.getCellComment());
}
// 不同数据类型处理
int fromCellType = fromCell.getCellType();
toCell.setCellType(fromCellType);
if (fromCellType == XSSFCell.CELL_TYPE_NUMERIC) {
if (XSSFDateUtil.isCellDateFormatted(fromCell)) {
toCell.setCellValue(fromCell.getDateCellValue());
} else {
toCell.setCellValue(fromCell.getNumericCellValue());
}
} else if (fromCellType == XSSFCell.CELL_TYPE_STRING) {
toCell.setCellValue(fromCell.getRichStringCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_BLANK) {
// nothing21
} else if (fromCellType == XSSFCell.CELL_TYPE_BOOLEAN) {
toCell.setCellValue(fromCell.getBooleanCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_ERROR) {
toCell.setCellErrorValue(fromCell.getErrorCellValue());
} else if (fromCellType == XSSFCell.CELL_TYPE_FORMULA) {
toCell.setCellFormula(fromCell.getCellFormula());
} else { // nothing29
}
}
/**
* 行复制功能
* @param wb
* @param oldRow
* @param toRow
*/
public static void copyRow(XSSFWorkbook wb, XSSFRow oldRow, XSSFRow toRow) {
toRow.setHeight(oldRow.getHeight());
for (Iterator cellIt = oldRow.cellIterator(); cellIt.hasNext();) {
XSSFCell tmpCell = (XSSFCell) cellIt.next();
XSSFCell newCell = toRow.createCell(tmpCell.getColumnIndex());
copyCell(wb, tmpCell, newCell);
}
}
/**
* Sheet复制
* @param wb
* @param fromSheet
* @param toSheet
*/
public static void copySheet(XSSFWorkbook wb, XSSFSheet fromSheet, XSSFSheet toSheet) {
mergeSheetAllRegion(fromSheet, toSheet);
// 设置列宽
int length = fromSheet.getRow(fromSheet.getFirstRowNum()).getLastCellNum();
for (int i = 0; i <= length; i++) {
toSheet.setColumnWidth(i, fromSheet.getColumnWidth(i));
}
for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
XSSFRow oldRow = (XSSFRow) rowIt.next();
XSSFRow newRow = toSheet.createRow(oldRow.getRowNum());
copyRow(wb, oldRow, newRow);
}
}
public class XSSFDateUtil extends DateUtil {
}
}
运行截图:
将Student1和Student2的数据整合到一个Excel表单中
生成的New.xlsx文件如下图所示:
java poi 如何合并多个sheet 为一个sheet_Java POI组件实现多个Excel文件整合成一个多Sheet的Excel文件...相关推荐
- java实现把一个大文件切割成N个固定大小的文件
这个好像是我一年前去面试时的一道面试题,分享一下!考 java I/O 的! //java实现把一个大文件切割成N个固定大小的文件 package com.johnny.test; import ja ...
- 报表合并软件怎么快速将多表格文件整合到一个表
今天跟大家分享一下报表合并软件怎么快速将多表格文件整合到一个表 1.如下图有多个excel文件,现在要求将这几个文件合并到一起 2.首先我们点击下图选项 3.鼠标点击[汇总拆分]-[汇总大师] 4.点 ...
- 使用java iTest实现PDF大文件压缩——将文件过大的图片PDF文件压缩成小一些的图片PDF文件
一.需求 项目中需要将文件大小过大的PDF文件,压缩成小PDF文件.通过iText的API,可以实现此需求.在保证文件不失真的前提下,将PDF大文件压缩成小文件. 二.代码 import com.it ...
- Java导出多个excel并且打包成zip压缩文件
最近的项目有一个导出汇总数据的要求,考虑到用户软件的差异,所以要分别导出xls以及xlsx并且打包提供下载,所以有了这个博客,代码如下. 实现思路: 获取需要导出的数据: 生成对应的xls.xlsx文 ...
- 将多个CSV合并成一个python
功能:将某个目录下的CSV文件合并成一个 #将某个目录下的CSV文件合格成一个 import pandas as pd import os def merge_csv():# 待处理的目录input_ ...
- Java poi导入合并单元格的excel数据【最完整】附pom文件和excel截图
代码如下: package com.haha.demo;import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermo ...
- JAVA POI拆分合并的单元格
JAVA POI拆分合并的单元格 最近项目中需要做一个导入功能,但是EXCEL数据有合并单元格的情况出现,导入的数据需要直接入库,合并单元格的数据首行会有数据,但次行的值为空.见下图 需要获取C列和E ...
- Java多个ppt合并脚本_Apache POI PPT - 合并( Merging)
Apache POI PPT - 合并( Merging) 您可以使用XMLSlideShow类的importContent()方法合并多个演示文稿. 以下是合并两个演示文稿的完整程序 -import ...
- poi读取合并单元格
poi读取合并单元格 学习了:http://blog.csdn.net/ycb1689/article/details/9764191 进行了列合并单元格的修正:原来是我自己找错了地方: import ...
最新文章
- cesium绘制网格_Cesium学习笔记-工具篇37-风场绘制
- java实验类与对象_【实验课件】上机实践2 类与对象
- python 视频分析_成为视频分析专家:自动生成集锦的方法(Python实现)
- Kubernetes RBAC 详解
- AMD yes! 平民炬丹师的卡皇Radeon VII,老年博士生的丹房修建记录:Rocm 3.1+ pytorch 1.4+tensorflow 2+tensorflow 1.15.2 折腾记
- 永川机器人博览会门票_14日 又到永川来看机器人哦
- 网络安全kali渗透学习 web渗透入门 Layer子域名挖掘机收集信息
- 【绝对干货】Swift是花拳绣腿吗?从Swift谈开发语言与程序员的职业发展
- 数据挖掘:Apriori(先验)算法
- linux的iso镜像文件,linux系统安装iso文件方法
- 《别看了,你学不会的》——Redis原理与实战(一)
- 华为正式发布方舟编译器,相关源码已开放下载;微软开源量子开发工具包 QDK;GitHub回应突然断供:也很无可奈何的样子……...
- 小白MacBook超级实战教程——装双系统WIN10
- 谷歌浏览器无法翻译此网页,最新解决方案(谷歌浏览器无法翻译成中文)
- 2022-2028年中国桌面云产业发展动态及市场需求预测报告
- 【查找】临近省赛,Alan邀请emoji玩起了猜数字游戏活跃一下大脑。游戏规则如下:首先Alan写下几个数字让emoji猜,当emoji猜完之后,Alan会给他一个提示(java)
- AI技术再升级:2022年最流行的10大人工智能技术
- 联合体union内有数组的情况
- 进程间通信之匿名管道和命名管道
- ssh-keygen产生公钥与私钥对,及密钥分发,ssh远程执行常用命令方法,和如何防止SSH登录入侵或被破解
热门文章
- jQuery二维数组遍历
- python raise valueerror_请教:Python多进程编程时出现raise ValueError(Pool not running) 为什么?。...
- java通过jxl处理execl空行_jxl操作Excel导入数据库之空行的处理
- xposed框架在分机安装失败_免root用Xposed框架!安卓用户一定要学会
- 计算机网络全部实验,计算机网络综合实验
- mysql获取当前节点的所有叶子节点_mssql sqlserver 如何获取一个叶子节点下所有子节点呢?...
- roboware studio_关于安装Roboware的教程
- Tomcat9 启动警告 org.apache.catalina.webresources.Cache.getResource ...请考虑增加缓存的最大空间
- nginx loaction
- 【spring boot】url中传递session id