如何使用Apache POI获取Excel文件中单元格的条件格式?

时间:2013-12-10 17:44:35

标签: java apache-poi

我有一个xlsx文件,它已经有几个条件格式规则。 如何从单元格中获取格式规则? 我想“读取”现有单元格中的所有规则并将它们应用于新的规则,我将创建

谢谢!

SheetConditionalFormatting rules = sheet.getSheetConditionalFormatting(); 
Cell cell = sheet.createRow(18).createCell(1); 
CellReference cr = new CellReference(cell.getRowIndex(), cell.getColumnIndex()); 
CellRangeAddress[] range = {CellRangeAddress.valueOf("B19")};
ConditionalFormatting rule = rules.getConditionalFormattingAt(0);
rules.addConditionalFormatting(range, rule.getRule(0)); 

但我希望能够像这样做:

cell.getFormattingRules();

我找到了答案。当我在表格中得到所有规则时,我可以得到这个规则的范围。而现在我需要检查,我的细胞是否在这个范围内。 感谢大家!

2 个答案:

答案 0 :(得分:1)

我有相同的要求,我用几个功能解决了它。如果您发现任何错误,请告诉我。

它将"复制"来自" styleCell"的条件格式样式到"单元格"

    private void expandConditionalFormatting(XSSFCell cell, XSSFCell styleCell){
      XSSFSheetConditionalFormatting xscf = cell.getSheet().getSheetConditionalFormatting();
      for(int idx = 0;idx<xscf.getNumConditionalFormattings();idx++){
         XSSFConditionalFormatting cf = xscf.getConditionalFormattingAt(idx);
         List<CellRangeAddress> cra = Arrays.asList(cf.getFormattingRanges());
         List<CellRangeAddress> newCra = new ArrayList();
         for(CellRangeAddress c:cra){
            if(containsCell(c, styleCell) && !containsCell(c,cell)){
               newCra.add(new CellRangeAddress(Math.min(c.getFirstRow(), cell.getRowIndex()),Math.max(c.getLastRow(),cell.getRowIndex()),Math.min(c.getFirstColumn(), cell.getColumnIndex()),Math.max(c.getLastColumn(),cell.getColumnIndex())));
            } else{
               newCra.add(c);
            }
         }
         ArrayList<XSSFConditionalFormattingRule> cfs = new ArrayList();
         for(int ci=0;ci<cf.getNumberOfRules();ci++){
            cfs.add(cf.getRule(ci));
         }

         xscf.addConditionalFormatting(newCra.toArray(new CellRangeAddress[newCra.size()]),cfs.toArray(new XSSFConditionalFormattingRule[cfs.size()]));
         xscf.removeConditionalFormatting(idx);
      }
   }
   private boolean containsCell(CellRangeAddress cra, Cell cell){
      if(cra.getFirstRow()<=cell.getRowIndex() && cra.getLastRow()>=cell.getRowIndex()){
         if(cra.getFirstColumn()<=cell.getColumnIndex() && cra.getLastColumn()>=cell.getColumnIndex()){
            return true;
         }
      }
      return false;
   }

答案 1 :(得分:0)

谢谢你这个例子, 你可以有一个不同的approche来扩展公式如下: 您必须确切知道orgin公式的范围

private void expandConditionalFormatting(XSSFSheet sheet, CellRangeAddress cellRangeSourceAddress,
        CellRangeAddress cellRangeDestinationAddress) {
    XSSFSheetConditionalFormatting conditions = sheet.getSheetConditionalFormatting();
    for (int idx = 0; idx < conditions.getNumConditionalFormattings(); idx++) {
        XSSFConditionalFormatting cf = conditions.getConditionalFormattingAt(idx);
        List<CellRangeAddress> cra = Arrays.asList(cf.getFormattingRanges());
        List<CellRangeAddress> newCra = new ArrayList<CellRangeAddress>();
        for (CellRangeAddress c : cra) {
            if (cellRangeSourceAddress.getFirstColumn() == c.getFirstColumn()
                    && cellRangeSourceAddress.getLastColumn() == c.getLastColumn()
                    && cellRangeSourceAddress.getFirstRow() == c.getFirstRow()
                    && cellRangeSourceAddress.getLastRow() == c.getLastRow()) {
                newCra.add(cellRangeDestinationAddress);
            } 
        }
        ArrayList<XSSFConditionalFormattingRule> cfs = new ArrayList<XSSFConditionalFormattingRule>();
        for (int ci = 0; ci < cf.getNumberOfRules(); ci++) {
            cfs.add(cf.getRule(ci));
        }
        if (newCra.size() > 0) {
            conditions.addConditionalFormatting(newCra.toArray(new CellRangeAddress[newCra.size()]),
                    cfs.toArray(new XSSFConditionalFormattingRule[cfs.size()]));
            conditions.removeConditionalFormatting(idx);
        }
    }
}