Currency, percentages, and dates exported as text can make a reconciliation report hard to calculate in Excel. With HotXLS in Delphi, write amounts and rates as numbers and calendar dates as date variants, then apply built-in cell styles and number formats to give the XLSX a readable layout while keeping the data available to formulas.
A visible structure for the worksheet
Use a built-in cell style for headings and status labels. TXLSXRange.ApplyBuiltinStyle applies font attributes and a fill when the preset defines one, so a call such as ApplyBuiltinStyle(xbsHeading1) gives a whole row of labels one appearance. Reserve xbsBad for a visible review label. That also suits an exception you already identified in code. The preset supplies both colors. If the color should react when somebody changes an amount in Excel, use a conditional formatting rule instead. The word "Review" in the worksheet still conveys the status when someone prints in black and white.
procedure WriteReportLabels(Sheet: TXLSXWorksheet);
begin
Sheet.Cells[1, 1].Value := 'Reconciliation adjustments';
Sheet.Cells[2, 1].Value := 'Adjustment';
Sheet.Cells[2, 2].Value := 'Amount';
Sheet.Cells[2, 3].Value := 'Rate';
Sheet.Cells[2, 4].Value := 'Posting date';
Sheet.Cells[2, 5].Value := 'Date serial';
Sheet.Cells[3, 1].Value := 'New charge';
Sheet.Cells[4, 1].Value := 'Review credit';
Sheet.Cells[5, 1].Value := 'No adjustment';
Sheet.Cells[6, 1].Value := 'Total';
Sheet.Range['A1'].ApplyBuiltinStyle(xbsTitle);
Sheet.Range['A2:E2'].ApplyBuiltinStyle(xbsHeading1);
Sheet.Range['A4'].ApplyBuiltinStyle(xbsBad);
Sheet.Range['A6:B6'].ApplyBuiltinStyle(xbsTotal);
Sheet.ColWidth[1] := 28;
Sheet.ColWidth[2] := 16;
Sheet.ColWidth[3] := 12;
Sheet.ColWidth[4] := 16;
Sheet.ColWidth[5] := 16;
end;
Apply the presets to the small areas that carry meaning. A title and a total serve different readers, and coloring every line makes it harder to find the credit that needs review. ApplyBuiltinStyle only copies appearance. It writes the preset's font and, when the preset has one, its fill onto the range. No Excel named style is created, and the cells are not attached to one. When a template already defines a style by name, apply it with ApplyNamedStyle, which resolves that name in the workbook. Classic XLS uses matching presets, the xbc constants, on a classic range or cell, and C++Builder code finds the declarations for both engines in the lxHandleX.hpp and lxHandle.hpp headers.
How do Excel currency and percentage formats keep cells numeric?
Write the number to TXLSXCell.Value. The pattern goes in as a string. TXLSXRange.SetNumberFormat looks the pattern up in the workbook's number formats, adds it when it is new, and applies the resulting index to every cell in the range, so the stored value never changes. For a single cell, assign the string to its NumberFormat property. Apply the pattern to the entire amount column for the rows you write, including the total, so that later edits to any of those cells pick up the display rules. If you handle indexes yourself, remember that NumberFormats.Add returns a zero-based position in the collection while SetNumberFormatIndex expects a one-based index.
procedure WriteReportAmounts(Sheet: TXLSXWorksheet);
begin
Sheet.Cells[3, 2].Value := 1234.5;
Sheet.Cells[4, 2].Value := -987.25;
Sheet.Cells[5, 2].Value := 0;
Sheet.Cells[3, 3].Value := 0.375;
Sheet.Cells[4, 3].Value := 0.125;
Sheet.Cells[5, 3].Value := 0;
Sheet.Cells[6, 2].Formula := 'SUM(B3:B5)';
Sheet.Range['B3:B6'].SetNumberFormat('"$"#,##0.00;[Red]("$"#,##0.00);"-";@');
Sheet.Range['C3:C5'].SetNumberFormat('0.0%');
end;
The pattern has four sections. They cover positive numbers, negative numbers, zero, and text. In a US Excel display, the charge should read $1,234.50, the credit should read ($987.25) in red, and the zero should show a dash. SUM(B3:B5) still receives three numbers and gives 247.25. Excel displays 0.375 as 37.5%, so an upstream field that supplies 37.5 to mean 37.5 percent has to be divided by 100 when it is imported. The pattern assumes a fraction. An input of 37.5 therefore displays as 3750.0%. The details of omitted sections and literal text are covered in Excel number format sections.
One call covers grouped integers, scientific notation, and fractions. Pick the pattern for the meaning of the column. A volume total can use #,##0, while a laboratory measurement may be easier to read with 0.00E+00. In a sheet that works in eighths, # ??/?? can show 0.375 as 3/8. To make decimal precision part of an accounting rule, round the actual value at the point the business rule calls for. With standard full-precision calculation, a format that displays two decimal places still leaves formulas using the underlying amount.
What changes when the workbook uses the 1904 date system?
TXLSXWorkbook.Date1904 selects the epoch that HotXLS uses when it saves typed calendar dates. Set it before putting dates into the report. Keep the 1900 system for new workbooks unless the report has to share its date system with an older template. For June 29, 2026, the expected Excel serial is 46202 in the 1900 system and 44740 in the 1904 system. That gap of 1,462 days explains why a date can jump by about four years when an application copies a raw serial into a workbook with a different epoch. Formatting both cells as yyyy-mm-dd will still leave the shared number tied to two different dates. Microsoft Support explains the two Excel date systems.
Use VarFromDateTime(EncodeDate(...)) to give HotXLS a date variant that carries the calendar value. The date pattern in column D makes the value readable while keeping it available for calculations. Column E shows the serial itself. Its reference to D3, with the number pattern 0, gives the saved file a place to inspect the serial without changing the date formatting in column D. Keep that helper column in a validation workbook if the regular report only needs to show the date. Excel date serials and date-format detection goes deeper into reading dates from workbooks produced by other applications.
// Add SysUtils, Variants, and lxHandleX to the uses clause.
procedure SaveAdjustmentReport(const OutputFile: string; Use1904: Boolean);
var
Book: TXLSXWorkbook;
Sheet: TXLSXWorksheet;
begin
Book := TXLSXWorkbook.Create;
try
Book.Date1904 := Use1904;
Sheet := Book.Sheets.Add('Adjustments');
WriteReportLabels(Sheet);
WriteReportAmounts(Sheet);
Sheet.Cells[3, 4].Value := VarFromDateTime(EncodeDate(2026, 6, 29));
Sheet.Cells[4, 4].Value := VarFromDateTime(EncodeDate(2026, 6, 30));
Sheet.Range['D3:D4'].SetNumberFormat('yyyy-mm-dd');
Sheet.Cells[3, 5].Formula := 'D3';
Sheet.Cells[3, 5].NumberFormat := '0';
if Book.Recalculate <> 1 then
raise Exception.Create('Could not recalculate the report');
if Book.SaveAs(OutputFile) <> 1 then
raise Exception.CreateFmt('Could not save %s', [OutputFile]);
finally
Book.Free;
end;
end;
What Excel receives when the XLSX is reopened
Open the saved file first. Look at the dates in a fresh TXLSXWorkbook. In memory, a reference to a date can still return a date variant, and that value only answers which day it is. The saved file answers how the workbook represents that day. A cell whose number format has date tokens comes back as a date variant, and the formula cell in column E comes back as the serial under its 0 pattern. Reading the date in column D next to the serial in column E catches two independent problems, a date that is rendered with the wrong value and a workbook that uses an unexpected epoch. The amounts, the rate, and the retained SUM formula can be printed in one pass.
// Add SysUtils, Variants, and lxHandleX to the uses clause.
procedure PrintSavedAdjustmentReport(const OutputFile: string);
var
Book: TXLSXWorkbook;
Sheet: TXLSXWorksheet;
PostingDate: Variant;
begin
Book := TXLSXWorkbook.Create;
try
if Book.Open(OutputFile) <> 1 then
raise Exception.CreateFmt('Could not open %s', [OutputFile]);
Sheet := Book.Sheets[1];
PostingDate := Sheet.Cells[3, 4].Value;
Writeln('Date1904=', BoolToStr(Book.Date1904, True));
Writeln('B3=', VarToStr(Sheet.Cells[3, 2].Value));
Writeln('C3=', VarToStr(Sheet.Cells[3, 3].Value));
Writeln('B6 formula=', Sheet.Cells[6, 2].Formula);
Writeln('B6=', VarToStr(Sheet.Cells[6, 2].Value));
Writeln('D3 is a date=', BoolToStr(VarIsDate(PostingDate), True));
Writeln('D3=', FormatDateTime('yyyy-mm-dd', VarToDateTime(PostingDate)));
Writeln('E3=', VarToStr(Sheet.Cells[3, 5].Value));
finally
Book.Free;
end;
end;
Expected output for PrintSavedAdjustmentReport('adjustments-1900.xlsx'), the file that SaveAdjustmentReport writes with False. A second run with True and the name adjustments-1904.xlsx differs in two lines, Date1904=True and E3=44740, so column D still holds 2026-06-29 while the serial moves by 1,462.
Date1904=False
B3=1234.5
C3=0.375
B6 formula=SUM(B3:B5)
B6=247.25
D3 is a date=True
D3=2026-06-29
E3=46202
In an unused Excel cell, =ISNUMBER(C3) should return TRUE and =C3*100 should give 37.5 in General format. To verify how the report is presented, open the file in the viewer your users rely on and check the widths, credit styling, and date display there. A correct numerical total and a clean layout answer different questions.
Adjusting the format for an existing template
An amount that comes back as text is the first thing to follow. Trace it to the assignment to Value. A correctly typed amount with the wrong appearance points to the formatting call. In that case, read the reopened cell's NumberFormat property and compare the pattern with the one you intended to assign. An index is harder to check, because it is only a position in that workbook's collection. A report exporter that reuses one index with a different template can pick up a different pattern, even when both workbooks use an identical amount column. Passing the pattern string to SetNumberFormat sidesteps that, since the lookup happens in the workbook that owns the range, and an index kept from NumberFormats.Add should stay local to the workbook that returned it.
A template may already carry the right fonts, borders, and fill colors. Write values into its existing cells and apply SetNumberFormat only to the range whose display requires a change. The range call updates the number-format part of the cell style. For example, switching a rate column to the pattern 0.00% shows the same 0.375 as 37.50% and keeps the template's other formatting. Read Date1904 first. Assign the incoming calendar date as a date variant so the workbook retains the epoch already used by its existing dates.
Keep the reusable part of the exporter small. Let the source data decide which value belongs in a cell, and keep the formatting rules with the report layout. That leaves room to reuse the same calculation for a plain numeric extract, an Excel workbook with currency formatting, or a revised template with different styling. The HotXLS Delphi spreadsheet component provides the file APIs for both XLSX and classic XLS.