技術記事

HotXLS Component: Delphi での conditional formatting, rich text, and styles

Delphi または C++Builder コードから直接 Excel ワークブックを作成、編集、検査、計算、エクスポートできます。HotXLS はソース付き Object Pascal スプレッドシートライブラリで、XLS/XLSX ワークフロー、デスクトップツール、バッチジョブ、レポート、Excel 自動化なしのサーバー側生成に適しています。

この記事は teams producing formatted workbook output that must remain editable and visually consistent 向けです。conditional formatting, rich text, and styles を単なるコンポーネント呼び出しではなく、本番向けのドキュメントエンジニアリングとして扱います。

実務上のリスクは style-heavy workbooks become slow, bloated, or visually inconsistent when every cell creates a new style or when conditional rules overlap without priority control です。そのため、明確な契約、観測可能な診断、実際の顧客ファイルに近い回帰サンプルが必要です。

アーキテクチャ上の判断

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

実装フロー

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

検証エビデンス

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.

本番実装の要点

HotXLS Component: Delphi での conditional formatting, rich text, and styles は HotXLS 呼び出しの前後に置く明確なサービス契約として扱い、入力検証、ブック作成、出力確認、サポート証跡を分離します

  • ブックを作成する前にデータソース、セル範囲、出力形式を確定する
  • 行数、シート数、警告、出力先をレビュー可能な証跡として記録する
  • アプリ固有の処理は UI イベントではなくテスト可能な helper に閉じ込める
  • 保存済みファイルを別システムや顧客へ渡す前に再オープンまたは検査する

事前に試験すべき失敗パターン

  • SaveAs の成功だけでは業務契約が正しいとは言えません
  • サーバーと開発機ではフォント、権限、地域設定が異なることがあります
  • ログにパスワード、顧客データ、内部リンクを残してはいけません

詳しい Delphi サンプル

次の Delphi 例は、このテーマをサービス境界として実装する形を示し、ポリシー、ログ、検証をテスト可能な層に分けます

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;

本番チェックリスト

  • 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