A variable-length order list creates two problems in an Excel template: new rows lose the sample row’s formatting, and the total row gets overwritten. This affects businesses that generate quotes, order reports, invoices, or inventory sheets from Java data using Axyra Sheets.
The most robust approach is to use Axyra Sheets template markers. The template row becomes a repeatable pattern, so each order receives the same borders and number formats while the total row moves below the generated records.
The problem
This issue came up in the Stack Overflow community when a workbook built with io.keikai:axyra-sheets, version 1.0.0-Beta, was populated from a Java list of order objects. The source question and its code examples are available in the original Stack Overflow discussion about repeating a formatted row.
The workbook contains:
- A header row with Product, Quantity, and Price.
- One formatted sample row.
- A total row with a
SUMformula. - A list whose number of records changes on each run.
Sheet.importData writes values into existing cells. It does not create enough rows or automatically copy the sample row’s borders and number formats. As a result, the second record can overwrite the total row, while later records appear without the original formatting.
The solution
Use Axyra Sheets’ built-in TemplateMarkers mechanism instead of treating the sample row as a fixed destination.
First, change the sample data row in the Excel template to use markers:
| Product | Quantity | Price |
|---|---|---|
${orders.product} | ${orders.quantity} | ${orders.price} |
| Total | ${orders.price:sum} |
Keep the desired borders, currency format, alignment, and other visual settings on the marker row.
The orders name connects the markers to the list passed from Java. The product, quantity, and price parts refer to fields on each order object. The :sum instruction tells Axyra Sheets to aggregate the price values for the total.
Then process the worksheet:
import io.keikai.axyra.sheets.template.TemplateMarkers;
try (Workbook wb = Workbook.open(Path.of("order-template.xlsx"))) {
TemplateMarkers.process(
wb.sheet("Orders"),
Map.of("orders", orders)
);
wb.recalculate();
wb.save(Path.of("orders.xlsx"));
}
For three records, Axyra Sheets repeats the marker row three times. It preserves the row formatting, moves the total row down, and fills the generated rows with the corresponding object values.
This approach also avoids relying on a fixed formula such as =SUM(C2:C2). That formula only covers one data row. ${orders.price:sum} is tied to the repeated list, so the total continues to match the number of records.
A manual alternative is possible when the workbook layout requires more control. Insert n - 1 rows below the sample row, copy the sample row’s style to each new row, import the data, and then rewrite the total formula for the expanded range. However, the template-marker approach is simpler for an existing report template with a changing number of orders.
How to apply it
- Open the existing
order-template.xlsxfile in Excel. - Keep the header row unchanged.
- Replace the sample values in the data row with these markers:
- Product:
${orders.product} - Quantity:
${orders.quantity} - Price:
${orders.price}
- Product:
- Preserve the formatting on that row. Apply borders to all required cells and set the Price cell to the desired currency format.
- Replace the fixed total formula with
${orders.price:sum}in the total cell. - Make sure the Java list uses fields that match the marker names. For the example, each object needs
product,quantity, andpricevalues. - Open the workbook and call
TemplateMarkers.process()on theOrderssheet. - Pass the list through
Map.of("orders", orders). The map key must match theordersprefix used in the template markers. - Call
wb.recalculate()before saving so the generated total is calculated in the output workbook. - Save the result to a new file, such as
orders.xlsx, rather than overwriting the original template. - Test the process with zero, one, and several orders. Pay special attention to the total row, currency formatting, and the position of any content below the order table.
If template markers are not suitable for a particular workbook, use the manual sequence instead: insert rows, copy the sample row’s style, import the records, and update the total formula. Confirm the exact method names in the Axyra Sheets version being used, because APIs such as row insertion and style copying can vary.
Implementation angle
This pattern can become a reliable report-generation service for quotes, sales orders, or inventory movements. An automation partner could connect a form, CRM, or database to Java code that validates each record, fills the Axyra Sheets template, and stores the finished workbook in the correct client folder. The same workflow could then send the report through Make or n8n, while Claude checks formulas and flags missing prices before the file reaches the customer.
Book an AI implementation engagement with Creator Scripts at creatorscripts.com to turn this Excel report process into a dependable business workflow, and browse the Creator Scripts resource library for more guides.
Frequently asked questions
How do I repeat a formatted Excel row for each Java list item?
Use Axyra Sheets template markers in the sample row, such as ${orders.product} and ${orders.price}. Call TemplateMarkers.process() with the list under the matching orders key.
How can I stop new Excel rows from overwriting the total row?
Use a repeatable marker row so Axyra Sheets can add a row for each record and move the total below them. Set the total cell to ${orders.price:sum} to sum the list’s prices.
How do I keep borders and number formats when generating Excel rows?
Apply the borders, alignment, and number formats to the marker row in the template. Axyra Sheets repeats that row’s formatting for the generated records.
No comments:
Post a Comment