项目中,我们经常使用Poi来操作excel,但是经常碰到一个不方便的地方,不如最简单常用的,在两个excel之间复制sheet,复制行,复制单元格等。

我这里是最近刚做的一个简单封装。不是很好,必须始终传过去一个“目标workbook“的引用,留下个mark!

源码如下:

public class POIUtils {
//  /**
//   * 把一个excel中的cellstyletable复制到另一个excel,这里会报错,不能用这种方法,不明白呀?????
//   * @param fromBook
//   * @param toBook
//   */
//  public static void copyBookCellStyle(HSSFWorkbook fromBook,HSSFWorkbook toBook){
//      for(short i=0;i<fromBook.getNumCellStyles();i++){
//          HSSFCellStyle fromStyle=fromBook.getCellStyleAt(i);
//          HSSFCellStyle toStyle=toBook.getCellStyleAt(i);
//          if(toStyle==null){
//              toStyle=toBook.createCellStyle();
//          }
//          copyCellStyle(fromStyle,toStyle);
//      }
//  }
/**
* 复制一个单元格样式到目的单元格样式
* @param fromStyle
* @param toStyle
*/
public static void copyCellStyle(HSSFCellStyle fromStyle,
HSSFCellStyle toStyle) {
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.setFillBackgroundColor(fromStyle.getFillBackgroundColor());
toStyle.setFillForegroundColor(fromStyle.getFillForegroundColor());
toStyle.setDataFormat(fromStyle.getDataFormat());
toStyle.setFillPattern(fromStyle.getFillPattern());
//      toStyle.setFont(fromStyle.getFont(null));
toStyle.setHidden(fromStyle.getHidden());
toStyle.setIndention(fromStyle.getIndention());//首行缩进
toStyle.setLocked(fromStyle.getLocked());
toStyle.setRotation(fromStyle.getRotation());//旋转
toStyle.setVerticalAlignment(fromStyle.getVerticalAlignment());
toStyle.setWrapText(fromStyle.getWrapText());
}
/**
* Sheet复制
* @param fromSheet
* @param toSheet
* @param copyValueFlag
*/
public static void copySheet(HSSFWorkbook wb,HSSFSheet fromSheet, HSSFSheet toSheet,
boolean copyValueFlag) {
//合并区域处理
mergerRegion(fromSheet, toSheet);
for (Iterator rowIt = fromSheet.rowIterator(); rowIt.hasNext();) {
HSSFRow tmpRow = (HSSFRow) rowIt.next();
HSSFRow newRow = toSheet.createRow(tmpRow.getRowNum());
//行复制
copyRow(wb,tmpRow,newRow,copyValueFlag);
}
}
/**
* 行复制功能
* @param fromRow
* @param toRow
*/
public static void copyRow(HSSFWorkbook wb,HSSFRow fromRow,HSSFRow toRow,boolean copyValueFlag){
for (Iterator cellIt = fromRow.cellIterator(); cellIt.hasNext();) {
HSSFCell tmpCell = (HSSFCell) cellIt.next();
HSSFCell newCell = toRow.createCell(tmpCell.getCellNum());
copyCell(wb,tmpCell, newCell, copyValueFlag);
}
}
/**
* 复制原有sheet的合并单元格到新创建的sheet
*
* @param sheetCreat 新创建sheet
* @param sheet      原有的sheet
*/
public static void mergerRegion(HSSFSheet fromSheet, HSSFSheet toSheet) {
int sheetMergerCount = fromSheet.getNumMergedRegions();
for (int i = 0; i < sheetMergerCount; i++) {
Region mergedRegionAt = fromSheet.getMergedRegionAt(i);
toSheet.addMergedRegion(mergedRegionAt);
}
}
/**
* 复制单元格
*
* @param srcCell
* @param distCell
* @param copyValueFlag
*            true则连同cell的内容一起复制
*/
public static void copyCell(HSSFWorkbook wb,HSSFCell srcCell, HSSFCell distCell,
boolean copyValueFlag) {
HSSFCellStyle newstyle=wb.createCellStyle();
copyCellStyle(srcCell.getCellStyle(), newstyle);
distCell.setEncoding(srcCell.getEncoding());
//样式
distCell.setCellStyle(newstyle);
//评论
if (srcCell.getCellComment() != null) {
distCell.setCellComment(srcCell.getCellComment());
}
// 不同数据类型处理
int srcCellType = srcCell.getCellType();
distCell.setCellType(srcCellType);
if (copyValueFlag) {
if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
distCell.setCellValue(srcCell.getDateCellValue());
} else {
distCell.setCellValue(srcCell.getNumericCellValue());
}
} else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {
distCell.setCellValue(srcCell.getRichStringCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
// nothing21
} else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {
distCell.setCellValue(srcCell.getBooleanCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {
distCell.setCellErrorValue(srcCell.getErrorCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {
distCell.setCellFormula(srcCell.getCellFormula());
} else { // nothing29
}
}
}
}

poi操作excel,复制sheet,复制行,复制单元格相关推荐

  1. java使用poi操作excel删除一整行

    java使用poi操作excel删除一整行 需求1:删除excel表格第4行 代码示例: sheet.shiftRows(4, sheet.getLastRowNum(),-1); 第一个参数为行数( ...

  2. Java之使用poi导出excel文件,并为特定单元格加锁

    使用 SXSSFWorkbook 进行Excel导出下载 注意:测试结果没有达到预期,那换个方式试试 参考:1.https://blog.csdn.net/aiza4108/article/detai ...

  3. POI操作EXCEL实战

    一.POI概述 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 结构: HSSF - 提供读写Mi ...

  4. POI操作Excel常用方法总结 .

    2019独角兽企业重金招聘Python工程师标准>>> 一. POI简介              Apache POI是Apache软件基金会的开放源码函式库,POI提供API给J ...

  5. POI操作Excel常用方法总结

    转载自:http://blog.csdn.net/xjun15/article/details/5805429 一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提 ...

  6. POI操作Excel表格

    POI操作Excle表格:  一. POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. 二 ...

  7. poi操作 excel 中文API文档

    poi操作 excel 中文API文档 依赖: <dependency><groupId>org.apache.poi</groupId><artifactI ...

  8. Excel复制粘贴——跳过空单元格案例

    要将表一与表二合并为表3. 有两步要走,第一步就是将表1与表2的第一列填充完整,第二步就是将表1和表2的内容进行合并. 将表2第一列补充完整.选中表2A列,即E5:E15.[开始]选项卡[查找与选择] ...

  9. 自己封装的poi操作Excel工具类

    在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完成的功能是:读取Excel.汇总Exc ...

  10. poi操作excel之填充数据、删除行

    poi操作excel之填充数据.删除行 每篇一句励志:有人说,这世间有两种浪漫,一种是相濡以沫,另一种是相忘于江湖. 废话不多说,直接上代码! // 获取到你要填充数据的sheet XSSFSheet ...

最新文章

  1. 百度AI快车道—企业深度学习实战营,推荐系统主题专场即将开课
  2. JAVA产生加密公钥私钥_Java生成非对称型加密的公钥和私钥
  3. web服务器和应用服务器的区别以及负载均衡---学习笔记
  4. mysql 存储中文问题
  5. OvS、OvS-DPDK、VPP 基准性能对比
  6. CCNA课堂练习一:路由器链路备份功能
  7. java递归算法实现
  8. JS ES6中的箭头函数(Arrow Functions)使用
  9. Tair是一个高性能,分布式,可扩展,高可靠的key/value结构存储系统(转)
  10. 3. Web Dynpro for ABAP: Web Dynpro Window Web Dynpro Program
  11. SAP License:在产品结算参数设置
  12. Js整理工具-开发必备
  13. matlab入门学习(良心版本,适合小白)
  14. “此网页上的某个 Web 部件或 Web 表单控件无法显示或导入。找不到该类型,或该类型未注册为安全类型。”
  15. 解决word里鼠标滚动速度慢
  16. python中的换行与不换行
  17. 有着奋斗比之都之称的杭州,现在还适不适合年轻人奋斗?
  18. 一个电脑接两个显示器(win10)
  19. 企业如何从0到1落地BI项目
  20. k8s集群搭建-1mater2node

热门文章

  1. sharemouse切窗口就锁定了什么原因_iPhone 提示“Apple ID 已锁定”是什么原因?
  2. “协同联动”: 新一代智慧防火墙重新定义NGFW
  3. ubuntu安装串口工具minicom
  4. Windows2012 桌面显示我的电脑
  5. 算法描述 100的阶乘
  6. QT中的常用数据结构
  7. 美团Java岗面经分享(技术四面):Spring+JVM+多线程+算法+设计
  8. RH2288V3服务器 硬件安装以及更换硬件
  9. 5G前传采用无源波分技术时的受限距离
  10. python自动产品分类_商品自动分类的贝叶斯方法及Python实现