Artykuł techniczny

HotXLS: conditional formatting, rich text, and styles

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 teams producing formatted workbook output that must remain editable and visually consistent. Traktuje conditional formatting, rich text, and styles jako produkcyjną inżynierię dokumentów, a nie pojedyncze wywołanie komponentu.

Praktyczne ryzyko polega na tym, że style-heavy workbooks become slow, bloated, or visually inconsistent when every cell creates a new style or when conditional rules overlap without priority control. Dlatego przepływ wymaga spisanego kontraktu, obserwowalnej diagnostyki i realistycznych plików regresyjnych.

Decyzje architektoniczne

Manage styles as reusable assets. style catalog, theme colors, indexed colors, fonts, borders, and number formats / conditional-rule priority, stop-if-true behavior, and target ranges

  • style catalog, theme colors, indexed colors, fonts, borders, and number formats
  • conditional-rule priority, stop-if-true behavior, and target ranges
  • rich text runs, hyperlink styling, and localization of inline labels
  • style reuse policy for generated rows and template-derived sections

Przebieg implementacji

Apply formatting through named profiles. The order below keeps the workflow reviewable for Delphi and C++Builder teams.

  1. define style profiles before writing large ranges
  2. reuse existing workbook styles when they match the intended appearance
  3. apply conditional rules in explicit priority order
  4. write rich text runs only where the mixed formatting carries meaning
  5. inspect style count and workbook size as part of regression testing

Dowody walidacji

Formatting evidence for regression review. Keep these fields with the output or support record.

  • style count, reused style identifiers, and newly created style profiles
  • conditional rule type, priority, target range, and formula or threshold
  • rich text run count, font changes, and hyperlink interactions
  • visual comparison against approved template output

Workbook formatting has a cost model

Conditional formatting, rich text runs, and cell styles are workbook resources. Reusing styles and explaining rule priority improves performance, file size, and long-term template maintenance.

Uwagi wdrożeniowe dla produkcji

Traktuj HotXLS: conditional formatting, rich text, and styles 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 BuildKpiWorkbook(const OutputFile: string; const Rows: TArray<TKpiRow>);
var
  Wb: TXLSXWorkbook;
  Sh: IXLSWorksheet;
  RowIndex: Integer;
  Row: TKpiRow;
begin
  Wb := TXLSXWorkbook.Create;
  try
    Sh := Wb.Sheets[0];
    Sh.Name := 'KPI Review';
    WriteHeaderRow(Sh, ['Team', 'Owner', 'Target', 'Actual', 'Status']);

    RowIndex := 2;
    for Row in Rows do
    begin
      Sh.Range['A' + IntToStr(RowIndex)].Value := Row.Team;
      Sh.Range['B' + IntToStr(RowIndex)].Value := Row.Owner;
      Sh.Range['C' + IntToStr(RowIndex)].Value := Row.Target;
      Sh.Range['D' + IntToStr(RowIndex)].Value := Row.Actual;
      Sh.Range['E' + IntToStr(RowIndex)].Value := Row.StatusText;
      Inc(RowIndex);
    end;

    Sh.Range['A1:E1'].ApplyBuiltinStyle(xbsTitle);
    ApplyCurrencyFormat(Sh, 'C2:D' + IntToStr(RowIndex - 1));
    AddTrafficLightRules(Sh, 'E2:E' + IntToStr(RowIndex - 1));
    AddRichTextStatusNotes(Sh, Rows, 2);
    ValidateStyleBudget(Wb, 80);

    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

Product documentation

HotXLS Component