Twórz, edytuj, sprawdzaj, obliczaj i eksportuj skoroszyty Excel bezpośrednio z kodu Delphi lub C++Builder. HotXLS to natywna biblioteka Object Pascal z kodem źródłowym dla przepływów XLS i XLSX, przeznaczona do narzędzi desktopowych, zadań batch, systemów raportowych i generowania dokumentów po stronie serwera bez automatyzacji Microsoft Excel.
Ten artykuł jest przeznaczony dla developers generating workbooks whose calculated results are part of the product output. Traktuje formula calculation and custom functions jako produkcyjną inżynierię dokumentów, a nie pojedyncze wywołanie komponentu.
Praktyczne ryzyko polega na tym, że formula results can appear correct in simple cases while dependency order, custom functions, locale parsing, volatile functions, or error values differ in production. Dlatego przepływ wymaga spisanego kontraktu, obserwowalnej diagnostyki i realistycznych plików regresyjnych.
Decyzje architektoniczne
Treat calculation as a deterministic service. calculation mode, dependency refresh, and whether cached values are trusted / custom function names, argument types, null handling, and versioning
- calculation mode, dependency refresh, and whether cached values are trusted
- custom function names, argument types, null handling, and versioning
- locale handling for decimal separators, date literals, and function names
- error-value policy for invalid references, division by zero, and unsupported formulas
Przebieg implementacji
Register custom functions before loading dependent formulas. The order below keeps the workflow reviewable for Delphi and C++Builder teams.
- register custom functions and workbook names before calculation
- load or generate formulas with explicit dependency expectations
- calculate targeted ranges or full workbook output according to the profile
- compare critical results with independent fixtures or approved Excel output
- write calculation diagnostics into the support bundle when results are disputed
Dowody walidacji
Calculation evidence for finance and support. Keep these fields with the output or support record.
- formula count, dependency graph summary, custom function calls, and recalculation time
- critical cell addresses, formula text, cached value, calculated value, and error state
- custom function version, arguments, returned value, and exception mapping
- locale profile and unsupported-formula warnings
Custom functions need versioned behavior
A formula engine workflow should make dependencies, custom function registration, recalculation mode, cached results, and error handling visible. That is especially important when generated workbooks are consumed without Excel automation.
Uwagi wdrożeniowe dla produkcji
Traktuj HotXLS: formula calculation and custom functions in Delphi jako jawny kontrakt usługi wokół wywołań HotXLS, oddzielając walidację wejścia, zapis skoroszytu, kontrolę wyniku i dowody dla wsparcia
- Ustal źródło danych, zakresy komórek i format wyjściowy przed utworzeniem skoroszytu
- Zapisuj liczbę wierszy, arkusze, ostrzeżenia i ścieżkę wyjściową w możliwym do sprawdzenia logu
- Szczegóły aplikacji zamykaj w testowalnych helperach zamiast w zdarzeniach UI
- Otwórz lub sprawdź zapisany plik przed przekazaniem go do innego systemu lub klienta
Tryby awarii do przećwiczenia
- Udany SaveAs nie dowodzi, że kontrakt biznesowy nadal jest poprawny
- Czcionki, uprawnienia i ustawienia regionalne mogą różnić się między serwerem a maszyną deweloperską
- Logi nie mogą ujawniać haseł, danych klientów ani linków wewnętrznych
Szczegółowy przykład Delphi
Poniższy przykład Delphi pokazuje praktyczną granicę usługi dla tego tematu, z polityką, logowaniem i walidacją w warstwie możliwej do testowania
procedure RecalculateInvoiceWorkbook(const TemplateFile, OutputFile: string; const Orders: TArray<TOrderRow>);
var
Wb: TXLSXWorkbook;
Sh: IXLSWorksheet;
RowIndex: Integer;
Order: TOrderRow;
begin
Wb := TXLSXWorkbook.Create;
try
Wb.Open(TemplateFile);
RegisterInvoiceFunctions(Wb);
Sh := Wb.Sheets[0];
EnsureTemplateVersion(Wb, 'invoice-formulas-v3');
RowIndex := 2;
for Order in Orders do
begin
Sh.Range['A' + IntToStr(RowIndex)].Value := Order.Sku;
Sh.Range['B' + IntToStr(RowIndex)].Value := Order.Quantity;
Sh.Range['C' + IntToStr(RowIndex)].Value := Order.UnitPrice;
Sh.Range['D' + IntToStr(RowIndex)].Value := '=B' + IntToStr(RowIndex) + '*C' + IntToStr(RowIndex);
Sh.Range['E' + IntToStr(RowIndex)].Value := '=InvoiceTax(D' + IntToStr(RowIndex) + ')';
Inc(RowIndex);
end;
Wb.Calculate;
AssertFormulaRangeHasValues(Sh, 'D2:E' + IntToStr(RowIndex - 1));
WriteCalculationAudit(Wb, 'invoice-formulas', RowIndex - 2);
if Wb.SaveAs(OutputFile) <> 1 then
RaiseWorkbookSaveError(OutputFile);
finally
Wb.Free;
end;
end;
Lista produkcyjna
- 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