POI Excel合并 分页符的复制
上接
Java POI Excel sheet合并
[url]http://zhuyufufu.iteye.com/blog/2033386[/url]
Java POI Excel sheet 合并遇到的问题解决
[url]http://zhuyufufu.iteye.com/blog/2035033[/url]
[url]http://zhuyufufu.iteye.com/blog/2036578[/url]
在合并sheet的时候,分页符的复制也是非常重要的,不然打印就会变形
//分页符的拷贝 int[] rowBreaks = sourceSheet.getRowBreaks(); for (int rowBreaksIndex = 0; rowBreaksIndex < rowBreaks.length; rowBreaksIndex++) { targetSheet.setRowBreak(pPosition + rowBreaks[rowBreaksIndex]); }
完整的复制代码
public static HSSFWorkbook mergeHSSFWorkbooks(HSSFWorkbook[] workbooks) { if(workbooks == null || workbooks.length == 0){ return null; }else if(workbooks.length == 1){ return workbooks[0]; } HSSFWorkbook wbFirst = workbooks[0]; HSSFSheet toSheet = wbFirst.getSheetAt(0); for (int i = 1; i < workbooks.length; i++) { toSheet.setRowBreak(toSheet.getLastRowNum()); HSSFWorkbook wb = workbooks[i]; HSSFSheet fromsheet = wb.getSheetAt(0); if(i == 1){ copyRows(wbFirst, wb, fromsheet, toSheet, fromsheet.getFirstRowNum(), fromsheet.getLastRowNum(), (toSheet.getLastRowNum() + 1)); }else{ copyRows(wbFirst, wb, fromsheet, toSheet, fromsheet.getFirstRowNum(), fromsheet.getLastRowNum(), (toSheet.getLastRowNum() + 1)); } } return wbFirst; }
/** * @param destWorkBook 目标workbook * @param sourceWorkBook 源workbook * @param sourceSheet 源sheet * @param targetSheet 目sheet * @param pStartRow 起始读取行 * @param pEndRow 结束读取行 * @param pPosition 目标保存 */ public static void copyRows(HSSFWorkbook destWorkBook, HSSFWorkbook sourceWorkBook, HSSFSheet sourceSheet,HSSFSheet targetSheet, int pStartRow, int pEndRow, int pPosition) { HSSFRow sourceRow = null; HSSFRow targetRow = null; HSSFCell sourceCell = null; HSSFCell targetCell = null; int cType; int i; int j; int targetRowFrom; int targetRowTo;
if ((pStartRow == -1) || (pEndRow == -1)) { return; }
List<CellRangeAddress> oldRanges = new ArrayList<CellRangeAddress>(); for (i = 0; i < sourceSheet.getNumMergedRegions(); i++) { oldRanges.add(sourceSheet.getMergedRegion(i)); }
// 拷贝合并的单元格。原理:复制当前合并单元格后,原位置的格式会移动到新位置,需在原位置生成旧格式 for (int k = 0; k < oldRanges.size(); k++) { CellRangeAddress oldRange = oldRanges.get(k); CellRangeAddress newRange = new CellRangeAddress(oldRange .getFirstRow(), oldRange.getLastRow(), oldRange .getFirstColumn(), oldRange.getLastColumn());
if (oldRange.getFirstRow() >= pStartRow && oldRange.getLastRow() <= pEndRow) { targetRowFrom = oldRange.getFirstRow() - pStartRow + pPosition; targetRowTo = oldRange.getLastRow() - pStartRow + pPosition; oldRange.setFirstRow(targetRowFrom); oldRange.setLastRow(targetRowTo); targetSheet.addMergedRegion(oldRange); sourceSheet.addMergedRegion(newRange); } } // 设置列宽 for (i = pStartRow; i <= pEndRow; i++) { sourceRow = sourceSheet.getRow(i); if (sourceRow != null) { for (j = sourceRow.getLastCellNum(); j > sourceRow.getFirstCellNum(); j--) { targetSheet.setColumnWidth(j, sourceSheet.getColumnWidth(j)); targetSheet.setColumnHidden(j, false); } break; } } List<HSSFCellStyle> cellStyleList = new ArrayList<HSSFCellStyle>(); // 拷贝行并填充数据 for (; i <= pEndRow; i++) { sourceRow = sourceSheet.getRow(i); if (sourceRow == null) { continue; } targetRow = targetSheet.createRow(i - pStartRow + pPosition); targetRow.setHeight(sourceRow.getHeight()); for (j = sourceRow.getFirstCellNum(); j <= sourceRow.getPhysicalNumberOfCells(); j++) { sourceCell = sourceRow.getCell(j); if (sourceCell == null) { continue; } targetCell = targetRow.createCell(j); //复制样式 //样式的设置 HSSFCellStyle cStyle = compareStoreAndGetCellStyle(cellStyleList, sourceCell.getCellStyle(), destWorkBook, sourceWorkBook); targetCell.setCellStyle(cStyle);
cType = sourceCell.getCellType(); targetCell.setCellType(cType); switch (cType) { case HSSFCell.CELL_TYPE_BOOLEAN: targetCell.setCellValue(sourceCell.getBooleanCellValue()); // System.out.println("--------TYPE_BOOLEAN:" + targetCell.getBooleanCellValue()); break; case HSSFCell.CELL_TYPE_ERROR: targetCell.setCellErrorValue(sourceCell.getErrorCellValue()); // System.out.println("--------TYPE_ERROR:" + targetCell.getErrorCellValue()); break; case HSSFCell.CELL_TYPE_FORMULA: // parseFormula这个函数的用途在后面说明 targetCell.setCellFormula(parseFormula(sourceCell.getCellFormula())); // System.out.println("--------TYPE_FORMULA:" + targetCell.getCellFormula()); break; case HSSFCell.CELL_TYPE_NUMERIC: targetCell.setCellValue(sourceCell.getNumericCellValue()); // System.out.println("--------TYPE_NUMERIC:" + targetCell.getNumericCellValue()); break; case HSSFCell.CELL_TYPE_STRING: targetCell.setCellValue(sourceCell.getRichStringCellValue()); // System.out.println("--------TYPE_STRING:" + i + targetCell.getRichStringCellValue()); break; default: targetCell.setCellValue(sourceCell.getRichStringCellValue()); // System.out.println("--------TYPE_STRING:" + i + targetCell.getRichStringCellValue()); break; } } }
//分页符的拷贝 int[] rowBreaks = sourceSheet.getRowBreaks(); for (int rowBreaksIndex = 0; rowBreaksIndex < rowBreaks.length; rowBreaksIndex++) { targetSheet.setRowBreak(pPosition + rowBreaks[rowBreaksIndex]); } }
/** * 比较存储并获得cellstyle * @param cellStyleList * @param cellStyle * @param sourceWorkBook * @return */ private static HSSFCellStyle compareStoreAndGetCellStyle( List<HSSFCellStyle> cellStyleList, HSSFCellStyle sourceCellStyle, HSSFWorkbook destWorkBook, HSSFWorkbook sourceWorkBook) { for (int index = 0; index < cellStyleList.size(); index++) { HSSFCellStyle cellStyle = cellStyleList.get(index); if(isEqual(cellStyle, sourceCellStyle, destWorkBook, sourceWorkBook)){ return cellStyle; } } //拷贝新的样式到列表 HSSFCellStyle cStyle = destWorkBook.createCellStyle(); cStyle = copyCellStyle(cStyle, sourceCellStyle, sourceWorkBook); cellStyleList.add(cStyle); return cStyle; }
/** * 两个cellStyle是否相同 * @param cellStyle * @param sourceCellStyle * @param sourceWorkBook * @param destWorkBook * @return */ private static boolean isEqual(HSSFCellStyle cellStyle, HSSFCellStyle sourceCellStyle, HSSFWorkbook destWorkBook, HSSFWorkbook sourceWorkBook) { //判断换行样式是否一样 if(cellStyle.getWrapText() != sourceCellStyle.getWrapText()){ return false; } //对齐方式是否一样 if(cellStyle.getAlignment() != sourceCellStyle.getAlignment()){ return false; } if(cellStyle.getVerticalAlignment()!= sourceCellStyle.getVerticalAlignment()){ return false; } //边框是否一样 if(cellStyle.getBorderBottom()!= sourceCellStyle.getBorderBottom()){ return false; } if(cellStyle.getBorderLeft()!= sourceCellStyle.getBorderLeft()){ return false; } if(cellStyle.getBorderRight()!= sourceCellStyle.getBorderRight()){ return false; } if(cellStyle.getBorderTop()!= sourceCellStyle.getBorderTop()){ return false; } if(cellStyle.getBottomBorderColor()!= sourceCellStyle.getBottomBorderColor()){ return false; } if(cellStyle.getLeftBorderColor()!= sourceCellStyle.getLeftBorderColor()){ return false; } if(cellStyle.getRightBorderColor()!= sourceCellStyle.getRightBorderColor()){ return false; } if(cellStyle.getTopBorderColor()!= sourceCellStyle.getTopBorderColor()){ return false; } //字体是否一样 HSSFFont sourceFont = sourceCellStyle.getFont(sourceWorkBook); HSSFFont destFont = cellStyle.getFont(destWorkBook); if(destFont.getBoldweight() != sourceFont.getBoldweight()){ return false; } if(destFont.getCharSet() != sourceFont.getCharSet()){ return false; } if(destFont.getColor() != sourceFont.getColor()){ return false; } if(destFont.getColor() != sourceFont.getColor()){ return false; } if(destFont.getFontHeight() != sourceFont.getFontHeight()){ return false; } if(destFont.getFontHeightInPoints() != sourceFont.getFontHeightInPoints()){ return false; } if(destFont.getIndex() != sourceFont.getIndex()){ return false; } if(destFont.getItalic() != sourceFont.getItalic()){ return false; } if(destFont.getUnderline() != sourceFont.getUnderline()){ return false; } if(destFont.getStrikeout() != sourceFont.getStrikeout()){ return false; } if(!destFont.getFontName().equals(sourceFont.getFontName())){ return false; } //别的样式是否一样 return true; }
/** * 样式拷贝 * @param cStyle 目标style * @param sourceCellStyle 源style * @param sourceWorkBook 源workBook * @return */ private static HSSFCellStyle copyCellStyle(HSSFCellStyle cStyle, HSSFCellStyle sourceCellStyle, HSSFWorkbook sourceWorkBook) { if(sourceCellStyle == null || cStyle == null){ return cStyle; } //是否换行 cStyle.setWrapText(sourceCellStyle.getWrapText()); //字体拷贝 cStyle.setFont(sourceCellStyle.getFont(sourceWorkBook));// cStyle.cloneStyleFrom(sourceCellStyle); //拷贝对齐方式 cStyle.setAlignment(sourceCellStyle.getAlignment()); cStyle.setVerticalAlignment(sourceCellStyle.getVerticalAlignment()); //边框拷贝 cStyle.setBorderBottom(sourceCellStyle.getBorderBottom()); cStyle.setBorderLeft(sourceCellStyle.getBorderLeft()); cStyle.setBorderRight(sourceCellStyle.getBorderRight()); cStyle.setBorderTop(sourceCellStyle.getBorderTop()); cStyle.setBottomBorderColor(sourceCellStyle.getBottomBorderColor()); cStyle.setLeftBorderColor(sourceCellStyle.getLeftBorderColor()); cStyle.setRightBorderColor(sourceCellStyle.getRightBorderColor()); cStyle.setTopBorderColor(sourceCellStyle.getTopBorderColor()); //别的样式的拷贝 return cStyle; }
/** * 处理公式 * @param pPOIFormula * @return */ private static String parseFormula(String pPOIFormula) { final String cstReplaceString = "ATTR(semiVolatile)"; //$NON-NLS-1$ StringBuffer result = null; int index; result = new StringBuffer(); index = pPOIFormula.indexOf(cstReplaceString); if (index >= 0) { result.append(pPOIFormula.substring(0, index)); result.append(pPOIFormula.substring(index + cstReplaceString.length())); } else { result.append(pPOIFormula); } return result.toString(); }
POI Excel合并 分页符的复制相关推荐
- POI Excel 合并数据相同的行
import java.io.Serializable;/*** POI Excel报表导出,列合并实体<br>* * @author WQ**/ public class PoiMode ...
- 【Java】poi | excel | 合并单元格
一.说明 1.maven项目 2.基于ruoyi-fast 二.解决方案 1.合并行 需求:合并第一行和第二行 解决: CellRangeAddress region = new CellRangeA ...
- java poi excel 视图-分页浏览 效果
今天接了个需求,要求导出excel的时候excel默认显示为分页浏览(分页视图模式) 发现HSSFSheet 和HSSFWorkbook 相关的api不支持(没有找到相关方法),上网查了一圈总结如下: ...
- java poi excel合并单元格 相同的列以及在有父级约束条件下合并二级列
import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress;public clas ...
- java SXSSF 导出excel 合并单元格,设置打印分页
官方poi地址: Busy Developers' Guide to HSSF and XSSF Features HSSFWorkbook.XSSFWorkbook.SXSSFWorkbook的区别 ...
- Java Excel 复制单元格 poi Excel 复制单元格 Java Excel 复制行 Java Excel 复制 sheet 页 poi excel copy
Java Excel 复制单元格 poi Excel 复制单元格 Java Excel 复制行 Java Excel 复制 sheet 页 一.前言 1.本文记录 poi excel 实现 单元格ce ...
- POI:使用XSSFWorkbook与SXSSFWorkbook在处理Excel合并时容易出现的问题
最近在做的需求中需要将两个Excel合并. 首先讲下POI中处理Excel的几种方式吧. 1.HSSFWorkbook,用来处理.xls后缀的Excel,即适用于Excel2003以前(包括2003) ...
- Java POI Excel移动行和复制行的处理
目录 Java POI Excel移动行和复制行的处理 坑点: 实现的代码 Java POI Excel移动行和复制行的处理 POI操作Excel时,不支持移动行的操作,因此在需要通过复制行+删除行+ ...
- Excel拼接后的字符串复制文本到word中出现换行符
Excel拼接后的字符串复制文本到word中出现换行符 现象描述 Excel拼接字符串 word中粘贴出现换行 解决办法 现象描述 需要将几百个数据需要放入代码中. 将excel中的数据拼接好后,粘贴 ...
- vs中列表分页符代码_电脑办公技巧Excel中Ctrl+K的使用技巧(十二)/Word2016中快速删除分页符与空白页的方法...
ctrl+k是超链接的快捷键,你可以批量创建超链接,学会使用这个方法你可以快速在文件之间快速跳跃了,无比方便快捷,感兴趣的小伙伴一起看下去吧! 1.在Excel中打开另一个Excel文件(其他类型文件 ...
最新文章
- 马云:很多P2P公司披着互联网金融的外衣做非法金融服务
- [CQOI2014]和谐矩阵
- 3D 投影矩阵学习1
- JVM内存管理------杂谈(借此也论一论obj=null)
- 牛客网暑期ACM多校训练营(第二场)J	farm (二维树状数组)
- [MySQL FAQ]系列 -- 账号密码包含反斜线时怎么办
- Tensorflow 循环神经网络 文本情感分析概述02
- X86汇编语言从实模式到保护模式10:进入保护模式
- ajax显示失败信息,javascript
- 洛谷——P1304 哥德巴赫猜想
- 关于合格工程师素养的一些思考
- MyEclipse出现红色感叹号解决办法
- uniapp将h5链接打包成安卓
- html百度站内搜索代码,网站添加百度站内搜索的教程
- 页面置换算法详解(10种)
- 实力验证:金蝶EAS 8.2授权注册 (包含Apusic 9999连接数破解)
- 知到网课大学生安全文化考试试题|真题|题库(含答案)
- java打印设置_java如何设置系统默认打印机
- Alpine中文字体
- 红糖水白糖水的转换--郝汉森
热门文章
- android 4.4 root精灵,ROOT精灵: 支持安卓4.3/4.4机型一键ROOT
- 输入等值线参数绘制等值线图python_专题复习:等值线(上)
- 框架设计--第六章 初识MyBatis--习题答案
- 软件设计与体系结构实验——图书馆管理系统
- 【小技巧】如何将PPT的图保持高分辨率导入到Word中
- 广州坐标系转换大地2000_实用帖 | 从地方坐标系到2000国家大地坐标系的转换方法...
- 深度学习之CNN反向传播
- redis 客户端连接及常用命令使用
- 41个机器学习面试题
- polyval polyvalm