ExcelUtil 工具类

public class ExcelUtil {
    /**
     intPosition:复制到指定的行数
     **/
    public static void copyRows(XSSFWorkbook wb, XSSFSheet sourceSheet,
                                XSSFSheet targetSheet, int intStartRow, int intEndRow, int intPosition) {

        // EXECL中的行是从1开始的,而POI中是从0开始的,所以这里要减1.

        int pStartRow = intStartRow - 1;
        int pEndRow = intEndRow - 1;
        int pPosition = intPosition - 1;
        XSSFRow sourceRow = null;
        XSSFRow targetRow = null;
        XSSFCell sourceCell = null;
        XSSFCell targetCell = null;
//			 Sheet sourceSheet = null;
//			Sheet targetSheet = null;
        CellRangeAddress region = null;
        CellType cType;
        int i;
        int j;
        int targetRowFrom;
        int targetRowTo;


        if ((pStartRow == -1) || (pEndRow == -1)) {

            return;

        }

//			 sourceSheet = wb.getSheetAt(pSourceSheet);
//			 targetSheet = wb.getSheetAt(pTargetSheet);

        //拷贝合并的单元格
        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 BOOLEAN:
                        targetCell.setCellValue(sourceCell.getBooleanCellValue());
                        break;
                    case ERROR:
                        targetCell.setCellErrorValue(sourceCell.getErrorCellValue());
                        break;
                    case FORMULA:
                        targetCell.setCellFormula(parseFormula(sourceCell.getCellFormula()));
                        break;
                    case _NONE:
                        break;
                    case NUMERIC:
                        targetCell.setCellValue(sourceCell.getNumericCellValue());
                        break;
                    case STRING:
                        targetCell.setCellValue(sourceCell.getRichStringCellValue());
                        break;
                    case BLANK:
                        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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值