条件格式可在Excel 2007中工作,但不能在Excel 2016中工作

时间:2019-04-17 15:16:22

标签: java excel apache-poi

我处于按如下公式进行条件格式设置的情况;

=COUNTIF($A14,"*~*")>0

对于单元格E14至E38。

现在,这在Excel 2007中可以很好地工作,但在Excel 2016中根本不能工作。 我检查了类似的XML文件;

<conditionalFormatting sqref="E14:E38">
    <cfRule priority="1" dxfId="0" type="expression">
        <formula>COUNTIF($A14,"*~*")>0</formula>
    </cfRule>
</conditionalFormatting>

即使我删除规则并再次添加它也无法正常工作。我尝试了其他规则,但是对于那些单元格,条件格式根本无法工作。

仅供参考,我已经设置了打印区域,并且在打印区域内的条件格式不适用于任何单元,但是在打印区域外的条件格式化效果很好。

所有这些问题仅在Excel 2016中存在。

N.B。我正在使用Apache POI创建excel文件

基本上,我将条件格式从一次Excel工作表复制到另一个。代码如下;

    private void copyConditionalFormattingRules(Sheet source,Sheet destination){

        try{
            SheetConditionalFormatting sscf= source.getSheetConditionalFormatting();
            SheetConditionalFormatting dscf= destination.getSheetConditionalFormatting();
            //SheetConditionalFormatting dscf0= ((XSSFSheet)destination).getSheetConditionalFormatting();

            // delete existng formattings,if any
            for(int i=0;i<dscf.getNumConditionalFormattings();i++){

                dscf.removeConditionalFormatting(i);                    
            }

            //looping over all conditional formattings
            for(int i=0;i<sscf.getNumConditionalFormattings();i++){

                ConditionalFormatting cf = sscf.getConditionalFormattingAt(i);
                ConditionalFormatting cf1 = sscf.getConditionalFormattingAt(i);


                CellRangeAddress[] range = cf.getFormattingRanges();

                for(int j=0;j<cf.getNumberOfRules();j++){

                    //getting various properties from source
                    ConditionalFormattingRule rule = cf.getRule(j);
                    String formula1 = rule.getFormula1();

                    //creating new rule for destination sheet
                    ConditionalFormattingRule newRule = dscf.createConditionalFormattingRule(formula1);

                    try{
                        //Get pattern formatting
                        PatternFormatting pattern = rule.getPatternFormatting();
                        PatternFormatting newPattern = newRule.createPatternFormatting();

                        //retrieve pattern information
                        Color bgcolor = pattern.getFillBackgroundColorColor();
                        Color fgcolor = pattern.getFillForegroundColorColor();
                        short bgcolor1 = pattern.getFillBackgroundColor();
                        short fgcolor1 = pattern.getFillForegroundColor();
                        short fillPattern = pattern.getFillPattern();

                        if(bgcolor != null)
                            newPattern.setFillBackgroundColor(bgcolor);

                        if(bgcolor1 != 0)
                            newPattern.setFillBackgroundColor(bgcolor1);

                        if(fgcolor != null)
                            newPattern.setFillForegroundColor(fgcolor);

                        if(fgcolor1 != 0)
                            newPattern.setFillBackgroundColor(fgcolor1);

                        if(fillPattern != 0)
                            newPattern.setFillPattern(fillPattern);
                    }catch(Exception e){
                        e.printStackTrace();
                    }


                    try{
                        //Get font formatting
                        FontFormatting font = rule.getFontFormatting();

                        FontFormatting newFont = newRule.createFontFormatting();

                        if(font != null) {
                            //retrieve font information
                            int colorIndex = font.getFontColorIndex();
                            Color fontColor = font.getFontColor();
                            int fontHeight = font.getFontHeight();
                            short ulType = font.getUnderlineType();


                            if(colorIndex != 0)
                                newFont.setFontColorIndex((short) colorIndex);

                            if(fontColor != null){
                                //currently font color not working
                                //https://stackoverflow.com/questions/45611870/can-not-set-font-color-in-conditional-formatting-cell-via-apache-poi
                                try{
                                    newFont.setFontColor(fontColor);
                                }catch(Exception e){

                                }
                            }

                            if(fontHeight != 0)
                                newFont.setFontHeight(fontHeight);

                            if(ulType != 0)
                                newFont.setUnderlineType(ulType);
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }


                    try{
                        //Get border formatting
                        BorderFormatting borderFormatting = rule.getBorderFormatting();
                        BorderFormatting newBorderFormatting = newRule.createBorderFormatting();

                        if(borderFormatting != null) {
                            //retrieve border information
                            int bBottom = borderFormatting.getBorderBottom();
                            int bLeft = borderFormatting.getBorderLeft();
                            int bDiagonal = borderFormatting.getBorderDiagonal();
                            int bTop = borderFormatting.getBorderTop();
                            int bRight = borderFormatting.getBorderRight();
                            int bBottomColor = borderFormatting.getBottomBorderColor();
                            int bTopColor = borderFormatting.getTopBorderColor();
                            int bRightColor = borderFormatting.getRightBorderColor();
                            int bLeftColor = borderFormatting.getLeftBorderColor();
                            int bDiagonalColor = borderFormatting.getDiagonalBorderColor();


                            if(bBottom != 0)
                                newBorderFormatting.setBorderBottom((short) bBottom);

                            if(bTop != 0)
                                newBorderFormatting.setBorderBottom((short) bTop);

                            if(bLeft != 0)
                                newBorderFormatting.setBorderBottom((short) bLeft);

                            if(bRight != 0)
                                newBorderFormatting.setBorderBottom((short) bRight);

                            if(bDiagonal != 0)
                                newBorderFormatting.setBorderBottom((short) bDiagonal);

                            if(bBottomColor != 0)
                                newBorderFormatting.setBottomBorderColor((short) bBottomColor);

                            if(bTopColor != 0)
                                newBorderFormatting.setTopBorderColor((short) bTopColor);

                            if(bLeftColor != 0)
                                newBorderFormatting.setLeftBorderColor((short) bLeftColor);

                            if(bRightColor != 0)
                                newBorderFormatting.setRightBorderColor((short) bRightColor);

                            if(bDiagonalColor != 0)
                                newBorderFormatting.setDiagonalBorderColor((short) bDiagonalColor);
                        }
                    }catch(Exception e){
                        e.printStackTrace();
                    }

                    //--------------------------------End of getting------------------------------

                    //setting properties from source


                    dscf.addConditionalFormatting(range,newRule);
                }
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }

任何帮助对我来说都是非常有益的。如果您需要更多详细信息,请告诉我。提前致谢。

0 个答案:

没有答案