poi操作excel,复制sheet,复制⾏,复制单元格
项⽬中,我们经常使⽤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&NumCellStyles();i++){
//  HSSFCellStyle CellStyleAt(i);
//  HSSFCellStyle CellStyleAt(i);
//  if(toStyle==null){
//    ateCellStyle();
//  }
//  copyCellStyle(fromStyle,toStyle);
//  }
// }
/**
* 复制⼀个单元格样式到⽬的单元格样式
* @param fromStyle
* @param toStyle
*/
htmlborderpublic static void copyCellStyle(HSSFCellStyle fromStyle,
HSSFCellStyle toStyle) {
toStyle.Alignment());
//边框和边框颜⾊
toStyle.BorderBottom());
toStyle.BorderLeft());
toStyle.BorderRight());
toStyle.BorderTop());
toStyle.TopBorderColor());
toStyle.BottomBorderColor());
toStyle.RightBorderColor());
toStyle.LeftBorderColor());
//背景和前景
toStyle.FillBackgroundColor());
toStyle.FillForegroundColor());
toStyle.DataFormat());
toStyle.FillPattern());
//  toStyle.Font(null));
toStyle.Hidden());
toStyle.Indention());//⾸⾏缩进
toStyle.Locked());
toStyle.Rotation());//旋转
toStyle.VerticalAlignment());
toStyle.WrapText());
}
/**
* 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 = wIterator(); rowIt.hasNext();) {
HSSFRow tmpRow = (HSSFRow) ();
HSSFRow newRow = RowNum());
//⾏复制
copyRow(wb,tmpRow,newRow,copyValueFlag);
}
}
/**
* ⾏复制功能
* @param fromRow
* @param toRow
*/
public static void copyRow(HSSFWorkbook wb,HSSFRow fromRow,HSSFRow toRow,boolean copyValueFlag){  for (Iterator cellIt = llIterator(); cellIt.hasNext();) {
HSSFCell tmpCell = (HSSFCell) ();
HSSFCell newCell = CellNum());
copyCell(wb,tmpCell, newCell, copyValueFlag);
}
}
/**
* 复制原有sheet的合并单元格到新创建的sheet
*
* @param sheetCreat 新创建sheet
* @param sheet      原有的sheet
*/
public static void mergerRegion(HSSFSheet fromSheet, HSSFSheet toSheet) {
int sheetMergerCount = NumMergedRegions();
for (int i = 0; i < sheetMergerCount; i++) {
Region mergedRegionAt = MergedRegionAt(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 ateCellStyle();
CellStyle(), newstyle);
distCell.Encoding());
//样式
distCell.setCellStyle(newstyle);
//评论
if (CellComment() != null) {
distCell.CellComment());
}
// 不同数据类型处理
int srcCellType = CellType();
distCell.setCellType(srcCellType);
if (copyValueFlag) {
if (srcCellType == HSSFCell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(srcCell)) {
distCell.DateCellValue());
} else {
distCell.NumericCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_STRING) {    distCell.RichStringCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_BLANK) {
// nothing21
} else if (srcCellType == HSSFCell.CELL_TYPE_BOOLEAN) {    distCell.BooleanCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_ERROR) {    distCell.ErrorCellValue());
} else if (srcCellType == HSSFCell.CELL_TYPE_FORMULA) {    distCell.CellFormula());
} else { // nothing29
}
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。