对原创做了一点修改,
public class ExcelUtil {
/**
intPosition:复制到指定的行数
**/
public static void copyRows(Workbook wb, Sheet sourceSheet,
Sheet targetSheet, int intStartRow, int intEndRow, int intPosition) {
// EXECL中的行是从1开始的,而POI中是从0开始的,所以这里要减1.
int pStartRow = intStartRow - 1;
int pEndRow = intEndRow - 1;
int pPosition = intPosition - 1;
Row sourceRow = null;
Row targetRow = null;
Cell sourceCell = null;
Cell targetCell = null;
// Sheet sourceSheet = null;
// Sheet targetSheet = null;
CellRangeAddress region = null;
int cType;
int i;
int j;
int targetRowFrom;
int targetRowTo;
if ((pStartRow == -1) || (pEndRow == -1)) {
return;
}
// sourceSheet = wb.getSheetAt(pSourceSheet);
// targetSheet = wb.getSheetAt(pTargetSheet);
System.out.println(sourceSheet.getNumMergedRegions());
//拷贝合并的单元格
for (i = 0; i < sourceSheet.getNumMergedRegions(); i++) {
region = sourceSheet.getMergedRegion(i);
if ((region.getFirstRow() >= pStartRow)&&(region.getLastRow() <= pEndRow)) {
targetRowFrom = region.getFirstRow() - pStartRow + pPosition;
targetRowTo = region.getLastRow() - pStartRow + pPosition;
// region.setFirstRow(targetRowFrom);
// region.setLastRow(targetRowTo);
CellRangeAddress targetregion = new CellRangeAddress(targetRowFrom,targetRowTo,region.getFirstColumn(),region.getLastColumn());
targetSheet.addMergedRegion(targetregion);
}
}
//设置列宽
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;
}
}
//拷贝行并填充数据
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);
targetCell.setCellStyle(sourceCell.getCellStyle());
cType = sourceCell.getCellType();
targetCell.setCellType(cType);
switch (cType) {
case Cell.CELL_TYPE_BOOLEAN:
targetCell.setCellValue(sourceCell.getBooleanCellValue());
System.out.println("--------TYPE_BOOLEAN:"+ targetCell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_ERROR:
targetCell.setCellErrorValue(sourceCell.getErrorCellValue());
System.out.println("--------TYPE_ERROR:"+ targetCell.getErrorCellValue());
break;
case Cell.CELL_TYPE_FORMULA:
targetCell.setCellFormula(parseFormula(sourceCell.getCellFormula()));
System.out.println("--------TYPE_FORMULA:"+targetCell.getCellFormula());
break;
case Cell.CELL_TYPE_NUMERIC:
targetCell.setCellValue(sourceCell.getNumericCellValue());
System.out.println("--------TYPE_NUMERIC:"+targetCell.getNumericCellValue());
break;
case Cell.CELL_TYPE_STRING:
targetCell.setCellValue(sourceCell.getRichStringCellValue());
System.out.println("--------TYPE_STRING:" +i+ targetCell.getRichStringCellValue());
break;
}
}
}
}
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();
}
public String getStringCellValue(Cell cell)
{
String strCell = "";
if(cell == null)
return "";
switch(cell.getCellType()){
case Cell.CELL_TYPE_STRING :
strCell = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_NUMERIC :
if(DateUtil.isCellDateFormatted(cell))
strCell = cell.getDateCellValue().toString();
else
// strCell = String.valueOf(cell.getNumericCellValue());
strCell = new DecimalFormat("#").format(cell.getNumericCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN :
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA :
strCell = cell.getCellFormula();
break;
case Cell.CELL_TYPE_BLANK :
strCell = "";
break;
default:
strCell = "";
break;
}
if(strCell == null || strCell.equals(""))
return "";
return strCell;
}
}