Java “阿帕奇poi”;“拉下”;格式化

Java “阿帕奇poi”;“拉下”;格式化,java,apache-poi,Java,Apache Poi,我要求ApachePOI像excel中的“下拉”格式一样工作。因此,取一个示例行,获取每个单元格中的“格式”,并将其应用于下面的所有单元格。根据要求设置的格式包括数字格式和单元格的背景颜色,这些颜色根据值而变化。因此,我编写了一个类,从示例行的单元格中获取CellStyle,并根据需要应用它 public class FormatScheme implements ObjIntConsumer<Sheet> { private Map<Integer, CellSty

我要求ApachePOI像excel中的“下拉”格式一样工作。因此,取一个示例行,获取每个单元格中的“格式”,并将其应用于下面的所有单元格。根据要求设置的格式包括数字格式和单元格的背景颜色,这些颜色根据值而变化。因此,我编写了一个类,从示例行的单元格中获取
CellStyle
,并根据需要应用它

public class FormatScheme implements ObjIntConsumer<Sheet> {

    private Map<Integer, CellStyle> cellFormats = new LinkedHashMap<>();

    public static FormatScheme of(Row row, int xOffset){
        FormatScheme scheme = new FormatScheme();

        for (int i = xOffset; i < row.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            if(cell==null) continue;

            scheme.cellFormats.put(i, cell.getCellStyle());
        }

        return scheme;
    }


    @Override
    public void accept(Sheet sheet, int rowIndex) {
        Row row = sheet.getRow(rowIndex);
        if(row==null) row=sheet.createRow(rowIndex);
        Row finalRow = row;

        cellFormats.entrySet().forEach(entry -> {
            Cell cell = finalRow.getCell(entry.getKey());
            if(cell==null) cell= finalRow.createCell(entry.getKey());

            cell.setCellStyle(entry.getValue());
        });
    }


    private FormatScheme(){}
}
公共类FormatScheme实现ObjIntConsumer{
私有映射cellFormats=新LinkedHashMap();
的公共静态格式化方案(行,int-xOffset){
FormatScheme方案=新的FormatScheme();
for(int i=xOffset;i{
Cell Cell=finalRow.getCell(entry.getKey());
如果(cell==null)cell=finalRow.createCell(entry.getKey());
cell.setCellStyle(entry.getValue());
});
}
私有FormatScheme(){}
}
这似乎适用于数字格式,但无法抓住不断变化的背景颜色~我想,我错过了什么~


在Alex Richter的帮助下,我知道我需要使用工作表的
工作表条件格式
。如何获取当前应用于单元格的
条件格式设置
,并向下扩展影响范围?

您的问题并不十分清楚。但我怀疑您想要将格式从一个目标行复制到多个相邻的后续行。您还需要扩展条件格式规则的范围,以便下面相邻行中的单元格也遵循该规则。因此,如果您选择一行,然后单击“格式画师”,然后选择多个相邻的行,则与
Excel
的格式画师所做的相同

如何复制单元格样式,您已经有了。但为什么这么复杂?将单元格样式从一个单元格复制到另一个单元格是一行代码:
targetCell.setCellStyle(sourceCell.getCellStyle())

其次,我们也应该复制可能的行样式。下面的示例有一个方法
void copyRowStyle(Row sourceRow,Row targetRow)

要扩展条件格式规则的范围,我们需要获取应用于单元格的规则。规则存储在图纸级别。因此,我们需要遍历
SheetConditionalFormat
,以获取单元格所在范围内的规则。下面的示例有一个方法
List getConditionalFormattingsForCell(Cell Cell)

有了这些,我们可以扩展条件格式规则的范围。下面的示例有一个方法
void expandConditionalFormatting(Cell sourceCell,Cell targetCell)
。它将条件格式规则的范围从
sourceCell
扩展到
targetCell

完整的示例显示了原理:

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.ConditionalFormatting;
import org.apache.poi.ss.util.*;
 
import java.io.FileInputStream;
import java.io.FileOutputStream;
 
import java.util.List;
import java.util.ArrayList;
 
class ExcelCopyFormatting {
 
 static List<ConditionalFormatting> getConditionalFormattingsForCell(Cell cell) {
  List<ConditionalFormatting> conditionalFormattingList = new ArrayList<ConditionalFormatting>();
  Sheet sheet = cell.getRow().getSheet();
  SheetConditionalFormatting sheetConditionalFormatting = sheet.getSheetConditionalFormatting();
  for (int i = 0; i < sheetConditionalFormatting.getNumConditionalFormattings(); i++) {
   ConditionalFormatting conditionalFormatting = sheetConditionalFormatting.getConditionalFormattingAt(i);
   CellRangeAddress[] cellRangeAddressArray = conditionalFormatting.getFormattingRanges();
   for (CellRangeAddress cellRangeAddress : cellRangeAddressArray) {
    if (cellRangeAddress.isInRange(cell)) {
     conditionalFormattingList.add(conditionalFormatting);
    }
   }
  }
  return conditionalFormattingList;
 }
 
 static void expandConditionalFormatting(Cell sourceCell, Cell targetCell) {
  List<ConditionalFormatting> conditionalFormattingList = getConditionalFormattingsForCell(sourceCell);
  for (ConditionalFormatting conditionalFormatting : conditionalFormattingList) {
   CellRangeAddress[] cellRangeAddressArray = conditionalFormatting.getFormattingRanges();
   for (int i = 0; i < cellRangeAddressArray.length; i++) {
    CellRangeAddress cellRangeAddress = cellRangeAddressArray[i];
    if (cellRangeAddress.isInRange(sourceCell)) {
     if (cellRangeAddress.getFirstRow() > targetCell.getRowIndex()) {
      cellRangeAddress.setFirstRow(targetCell.getRowIndex());
     }
     if (cellRangeAddress.getFirstColumn() > targetCell.getColumnIndex()) {
      cellRangeAddress.setFirstColumn(targetCell.getColumnIndex());
     }
     if (cellRangeAddress.getLastRow() < targetCell.getRowIndex()) {
      cellRangeAddress.setLastRow(targetCell.getRowIndex());
     }
     if (cellRangeAddress.getLastColumn() < targetCell.getColumnIndex()) {
      cellRangeAddress.setLastColumn(targetCell.getColumnIndex());
     }
     cellRangeAddressArray[i] = cellRangeAddress;
    }
   }
   conditionalFormatting.setFormattingRanges(cellRangeAddressArray);
  }
 }
 
 static void copyRowStyle(Row sourceRow, Row targetRow) {
  if (sourceRow.isFormatted()) {
   targetRow.setRowStyle(sourceRow.getRowStyle());
  }
 }

 static void copyCellStyle(Cell sourceCell, Cell targetCell) {
  targetCell.setCellStyle(sourceCell.getCellStyle());
 }

 static void copyFormatting(Sheet sheet, int fromRow, int upToRow) {
  Row sourceRow = sheet.getRow(fromRow);
  for (int r = fromRow + 1; r <= upToRow; r++) {
   Row targetRow = sheet.getRow(r);
   if (targetRow == null) targetRow = sheet.createRow(r); 
   copyRowStyle(sourceRow, targetRow);
   for (Cell sourceCell : sourceRow) {
    Cell targetCell = targetRow.getCell(sourceCell.getColumnIndex());
    if (targetCell == null) targetCell = targetRow.createCell(sourceCell.getColumnIndex());
    copyCellStyle(sourceCell, targetCell);
    if (r == upToRow) {
     if (getConditionalFormattingsForCell(sourceCell).size() > 0) {
      expandConditionalFormatting(sourceCell, targetCell);
     }
    }
   }
  }
 }
 
 public static void main(String[] args) throws Exception {
  //Workbook workbook = WorkbookFactory.create(new FileInputStream("./Workbook.xls")); String filePath = "./WorkbookNew.xls";
  Workbook workbook = WorkbookFactory.create(new FileInputStream("./Workbook.xlsx")); String filePath = "./WorkbookNew.xlsx";
 
  Sheet sheet = workbook.getSheetAt(0);
 
  copyFormatting(sheet, 1, 9); // copy formatting from row 2 up to row 10
 
  FileOutputStream out = new FileOutputStream(filePath);
  workbook.write(out);
  out.close();
  workbook.close();
 }
}
import org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.usermodel.ConditionalFormatting;
导入org.apache.poi.ss.util.*;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.util.List;
导入java.util.ArrayList;
类ExcelCopyFormatting{
静态列表getConditionalFormattingsForCell(单元格){
列表条件FormattingList=新建ArrayList();
工作表=cell.getRow().getSheet();
SheetConditionalFormatting SheetConditionalFormatting=sheet.getSheetConditionalFormatting();
对于(int i=0;itargetCell.getRowIndex()){
cellRangeAddress.setFirstRow(targetCell.getRowIndex());
}
if(cellRangeAddress.getFirstColumn()>targetCell.getColumnIndex()){
cellRangeAddress.setFirstColumn(targetCell.getColumnIndex());
}
if(cellRangeAddress.getLastRow()