Техническая статья

HotXLS Component: template-based report generation в Delphi

Создавайте, редактируйте, проверяйте, вычисляйте и экспортируйте книги Excel напрямую из кода Delphi или C++Builder. HotXLS — нативная библиотека Object Pascal с исходным кодом для рабочих процессов XLS и XLSX, предназначенная для настольных инструментов, batch-задач, систем отчетности и серверной генерации документов без автоматизации Microsoft Excel.

Эта статья предназначена для teams producing customer-facing workbooks from maintained Excel templates rather than assembling every cell in code. Она рассматривает template-based report generation как промышленную инженерию документов, а не как одиночный вызов компонента.

Практический риск состоит в том, что template-driven output becomes fragile when placeholder ownership, dynamic rows, styles, formulas, and localization are not versioned. Поэтому процессу нужны письменный контракт, наблюдаемая диагностика и реалистичные регрессионные файлы.

Архитектурные решения

Make templates part of the application interface. placeholder syntax, required values, repeated regions, and named-range ownership / style preservation, formula refresh, merged-cell behavior, and row insertion rules

  • placeholder syntax, required values, repeated regions, and named-range ownership
  • style preservation, formula refresh, merged-cell behavior, and row insertion rules
  • localization of labels, number formats, dates, and sheet names
  • template versioning, approval, rollback, and compatibility with old data profiles

Порядок реализации

Bind placeholders to typed data contracts. The order below keeps the workflow reviewable for Delphi and C++Builder teams.

  1. validate template version and required placeholders before loading data
  2. bind typed data to named ranges or placeholder regions
  3. expand repeating sections while preserving formulas, styles, merges, and page breaks
  4. calculate and validate key cells after data insertion
  5. store template version and placeholder results with the generated workbook

Доказательства проверки

Template evidence that speeds up support. Keep these fields with the output or support record.

  • template version, data profile, placeholder list, and missing-placeholder warnings
  • expanded region sizes, inserted rows, formula refresh status, and style preservation
  • localized label set and number-format profile
  • regression workbook comparison against approved samples

A template is executable design

A workbook template carries layout, formatting, formulas, and business assumptions. The code should treat placeholders, named ranges, and repeating regions as a documented contract rather than free-form cell addresses.

Заметки по промышленной реализации

Рассматривайте HotXLS Component: template-based report generation в Delphi как явный сервисный контракт вокруг вызовов HotXLS, разделяя проверку входных данных, запись книги, контроль результата и сведения для поддержки

  • Определите источник данных, диапазоны ячеек и формат вывода до создания книги
  • Записывайте число строк, листы, предупреждения и путь вывода в проверяемые сведения поддержки
  • Инкапсулируйте прикладные детали в тестируемые helper-функции, а не в события UI
  • Повторно откройте или проверьте сохраненный файл перед передачей другой системе или клиенту

Сценарии отказов для проверки

  • Успешный SaveAs не доказывает, что бизнес-контракт остался корректным
  • Шрифты, права и региональные настройки на сервере могут отличаться от машины разработчика
  • Журналы не должны раскрывать пароли, данные клиентов или внутренние ссылки

Подробный пример Delphi

Следующий пример Delphi показывает практическую границу сервиса для этой темы, где политика, журналирование и проверка остаются тестируемыми

procedure GenerateReportFromTemplate(const TemplateFile, OutputFile: string; const Context: TReportContext);
var
  Wb: TXLSXWorkbook;
begin
  RequireFileExists(TemplateFile);
  Wb := TXLSXWorkbook.Create;
  try
    Wb.Open(TemplateFile);
    EnsureTemplateVersion(Wb, Context.TemplateVersion);
    SetNamedValue(Wb, 'ReportTitle', Context.Title);
    SetNamedValue(Wb, 'ReportPeriod', Context.PeriodText);
    FillNamedTable(Wb, 'RevenueRows', Context.RevenueRows);
    FillNamedTable(Wb, 'ExpenseRows', Context.ExpenseRows);
    Wb.Calculate;

    AssertNoUnresolvedTemplateMarkers(Wb);
    AssertRequiredSheetsVisible(Wb, ['Cover', 'Summary', 'Detail']);
    WriteGenerationAudit(Wb, Context);

    if Wb.SaveAs(OutputFile) <> 1 then
      RaiseWorkbookSaveError(OutputFile);
  finally
    Wb.Free;
  end;
end;

Производственный чек-лист

  • Run the workflow on an empty workbook, a normal customer workbook, and a worst-case workbook
  • Open the output with the target spreadsheet application or downstream importer
  • Log product version, template version, profile, row count, output path, elapsed time, and warning count
  • Keep passwords, temporary files, customer data, and support bundles under explicit retention rules
  • Add regression workbooks when a customer file exposes a new edge case

Документация по продукту

HotXLS Component

Дополнительные примеры кода

const
  DetailRow = 10;            // the formatted sample row in the template
var
  I: Integer;
begin
  // Open space before the totals block first, so the SUM range
  // below the detail band stretches together with the data.
  if Length(Items) > 1 then
    Sheet.InsertRows(DetailRow + 1, Length(Items) - 1);

  for I := 0 to High(Items) do
  begin
    if I > 0 then              // clone styles + formulas from the sample row
      Sheet.CopyRange(DetailRow, 1, DetailRow, 5, DetailRow + I, 1);
    Sheet.Cells[DetailRow + I, 1].Value := Items[I].Name;
    Sheet.Cells[DetailRow + I, 2].Value := Items[I].Qty;
    Sheet.Cells[DetailRow + I, 3].Value := Items[I].UnitPrice;
    Sheet.Cells[DetailRow + I, 4].Formula :=
      Format('B%d*C%d', [DetailRow + I, DetailRow + I]);  // no '=' prefix
  end;
end;
var
  Total: Variant;
  LastDetail: Integer;
begin
  LastDetail := DetailRow + Length(Items) - 1;
  Total := Book.Calculate(Format('SUM(Invoice!D%d:D%d)',
    [DetailRow, LastDetail]));
  if (not VarIsNumeric(Total)) or
     (Abs(Total - ExpectedTotal) > 0.005) then
    raise Exception.Create('Invoice total does not match the order record');

  if Book.SaveAs('invoice-2026-0611.xlsx') <> 1 then
    raise Exception.Create('Save failed: check output path and permissions');
end;