Technischer Artikel

HotXLS: streaming write and server batch jobs in Delphi

Dieser deutsche Artikel behandelt HotXLS: streaming write and server batch jobs in Delphi für Teams, die mit Delphi, C++Builder, Lazarus/FPC und losLab-Komponenten arbeiten

Der Fokus liegt auf praxisnahen Entscheidungen, Fallstricken und Prüfpunkten, damit die Lösung im produktiven Einsatz verlässlich bleibt

Architekturentscheidungen

Treat every workbook as an isolated job. job identifier, output destination, retry policy, timeout, and cancellation behavior / temporary file location, cleanup, quota, and retention for failed workbooks

  • job identifier, output destination, retry policy, timeout, and cancellation behavior
  • temporary file location, cleanup, quota, and retention for failed workbooks
  • batch size, concurrency level, progress reporting, and back-pressure
  • which validation can run before final upload or delivery

Implementierungsablauf

Stream output only after validation checkpoints are defined. The order below keeps the workflow reviewable for Delphi and C++Builder teams.

  1. assign a job identifier and output boundary before opening the workbook writer
  2. prepare styles, sheets, and headers before streaming row data
  3. write rows in batches and checkpoint progress for operators
  4. validate size, sheet count, row count, and key formulas before final delivery
  5. clean or retain temporary files according to success, cancellation, or failure

Validierungsnachweise

Batch evidence for operators. Keep these fields with the output or support record.

  • job identifier, batch item count, row count, output size, elapsed time, and memory peak
  • stage timings for data load, workbook write, validation, save, and upload
  • temporary path, cleanup status, retry count, and cancellation reason
  • operator-facing failure reason and whether retry is safe

Streaming is a resource policy

Streaming write helps scale workbook generation, but it also limits which parts of the workbook can be revisited later. The job design should decide what must be known before streaming begins.

Implementierungshinweise für die Produktion

Behandle HotXLS: streaming write and server batch jobs in Delphi als klaren Servicevertrag rund um die HotXLS-Aufrufe, mit getrennten Schritten für Eingabeprüfung, Arbeitsmappenaufbau, Ausgabekontrolle und Support-Evidenz

  • Datenquelle, Zellbereiche und Ausgabeformat festlegen, bevor die Arbeitsmappe erzeugt wird
  • Zeilenanzahl, Blattanzahl, Warnungen und Ausgabepfad in ein prüfbares Support-Protokoll schreiben
  • Anwendungsspezifische Details in testbare Helper kapseln, statt sie in UI-Ereignissen zu verteilen
  • Die gespeicherte Datei erneut öffnen oder prüfen, bevor sie an ein anderes System oder an Kunden geht

Fehlerfälle, die getestet werden sollten

  • Ein erfolgreicher SaveAs-Aufruf beweist noch nicht, dass der fachliche Vertrag stimmt
  • Schriftarten, Rechte und regionale Einstellungen können auf Servern anders sein als auf Entwicklerrechnern
  • Logs dürfen keine Passwörter, Kundendaten oder internen Links offenlegen

Ausführliches Delphi-Beispiel

Das folgende Beispiel zeigt eine praktische Servicegrenze für dieses Thema und hält Policy, Logging und Validierung testbar getrennt

procedure RunWorkbookBatch(const Jobs: TArray<TWorkbookJob>);
var
  Job: TWorkbookJob;
  JobResult: TWorkbookJobResult;
begin
  for Job in Jobs do
  begin
    StartJobAudit(Job.Id, Job.OutputFile);
    try
      RequireWritableDestination(Job.OutputFile);
      RequireTempQuota(Job.TempFolder, Job.ExpectedRows);
      JobResult := WriteWorkbookJob(Job);
      ValidateWorkbookForDelivery(JobResult.OutputFile, JobResult.ExpectedRows);
      CompleteJobAudit(Job.Id, JobResult);
    except
      on E: Exception do
      begin
        MarkJobFailed(Job.Id, E.Message, CanRetryWorkbookJob(Job));
        CleanupOrRetainTempFiles(Job, E);
        raise;
      end;
    end;
  end;
end;

function WriteWorkbookJob(const Job: TWorkbookJob): TWorkbookJobResult;
var
  Wb: TXLSXWorkbook;
  Sh: IXLSWorksheet;
begin
  Wb := TXLSXWorkbook.Create;
  try
    Sh := Wb.Sheets[0];
    Sh.Name := 'Batch Output';
    StreamRowsIntoWorksheet(Sh, Job.Reader, Job.Progress);
    WriteBatchMetricsSheet(Wb, Job);
    if Wb.SaveAs(Job.OutputFile) <> 1 then
      RaiseWorkbookSaveError(Job.OutputFile);
    Result := BuildWorkbookJobResult(Job);
  finally
    Wb.Free;
  end;
end;

Produktionscheckliste

  • 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

Product documentation

HotXLS Component

Zusätzliche Codebeispiele

procedure TBulkExporter.FillRow(Sender: TObject; SheetIndex, Row, FirstCol,
  LastCol: Integer; var Values: Variant; var Skip: Boolean;
  var Cancel: Boolean);
begin
  if not FReader.Next then
  begin
    Cancel := True;              // data source drained: stop cleanly
    Exit;
  end;
  Values := VarArrayCreate([FirstCol, LastCol], varVariant);
  Values[FirstCol]     := FReader.RecordId;
  Values[FirstCol + 1] := FReader.CustomerName;
  Values[FirstCol + 2] := FReader.Amount;
end;

// fill rows 2..100001, columns A..C, pulling from the reader
Sheet.WriteRows(2, 1, 100001, 3, FillRow);
for FileName in SourceFiles do
begin
  Book := TXLSXWorkbook.Create;        // fresh instance: no state bleed
  try
    Book.StreamingWrite := True;
    if Book.Open(FileName) <> 1 then
      Continue;                        // one bad input must not kill the batch
    Book.SaveAsCSV(ChangeFileExt(FileName, '.csv'), 0, ',');
  finally
    Book.Free;
  end;
end;