Tillämpa skuggning på alternerande rader och kolumner med villkorlig formatering
Applicera skuggning på alternerande rader och kolumner med hjälp av villkorlig formatering
Denna artikel använder Excels inbyggda funktioner såsom ROW, COLUMN & MOD. Här är lite detaljer om dessa funktioner för en bättre förståelse av kodsnutten som presenteras nedan.
- ROW()-funktionen returnerar radnumret för en cellreferens. Om referensen utelämnas antar den att referensen är celladressen där ROW-funktionen har infogats.
- Funktionen COLUMN() returnerar kolumnnumret för en cellreferens. Om referensen utelämnas antar den att referensen är celladressen där COLUMN-funktionen har infogats.
- MOD()-funktionen returnerar resten efter att ett nummer har delats av en divisor, där det första parametern till funktionen är det numeriska värdet vars rest du vill hitta och det andra parametern är det tal som används för att dela in i nummerparametern. Om divisorn är 0 kommer den att returnera felen #DIV/0!.
Låt oss börja skriva lite kod för att uppnå målet med hjälp av Aspose.Cells for Java API.
// 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"); |
Följande ögonblicksbild visar det resulterande kalkylarket som är laddat i Excel-applikationen.
För att applicera nyanser på alternativa kolumner, behöver du bara ändra formeln =MOD(RAD(),2)=0 till =MOD(KOLUMN(),2)=0, det vill säga; istället för att få radindexet, modifiera formeln för att hämta kolumnindexet. Den resulterande kalkylbladet kommer i detta fall se ut som den följande bilden.