Aspose.Cells FOSS for Java Use Cases
View as Markdown

Aspose.Cells FOSS for Java Use Cases

Aspose.Cells FOSS for Java covers a focused set of real-world Excel generation tasks on .xlsx workbooks — from one-off report generation to batch processing a folder of files. This guide walks through four common scenarios built entirely from the classes covered in the other how-to articles for this platform.

Step-by-Step Guide

Step 1: Install the Package

<dependency>
  <groupId>org.aspose</groupId>
  <artifactId>aspose-cells-foss</artifactId>
  <version>26.7.0</version>
</dependency>

Step 2: Generate a Styled Summary Report

Combine Cell.putValue(...) with Style to produce a small formatted report — a bold, currency-formatted total next to a plain label:

import org.aspose.cells_foss.Cell;
import org.aspose.cells_foss.Style;
import org.aspose.cells_foss.Workbook;
import org.aspose.cells_foss.Worksheet;

try (Workbook workbook = new Workbook()) {
    Worksheet sheet = workbook.getWorksheets().get(0);
    sheet.setName("Report");

    sheet.getCells().get("A1").putValue("Revenue");
    sheet.getCells().get("B1").putValue(12500.75);

    Cell total = sheet.getCells().get("B1");
    Style style = total.getStyle();
    style.getFont().setBold(true);
    style.setCustom("#,##0.00");
    total.setStyle(style);

    workbook.getDocumentProperties().setTitle("Monthly Revenue Summary");
    workbook.save("report.xlsx");
}

Step 3: Validate Submitted Data Before It Reaches a Sheet

Attach a whole-number validation rule to an input range so the saved file itself enforces the same constraint a form might apply before submission:

import org.aspose.cells_foss.CellArea;
import org.aspose.cells_foss.OperatorType;
import org.aspose.cells_foss.Validation;
import org.aspose.cells_foss.ValidationType;

int validationIndex = sheet.getValidations().add(new CellArea(1, 1, 20, 1));
Validation validation = sheet.getValidations().get(validationIndex);
validation.setType(ValidationType.WHOLE_NUMBER);
validation.setOperator(OperatorType.BETWEEN);
validation.setFormula1("0");
validation.setFormula2("1000");

Step 4: Highlight Values That Fall Outside an Expected Range

Layer conditional formatting on top of the same data so out-of-range values stand out visually when the sheet is opened, independent of the validation rule in Step 3:

import org.aspose.cells_foss.FormatCondition;
import org.aspose.cells_foss.FormatConditionCollection;
import org.aspose.cells_foss.FormatConditionType;

int cfIndex = sheet.getConditionalFormattings().add();
FormatConditionCollection conditions = sheet.getConditionalFormattings().get(cfIndex);
conditions.addArea(CellArea.createCellArea("B2", "B21"));
int condIndex = conditions.addCondition(
    FormatConditionType.CELL_VALUE, OperatorType.GREATER_THAN, "1000");
FormatCondition condition = conditions.get(condIndex);
Style highlight = condition.getStyle();
highlight.getFont().setBold(true);
condition.setStyle(highlight);

Step 5: Batch-Process a Folder of Workbooks

Iterate a list of input paths, apply the same transformation to each workbook, and save alongside a new filename — useful for normalizing a batch of spreadsheets exported from another system:

import java.util.List;

List<String> inputFiles = List.of("jan.xlsx", "feb.xlsx", "mar.xlsx");
for (String path : inputFiles) {
    try (Workbook workbook = new Workbook(path)) {
        for (Worksheet ws : workbook.getWorksheets()) {
            ws.getCells().getColumns().get(0).setWidth(20.0);
        }
        workbook.save(path.replace(".xlsx", "-formatted.xlsx"));
    }
}

Common Issues and Fixes

Batch processing one bad file stops the whole run. Wrap each iteration’s body in its own CellsException catch block so one corrupt or unreadable input doesn’t abort the remaining files:

for (String path : inputFiles) {
    try (Workbook workbook = new Workbook(path)) {
        workbook.save(path.replace(".xlsx", "-formatted.xlsx"));
    } catch (CellsException e) {
        System.err.println("Skipping " + path + ": " + e.getMessage());
    }
}

Conditional formatting and validation appear to conflict. They are independent features — a validation rule restricts what can be typed into a cell, while conditional formatting only changes appearance based on the current value. A value can fail validation visually highlighted by a conditional format rule at the same time; this is expected, not a bug.

Document properties set in a batch run don’t appear in every output file. Each Workbook instance has its own DocumentProperties — call getDocumentProperties() and set fields inside the per-file loop, not once outside it, if every output file should carry the same metadata.

Frequently Asked Questions

Can these scenarios be combined into a single workbook?

Yes — Steps 2 through 4 all operate on the same Worksheet and can be applied to the same workbook in sequence before a single Workbook.save(...) call.

Does batch processing require every input to be loaded into memory at once?

No. Each iteration in Step 5 opens, processes, and saves one Workbook inside its own try-with-resources block, so only one input’s data is held in memory at a time.

Is there a way to apply the same style object to multiple cells without rebuilding it each time?

Yes. Build one Style object, then call Cell.setStyle(style) on each target cell — the same Style instance can be reused as long as its properties are not mutated differently between assignments.

See Also

 English