poi 规则copy


    /**
     * rowコピー
     * @param srcRow
     * @param desRow
     */
    private void copyRow(Row srcRow, Row desRow) {
        Iterator<Cell> it = srcRow.cellIterator();
        desRow.setHeight(srcRow.getHeight());
        while (it.hasNext()) {
            Cell srcCell = it.next();
            Cell desCell = desRow.createCell(srcCell.getColumnIndex());
            copyCell(srcCell, desCell);
        }
    }

    /**
     * cellコピー
     * @param srcCell
     * @param desCell
     */
    private void copyCell(Cell srcCell, Cell desCell) {
        desCell.setCellStyle(srcCell.getCellStyle());
        if (srcCell.getCellComment() != null) {
            desCell.setCellComment(srcCell.getCellComment());
        }
        // スタイルをコーヒー
        CellStyle newCellStyle = this.sheet.getWorkbook().createCellStyle();
        newCellStyle.cloneStyleFrom(srcCell.getCellStyle());
        newCellStyle.setBorderTop(srcCell.getCellStyle().getBorderBottomEnum());
        desCell.setCellStyle(newCellStyle);
        // 関数をコーヒー
        desCell.setCellValue(srcCell.getRichStringCellValue());
  if (srcCell.getCellType() == Cell.CELL_TYPE_FORMULA) {
            copyFormula(srcCell, desCell, this.sheet.getWorkbook());
        }
      
    }

private void copyFormula(Cell srcCell, Cell destCell, Workbook book) {
        // 相対参照で数式をコピーする
        // srcCell コピー元 destCell コピー先

        String formula = srcCell.getCellFormula();
        EvaluationWorkbook ew;
        FormulaRenderingWorkbook rw;
        Ptg[] ptgs;

        ew = XSSFEvaluationWorkbook.create((XSSFWorkbook) book);
        ptgs = FormulaParser.parse(formula, (XSSFEvaluationWorkbook) ew, FormulaType.CELL, 0);
        // 詳しいことはわからないが、最後のパラメータはシート番号を0始まりで指定
        rw = (XSSFEvaluationWorkbook) ew;

        for (Ptg ptg : ptgs) {
            // 座標の計算
            int shiftRows = destCell.getRowIndex() - srcCell.getRowIndex();
            int shiftCols = destCell.getColumnIndex() - srcCell.getColumnIndex();

            if (ptg instanceof RefPtgBase) {
                RefPtgBase ref = (RefPtgBase) ptg;

                if (ref.isColRelative()) {
                    ref.setColumn(ref.getColumn() + shiftCols);
                }

                if (ref.isRowRelative()) {
                    ref.setRow(ref.getRow() + shiftRows);
                }

            } else if (ptg instanceof AreaPtg) {
                AreaPtg ref = (AreaPtg) ptg;

                if (ref.isFirstColRelative()) {
                    ref.setFirstColumn(ref.getFirstColumn() + shiftCols);
                }

                if (ref.isLastColRelative()) {
                    ref.setLastColumn(ref.getLastColumn() + shiftCols);
                }

                if (ref.isFirstRowRelative()) {
                    ref.setFirstRow(ref.getFirstRow() + shiftRows);
                }

                if (ref.isLastRowRelative()) {
                    ref.setLastRow(ref.getLastRow() + shiftRows);
                }
            }
        }

        destCell.setCellFormula(FormulaRenderer.toFormulaString(rw, ptgs));
    }


    /**
     * ルールを設定
     * @param maxRow :最大の制定行
     */
    private void setConditionalRule(int maxRow) {
        // ルールの範囲
        Map<Integer, CellRangeAddress[]> rangesMap = new HashedMap<Integer, CellRangeAddress[]>();
        // ルール
        Map<Integer, List<ConditionalFormattingRule>> rulesMap = new HashedMap<Integer, List<ConditionalFormattingRule>>();

        SheetConditionalFormatting scf = sheet.getSheetConditionalFormatting();
        int countOfFormat = scf.getNumConditionalFormattings();
        // ルールのループ
        for (int i = 0; i < countOfFormat; i++) {
            List<ConditionalFormattingRule> ruleList = new ArrayList<ConditionalFormattingRule>();
            ConditionalFormatting format = scf.getConditionalFormattingAt(i);
            // ルールの範囲
            CellRangeAddress[] ranges = format.getFormattingRanges();
            rangesMap.put(i, ranges);

            // 範囲内のルール
            int numOfRule = format.getNumberOfRules();
            for (int j = 0; j < numOfRule; j++) {//範囲内は複数ルールがある
                ConditionalFormattingRule rule = format.getRule(j);
                ruleList.add(rule);
            }
            rulesMap.put(i, ruleList);
        }
        // 第一行によると、第二行からルールを設定
        for (Integer key : rangesMap.keySet()) {
            CellRangeAddress[] regions = rangesMap.get(key);
            CellRangeAddress[] newRegions = new CellRangeAddress[regions.length];
            for (int r = 0; r < regions.length; r++) { // 新しい範囲を設定
                CellRangeAddress region = new CellRangeAddress(regions[r].getFirstRow() + 1,
                        maxRow + regions[r].getFirstRow() - 1, regions[r].getFirstColumn(), regions[r].getLastColumn());
                newRegions[r] = region;
            }
            // ルールを設定
            List<ConditionalFormattingRule> ruleList = rulesMap.get(key);
            for (ConditionalFormattingRule rule : ruleList) {
                scf.addConditionalFormatting(newRegions, rule);
            }
        }
    }

    /**
     * データリストをコーヒー
     * @param maxRow
     */
    private void dataValidationCopy(int maxRow) {
        // シートにすべてのリストが取得
        List<? extends DataValidation> validations = sheet.getDataValidations();
        for (DataValidation validation : validations) {
            CellRangeAddressList addressList = validation.getRegions();
            if (null == addressList || addressList.getSize() == 0) {
                continue;
            }
            XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper((XSSFSheet) sheet);
            XSSFDataValidationConstraint dvConstraint = null;
            DataValidationConstraint constraint = validation.getValidationConstraint();
            String[] strs = constraint.getExplicitListValues();
            if (strs != null && strs.length > 0) { // リスト値を設定
                dvConstraint = (XSSFDataValidationConstraint) dvHelper.createExplicitListConstraint(strs);
            } else {// 元の値はその他シートのデータ時
                dvConstraint = (XSSFDataValidationConstraint) dvHelper
                        .createFormulaListConstraint(constraint.getFormula1());
            }
            // 最新の範囲を設定
            int firstRow = addressList.getCellRangeAddress(0).getFirstRow();
            int lastRow = maxRow + firstRow -1;
            int firstCol = addressList.getCellRangeAddress(0).getFirstColumn();
            int lastCol = addressList.getCellRangeAddress(0).getLastColumn();
            CellRangeAddressList addressList2 = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
            // 最新のデータリストを作成
            XSSFDataValidation validation2 = (XSSFDataValidation) dvHelper.createValidation(dvConstraint, addressList2);
            validation2.setSuppressDropDownArrow(validation.getSuppressDropDownArrow());
            validation2.setShowErrorBox(validation.getShowErrorBox());
            sheet.addValidationData(validation2);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值