使用poi复制sheet的内容到指定位置后,格式乱了!

fatkids 2017-03-23 07:26:27
各位大神好!
我再使用ecplise中使用poi复制sourcesheet到targetsheet后,原来写好的excel的格式乱了,这个是什么原因!
还请各位帮助看看,是什么方面的原因,非常感谢!
以下是我的代码:

public static void copySheet(HSSFSheet targetSheet, HSSFSheet sourceSheet,
HSSFWorkbook targetWork, HSSFWorkbook sourceWork, boolean copyStyle, int row_index)throws Exception {

if(targetSheet == null || sourceSheet == null || targetWork == null || sourceWork == null){
throw new IllegalArgumentException("调用PoiUtil.copySheet()方法时,targetSheet、sourceSheet、targetWork、sourceWork都不能为空,故抛出该异常!");
}

//复制源表中的行
int maxColumnNum = 0;

Map styleMap = (copyStyle) ? new HashMap() : null;

HSSFPatriarch patriarch = targetSheet.createDrawingPatriarch(); //用于复制注释
for (int i = sourceSheet.getFirstRowNum(); i <= sourceSheet.getLastRowNum(); i++) {
HSSFRow sourceRow = sourceSheet.getRow(i);
HSSFRow targetRow = targetSheet.createRow(i + row_index + 2);

if (sourceRow != null) {
copyRow(targetRow, sourceRow,
targetWork, sourceWork,patriarch, styleMap);
if (sourceRow.getLastCellNum() > maxColumnNum) {
maxColumnNum = sourceRow.getLastCellNum();
}
}
}

//复制源表中的合并单元格
mergerRegion(targetSheet, sourceSheet);

//设置目标sheet的列宽
for (int i = 0; i <= maxColumnNum; i++) {
targetSheet.setColumnWidth(i, sourceSheet.getColumnWidth(i));
}
}

public static void mergerRegion(HSSFSheet targetSheet, HSSFSheet sourceSheet)throws Exception {
if(targetSheet == null || sourceSheet == null){
throw new IllegalArgumentException("调用PoiUtil.mergerRegion()方法时,targetSheet或者sourceSheet不能为空,故抛出该异常!");
}

for (int i = 0; i < sourceSheet.getNumMergedRegions(); i++) {
org.apache.poi.ss.util.CellRangeAddress oldRange = sourceSheet.getMergedRegion(i);
CellRangeAddress newRange = new CellRangeAddress(
oldRange.getFirstRow(), oldRange.getLastRow(),
oldRange.getFirstColumn(), oldRange.getLastColumn());
targetSheet.addMergedRegion(newRange);
}
}

public static void copyRow(HSSFRow targetRow, HSSFRow sourceRow,
HSSFWorkbook targetWork, HSSFWorkbook sourceWork,HSSFPatriarch targetPatriarch, Map styleMap) throws Exception {
if(targetRow == null || sourceRow == null || targetWork == null || sourceWork == null || targetPatriarch == null){
throw new IllegalArgumentException("调用PoiUtil.copyRow()方法时,targetRow、sourceRow、targetWork、sourceWork、targetPatriarch都不能为空,故抛出该异常!");
}

//设置行高
targetRow.setHeight(sourceRow.getHeight());

for (int i = sourceRow.getFirstCellNum(); i <= sourceRow.getLastCellNum(); i++) {
HSSFCell sourceCell = sourceRow.getCell(i);
HSSFCell targetCell = targetRow.getCell(i);

if (sourceCell != null) {
if (targetCell == null) {
targetCell = targetRow.createCell(i);
}

//拷贝单元格,包括内容和样式
copyCell(targetCell, sourceCell, targetWork, sourceWork, styleMap);

//拷贝单元格注释
copyComment(targetCell,sourceCell,targetPatriarch);
}
}
}

public static void copyCell(HSSFCell targetCell, HSSFCell sourceCell, HSSFWorkbook targetWork, HSSFWorkbook sourceWork,Map styleMap) {
if(targetCell == null || sourceCell == null || targetWork == null || sourceWork == null ){
throw new IllegalArgumentException("调用PoiUtil.copyCell()方法时,targetCell、sourceCell、targetWork、sourceWork都不能为空,故抛出该异常!");
}

//处理单元格样式
if(styleMap != null){
if (targetWork == sourceWork) {
targetCell.setCellStyle(sourceCell.getCellStyle());
} else {
String stHashCode = "" + sourceCell.getCellStyle().hashCode();
HSSFCellStyle targetCellStyle = (HSSFCellStyle) styleMap
.get(stHashCode);
if (targetCellStyle == null) {
targetCellStyle = targetWork.createCellStyle();
targetCellStyle.cloneStyleFrom(sourceCell.getCellStyle());
styleMap.put(stHashCode, targetCellStyle);
}

targetCell.setCellStyle(targetCellStyle);
}
}

//处理单元格内容
switch (sourceCell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
targetCell.setCellValue(sourceCell.getRichStringCellValue());
break;
case HSSFCell.CELL_TYPE_NUMERIC:
targetCell.setCellValue(sourceCell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
targetCell.setCellType(HSSFCell.CELL_TYPE_BLANK);
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
targetCell.setCellValue(sourceCell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_ERROR:
targetCell.setCellErrorValue(sourceCell.getErrorCellValue());
break;
case HSSFCell.CELL_TYPE_FORMULA:
targetCell.setCellFormula(sourceCell.getCellFormula());
break;
default:
break;
}
}

public static void copyComment(HSSFCell targetCell,HSSFCell sourceCell,HSSFPatriarch targetPatriarch)throws Exception{
if(targetCell == null || sourceCell == null || targetPatriarch == null){
throw new IllegalArgumentException("调用PoiUtil.copyCommentr()方法时,targetCell、sourceCell、targetPatriarch都不能为空,故抛出该异常!");
}

//处理单元格注释
HSSFComment comment = sourceCell.getCellComment();
if(comment != null){
HSSFComment newComment = targetPatriarch.createComment(new HSSFClientAnchor());
newComment.setAuthor(comment.getAuthor());
newComment.setColumn(comment.getColumn());
newComment.setFillColor(comment.getFillColor());
newComment.setHorizontalAlignment(comment.getHorizontalAlignment());
newComment.setLineStyle(comment.getLineStyle());
newComment.setLineStyleColor(comment.getLineStyleColor());
newComment.setLineWidth(comment.getLineWidth());
newComment.setMarginBottom(comment.getMarginBottom());
newComment.setMarginLeft(comment.getMarginLeft());
newComment.setMarginTop(comment.getMarginTop());
newComment.setMarginRight(comment.getMarginRight());
newComment.setNoFill(comment.isNoFill());
newComment.setRow(comment.getRow());
newComment.setShapeType(comment.getShapeType());
newComment.setString(comment.getString());
newComment.setVerticalAlignment(comment.getVerticalAlignment());
newComment.setVisible(comment.isVisible());
targetCell.setCellComment(newComment);
}
}
...全文
284 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧