poi操作Excel中sheet的复制(java版)
更新时间:2024-06-03 09:57:01 阅读量: 综合文库 文档下载
- poi操作Excel推荐度:
- 相关推荐
package cn.labsoft.labos.utils.exportexcel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.util.Region; import org.apache.poi.ss.usermodel.Font;
public class CopyExcelSheet{ @SuppressWarnings(\ public static void main(String[] args) throws FileNotFoundException, IOException { String fromPath = \存放路径 String toPath = \保存新EXCEL路径 copyExcelSheet(fromPath, toPath); } /** * @Title: 根据文件路径复制sheet * @Description: 将fromPath文件中的sheet全部复制到toPath中 * @param @param fromPath 源文件全路径 * @param @param toPath 新文件全路径 * @param @throws IOException * @return 返回类型 * @throws */ @SuppressWarnings(\ public static void copyExcelSheet(String fromPath, String toPath) throws IOException { // 创建新的excel File toFile = new File(toPath); HSSFWorkbook wbCreat = null;
if (!toFile.isFile()) {
wbCreat = new HSSFWorkbook();//建立新HSSFWorkbook对象 } else { InputStream out = new FileInputStream(toFile); wbCreat = new HSSFWorkbook(out); }
File fromFile = new File(fromPath); if (fromFile.exists()) { try { InputStream in = new FileInputStream(fromFile); HSSFWorkbook wb = new HSSFWorkbook(in); for (int ii = 0; ii < wb.getNumberOfSheets(); ii++) { HSSFSheet sheet = wb.getSheetAt(ii); if (wbCreat.getSheetIndex(sheet.getSheetName()) >= 0) { continue; } HSSFSheet sheetCreat = wbCreat.createSheet(sheet .getSheetName()); // 复制源表中的合并单元格 MergerRegion(sheetCreat, sheet); int firstRow = sheet.getFirstRowNum(); int lastRow = sheet.getLastRowNum(); for (int i = firstRow; i <= lastRow; i++) { // 创建新建excel Sheet的行 HSSFRow rowCreat = sheetCreat.createRow(i); // 取得源有excel Sheet的行 HSSFRow row = sheet.getRow(i); if (row == null) continue; // 单元格式样 int firstCell = row.getFirstCellNum(); int lastCell = row.getLastCellNum(); for (int j = firstCell; j < lastCell; j++) { if (row.getCell(j) == null) continue; if (i == 0) { short width = sheet.getColumnWidth((short) j); sheetCreat.setColumnWidth((short) j,(short) (1.15 width)); boolean isHidden=sheet.isColumnHidden(j); sheetCreat.setColumnHidden(j,isHidden); }
rowCreat.createCell(j);
* // 设置高度 rowCreat.getCell(j).getRow().setHeight( row.getCell(j).getRow().getHeight()); //复制样式 HSSFCellStyle newStyle = wbCreat.createCellStyle(); HSSFCellStyle oldStely = row.getCell(j).getCellStyle(); newStyle=copyStyle(newStyle, wbCreat, oldStely, wb); rowCreat.getCell(j).setCellStyle(newStyle); //复制内容 rowCreat.getCell(j).setCellValue(copyfont(wbCreat, row.getCell(j))); } } } FileOutputStream fileOut = new FileOutputStream(toFile); wbCreat.write(fileOut); fileOut.flush(); fileOut.close(); in.close(); } catch (RuntimeException e) { e.printStackTrace(); } } }; /** * @Title: 复制sheet * @Description: 将fromFile文件中的sheet全部复制到toFile中 * @param @param fromFile .xls源文件 * @param @param toFile .xls新文件 * @param @throws IOException * @return 返回类型 * @throws */ @SuppressWarnings(\ public static void copyExcelSheet(File fromFile, File toFile) throws IOException { // 创建新的excel HSSFWorkbook wbCreat = null; if (!toFile.isFile()) { wbCreat = new HSSFWorkbook();//建立新HSSFWorkbook对象 } else { InputStream out = new FileInputStream(toFile); wbCreat = new HSSFWorkbook(out); }
wb,
if (fromFile.exists()) {
try { InputStream in = new FileInputStream(fromFile); HSSFWorkbook wb = new HSSFWorkbook(in); for (int ii = 0; ii < wb.getNumberOfSheets(); ii++) { width));
HSSFSheet sheet = wb.getSheetAt(ii); if (wbCreat.getSheetIndex(sheet.getSheetName()) >= 0) { continue; } HSSFSheet sheetCreat = wbCreat.createSheet(sheet .getSheetName()); // 复制源表中的合并单元格 MergerRegion(sheetCreat, sheet); int firstRow = sheet.getFirstRowNum(); int lastRow = sheet.getLastRowNum(); for (int i = firstRow; i <= lastRow; i++) { // 创建新建excel Sheet的行 HSSFRow rowCreat = sheetCreat.createRow(i); // 取得源有excel Sheet的行 HSSFRow row = sheet.getRow(i); if (row == null) continue; // 单元格式样 int firstCell = row.getFirstCellNum(); int lastCell = row.getLastCellNum(); for (int j = firstCell; j < lastCell; j++) { if (row.getCell(j) == null) continue; if (i == 0) { short width = sheet.getColumnWidth((short) j); sheetCreat.setColumnWidth((short) j,(short) (1.15 boolean isHidden=sheet.isColumnHidden(j); sheetCreat.setColumnHidden(j,isHidden); }
rowCreat.createCell(j); // 设置高度
rowCreat.getCell(j).getRow().setHeight( row.getCell(j).getRow().getHeight()); //复制样式
HSSFCellStyle newStyle = wbCreat.createCellStyle(); HSSFCellStyle oldStely = row.getCell(j).getCellStyle();
newStyle=copyStyle(newStyle, wbCreat, oldStely, wb);
* rowCreat.getCell(j).setCellStyle(newStyle); //复制内容 rowCreat.getCell(j).setCellValue(copyfont(wbCreat, row.getCell(j))); } } } FileOutputStream fileOut = new FileOutputStream(toFile); wbCreat.write(fileOut); fileOut.flush(); fileOut.close(); in.close(); } catch (RuntimeException e) { e.printStackTrace(); } } }; /** * @Title: 复制源文件中的第n个sheet * @Description: 将fromFile文件中的第n个sheet复制到toFile中 * @param @param fromFile .xls源文件 * @param @param n 源文件中要复制的sheet的序号(从0开始) * @param @param toFile .xls新文件 * @param @throws IOException * @return 返回类型 * @throws */ @SuppressWarnings(\ public static void copyExcelSheet(File fromFile,int n, File toFile) throws IOException { // 创建新的excel HSSFWorkbook wbCreat = null; if (!toFile.isFile()) { wbCreat = new HSSFWorkbook();//建立新HSSFWorkbook对象 } else { InputStream out = new FileInputStream(toFile); wbCreat = new HSSFWorkbook(out); } if (fromFile.exists()) { try { InputStream in = new FileInputStream(fromFile); HSSFWorkbook wb = new HSSFWorkbook(in); if(wb.getNumberOfSheets()>=(n+1)){ HSSFSheet sheet = wb.getSheetAt(n);
wb,
width)); row.getCell(j)));
String sheetName=sheet.getSheetName(); int x=1;
while(wbCreat.getSheetIndex(sheetName) >= 0){ sheetName+=\}
HSSFSheet sheetCreat = wbCreat.createSheet(sheetName); // 复制源表中的合并单元格 MergerRegion(sheetCreat, sheet);
int firstRow = sheet.getFirstRowNum(); int lastRow = sheet.getLastRowNum(); for (int i = firstRow; i <= lastRow; i++) { // 创建新建excel Sheet的行 HSSFRow rowCreat = sheetCreat.createRow(i); // 取得源有excel Sheet的行 HSSFRow row = sheet.getRow(i); if (row == null) continue; // 单元格式样 int firstCell = row.getFirstCellNum(); int lastCell = row.getLastCellNum(); for (int j = firstCell; j < lastCell; j++) { if (row.getCell(j) == null) continue; if (i == 0) { short width = sheet.getColumnWidth((short) j); sheetCreat.setColumnWidth((short) j,(short) (1.15 * boolean isHidden=sheet.isColumnHidden(j); sheetCreat.setColumnHidden(j,isHidden); }
rowCreat.createCell(j); // 设置高度
rowCreat.getCell(j).getRow().setHeight( row.getCell(j).getRow().getHeight()); //复制样式
HSSFCellStyle newStyle = wbCreat.createCellStyle(); HSSFCellStyle oldStely = row.getCell(j).getCellStyle(); newStyle=copyStyle(newStyle, wbCreat, oldStely, wb); rowCreat.getCell(j).setCellStyle(newStyle); //复制内容
rowCreat.getCell(j).setCellValue(copyfont(wbCreat,
wb,
}
} } FileOutputStream fileOut = new FileOutputStream(toFile); wbCreat.write(fileOut); fileOut.flush(); fileOut.close(); in.close(); } catch (RuntimeException e) { e.printStackTrace(); } } }; /**
* @Title: 复制源文件中的名称为sheetName的sheet
* @Description: 将fromFile文件中的名称为sheetName的sheet复制到toFile中 * @param @param fromFile .xls源文件
* @param @param sheetName 源文件中要复制的sheet的名称 * @param @param toFile .xls新文件 * @param @throws IOException * @return 返回类型 * @throws */
@SuppressWarnings(\
public static void copyExcelSheet(File fromFile,String sheetName, File toFile) throws IOException { // 创建新的excel HSSFWorkbook wbCreat = null; if (!toFile.isFile()) { wbCreat = new HSSFWorkbook();//建立新HSSFWorkbook对象 } else { InputStream out = new FileInputStream(toFile); wbCreat = new HSSFWorkbook(out); } if (fromFile.exists()) { try { InputStream in = new FileInputStream(fromFile); HSSFWorkbook wb = new HSSFWorkbook(in); if(wb.getSheetIndex(sheetName)>=0){ HSSFSheet sheet = wb.getSheetAt(wb.getSheetIndex(sheetName)); int x=1; while(wbCreat.getSheetIndex(sheetName) >= 0){ sheetName+=\ } HSSFSheet sheetCreat = wbCreat.createSheet(sheetName);
width)); row.getCell(j)));
// 复制源表中的合并单元格 MergerRegion(sheetCreat, sheet);
int firstRow = sheet.getFirstRowNum(); int lastRow = sheet.getLastRowNum(); for (int i = firstRow; i <= lastRow; i++) { // 创建新建excel Sheet的行 HSSFRow rowCreat = sheetCreat.createRow(i); // 取得源有excel Sheet的行 HSSFRow row = sheet.getRow(i); if (row == null) continue; // 单元格式样 int firstCell = row.getFirstCellNum(); int lastCell = row.getLastCellNum(); for (int j = firstCell; j < lastCell; j++) { if (row.getCell(j) == null) continue; if (i == 0) { short width = sheet.getColumnWidth((short) j); sheetCreat.setColumnWidth((short) j,(short) (1.15 * boolean isHidden=sheet.isColumnHidden(j); sheetCreat.setColumnHidden(j,isHidden); }
rowCreat.createCell(j); // 设置高度
rowCreat.getCell(j).getRow().setHeight( row.getCell(j).getRow().getHeight()); //复制样式
HSSFCellStyle newStyle = wbCreat.createCellStyle(); HSSFCellStyle oldStely = row.getCell(j).getCellStyle(); newStyle=copyStyle(newStyle, wbCreat, oldStely, wb); rowCreat.getCell(j).setCellStyle(newStyle); //复制内容
rowCreat.getCell(j).setCellValue(copyfont(wbCreat,
wb,
} } }
FileOutputStream fileOut = new FileOutputStream(toFile); wbCreat.write(fileOut); fileOut.flush(); fileOut.close();
in.close(); } catch (RuntimeException e) { e.printStackTrace(); } } }; private static HSSFRichTextString copyfont(HSSFWorkbook wbCreat,HSSFWorkbook wb,HSSFCell oldCell){ int fromIndex = 0; int toIndex = 0; String value = \ //获取单元格中的数据 HSSFRichTextString rts = (HSSFRichTextString) oldCell.getRichStringCellValue(); //获取每个单元格数据的style属性 HSSFFont fontTemp =oldCell.getCellStyle().getFont(wb); //处理上下标 if (rts.numFormattingRuns() > 0) { for (int k = 0; k < rts.numFormattingRuns(); k++) { toIndex = rts.getIndexOfFormattingRun(k); String temp = rts.toString().substring(fromIndex, toIndex); //判断下标 if (fontTemp.getTypeOffset() == HSSFFont.SS_SUB) { Font font1 = wbCreat.createFont(); font1.setTypeOffset(HSSFFont.SS_SUB); rts.applyFont(fromIndex, toIndex,font1); } //判断上标 if (fontTemp.getTypeOffset() == HSSFFont.SS_SUPER) { Font font2 = wbCreat.createFont(); font2.setTypeOffset(HSSFFont.SS_SUPER); rts.applyFont(fromIndex, toIndex,font2); } value += temp; if (!value.equals(\ fontTemp = wb.getFontAt(rts.getFontOfFormattingRun(k)); } fromIndex = toIndex; } toIndex = rts.length(); String temp1 = rts.toString().substring(fromIndex, toIndex); if (fontTemp.getTypeOffset() == HSSFFont.SS_SUB) { Font font1 = wbCreat.createFont(); font1.setTypeOffset(HSSFFont.SS_SUB); rts.applyFont(fromIndex, toIndex,font1);
} if (fontTemp.getTypeOffset() == HSSFFont.SS_SUPER) { Font font2 = wbCreat.createFont(); font2.setTypeOffset(HSSFFont.SS_SUPER); rts.applyFont(fromIndex, toIndex,font2); } value += temp1; } return rts; } private static HSSFCellStyle copyStyle(HSSFCellStyle newStyle,HSSFWorkbook wbCreat,HSSFCellStyle oldStely,HSSFWorkbook wb){ newStyle.setAlignment(oldStely.getAlignment()); newStyle.setBorderTop(oldStely.getBorderTop()); newStyle.setBorderBottom(oldStely.getBorderBottom()); newStyle.setBorderLeft(oldStely.getBorderLeft()); newStyle.setBorderRight(oldStely.getBorderRight()); newStyle.setBottomBorderColor(oldStely.getBottomBorderColor()); newStyle.setFillBackgroundColor(oldStely.getFillBackgroundColor()); newStyle.setDataFormat(oldStely.getDataFormat()); newStyle.setFillForegroundColor(oldStely.getFillForegroundColor()); newStyle.setFillPattern(oldStely.getFillPattern()); newStyle.setHidden(oldStely.getHidden()); newStyle.setIndention(oldStely.getIndention()); newStyle.setLeftBorderColor(oldStely.getLeftBorderColor()); newStyle.setLocked(oldStely.getLocked()); newStyle.setRightBorderColor(oldStely.getRightBorderColor()); newStyle.setRotation(oldStely.getRotation()); newStyle.setTopBorderColor(oldStely.getTopBorderColor()); newStyle.setVerticalAlignment(oldStely.getVerticalAlignment()); newStyle.setWrapText(oldStely.getWrapText()); //设置字体 Font newfont = wbCreat.createFont(); HSSFFont font = oldStely.getFont(wb); newfont.setFontHeightInPoints(font.getFontHeightInPoints());//设置字号 newfont.setFontName(font.getFontName());//设置字体 newfont.setItalic(font.getItalic());//设置是否斜体 newfont.setStrikeout(font.getStrikeout());//设置是否强调 newfont.setBoldweight(font.getBoldweight()); newfont.setFontHeight(font.getFontHeight()); newfont.setCharSet(font.getCharSet()); newfont.setColor(font.getColor()); newfont.setTypeOffset(font.getTypeOffset()); newfont.setUnderline(font.getUnderline());
}
newStyle.setFont(newfont); return newStyle; } /**
* 复制原有sheet的合并单元格到新创建的sheet *
* @param sheetCreat
* 新创建sheet * @param sheet
* 原有的sheet */
@SuppressWarnings(\
private static void MergerRegion(HSSFSheet sheetCreat, HSSFSheet sheet) { int sheetMergerCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergerCount; i++) { Region mergedRegionAt = sheet.getMergedRegionAt(i); sheetCreat.addMergedRegion(mergedRegionAt); } }
}
newStyle.setFont(newfont); return newStyle; } /**
* 复制原有sheet的合并单元格到新创建的sheet *
* @param sheetCreat
* 新创建sheet * @param sheet
* 原有的sheet */
@SuppressWarnings(\
private static void MergerRegion(HSSFSheet sheetCreat, HSSFSheet sheet) { int sheetMergerCount = sheet.getNumMergedRegions(); for (int i = 0; i < sheetMergerCount; i++) { Region mergedRegionAt = sheet.getMergedRegionAt(i); sheetCreat.addMergedRegion(mergedRegionAt); } }
正在阅读:
poi操作Excel中sheet的复制(java版)06-03
初中历史第2次作业06-12
土木工程经济学期末试题(含答案)10-02
不相容岗位分离说透04-15
【资格考试】2019最新整理--(备考辅导)英语常用同义词辨析N04-30
2019年部编版小学五年级数学上册全册知识点易错题及答案02-28
最新-财政所2019年上半年工作总结及下半年工作计划 精品03-16
县妇联驻村调研02-11
第四章 - 工程经济评价的基本指标04-27
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 复制
- 操作
- Excel
- sheet
- java
- poi
- 2014高考生物一轮复习题库 2-3-3从杂交育种到基因工程
- 企业会计准则第14号-收入课件及习题答案
- 程泽辉劳模创新工作室汇报材料(册) - 中国电信
- 《中国古代文学Ⅰ(唐前)》在线测试
- 2007年北京市高考语文阅卷纵横
- 《抄表核算收费员》中级工理论试卷
- 考勤管理规定
- 防盗报警装置的设计
- 14秋东财《人力资源管理》在线作业三(随机)答案
- 三年级英语学习小能手练习题(新)
- 《 基本不等式的证明》教学设计
- 土地整理项目施工组织设计方案
- 第三章 三相交流电路
- 俄语语法图文并解
- 中学生政治小论文写作指导策略的实践研究
- 基于51单片机的电热水器控制系统设计 - 图文
- 原子结构和周期律试题与答案
- 分类号(宋体小三加黑)
- 滦县春玉米高产创建技术规范模式图
- 2015版马克思主义基本原理概论课后题答案