Schattierung auf abwechselnden Zeilen und Spalten mit bedingter Formatierung anwenden

Anwendung von Schattierung auf alternative Zeilen & Spalten mithilfe bedingter Formatierung

Dieser Artikel verwendet integrierte Funktionen von Excel wie ZEILE, SPALTE & REST. Hier sind einige Details dieser Funktionen zur besseren Verständnis des vorangehenden Codes.

  • Die ZEILE()-Funktion gibt die Zeilennummer einer Zellreferenz zurück. Wenn die Referenz ausgelassen wird, nimmt sie an, dass die Referenz die Zelladresse ist, in die die ZEILE-Funktion eingegeben wurde.
  • Die SPALTE()-Funktion gibt die Spaltennummer einer Zellreferenz zurück. Wenn die Referenz ausgelassen wird, nimmt sie an, dass die Referenz die Zelladresse ist, in die die SPALTE-Funktion eingegeben wurde.
  • Die MOD()-Funktion gibt den Rest nach der Division einer Zahl durch einen Divisor zurück, wobei der erste Parameter der numerische Wert ist, von dem Sie den Rest finden möchten, und der zweite Parameter die Zahl ist, durch die die Nummernparameter dividiert werden. Wenn der Divisor 0 ist, wird der Fehler #DIV/0! zurückgegeben.

Lassen Sie uns einige Codezeilen schreiben, um das Ziel mithilfe der Aspose.Cells for Java API zu erreichen.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(ApplyShadingToAlternateRowsAndColumns.class);
/*
* Create an instance of Workbook Optionally load an existing spreadsheet by passing its stream or path to Workbook
* constructor
*/
Workbook book = new Workbook();
// Access the Worksheet on which desired rule has to be applied
Worksheet sheet = book.getWorksheets().get(0);
// Add FormatConditions to the instance of Worksheet
int index = sheet.getConditionalFormattings().add();
// Access the newly added FormatConditions via its index
FormatConditionCollection conditionCollection = sheet.getConditionalFormattings().get(index);
// Define a CellsArea on which conditional formatting will be applicable
CellArea area = CellArea.createCellArea("A1", "I20");
// Add area to the instance of FormatConditions
conditionCollection.addArea(area);
// Add a condition to the instance of FormatConditions. For this case, the condition type is expression, which is based on
// some formula
index = conditionCollection.addCondition(FormatConditionType.EXPRESSION);
// Access the newly added FormatCondition via its index
FormatCondition formatCondirion = conditionCollection.get(index);
// Set the formula for the FormatCondition. Formula uses the Excel's built-in functions as discussed earlier in this
// article
formatCondirion.setFormula1("=MOD(ROW(),2)=0");
// Set the background color and patter for the FormatCondition's Style
formatCondirion.getStyle().setBackgroundColor(Color.getBlue());
formatCondirion.getStyle().setPattern(BackgroundType.SOLID);
// Save the result on disk
book.save(dataDir + "output.xlsx");

Die folgende Momentaufnahme zeigt die resultierende Tabelle, die in der Excel-Anwendung geladen wird.

todo:image_alt_text

Um die Schattierung auf alternative Spalten anzuwenden, müssen Sie lediglich die Formel =MOD(ZEILE(),2)=0 durch =MOD(SPALTE(),2)=0 ersetzen, d.h. anstatt den Zeilenindex zu erhalten, ändern Sie die Formel, um den Spaltenindex abzurufen. Die resultierende Tabelle sieht in diesem Fall wie das folgende Bild aus.

todo:image_alt_text