HotXLS 文件

TXLSXRange class

單元:lxHandleX
表示 a rectangular XLSX worksheet range returned by TXLSXWorksheet.Range, RCRange, UsedRange, EntireRow, or EntireColumn. A range is a lightweight view over its worksheet; it does not own cell data

Declaration

TXLSXBuiltinCellStyle = (
  xbsNormal, xbsGood, xbsBad, xbsNeutral,
  xbsCalculation, xbsCheckCell, xbsExplanatoryText, xbsInput,
  xbsLinkedCell, xbsNote, xbsOutput, xbsWarningText,
  xbsHeading1, xbsHeading2, xbsHeading3, xbsHeading4, xbsTitle, xbsTotal,
  xbsAccent1, xbsAccent2, xbsAccent3, xbsAccent4, xbsAccent5, xbsAccent6);

TXLSXRange = class
  constructor Create(ASheet: TXLSXWorksheet; ARow1, ACol1, ARow2, ACol2: Integer);
  function RefA1: WideString;
  function SaveAsHTML(const FileName: WideString): Integer; overload;
  function SaveAsHTML(const FileName: WideString; Options: TXLSXHtmlExportOptions): Integer; overload;
  function SaveAsHTML(Stream: TStream): Integer; overload;
  function SaveAsHTML(Stream: TStream; Options: TXLSXHtmlExportOptions): Integer; overload;

  property Sheet: TXLSXWorksheet;
  property Row1, Col1, Row2, Col2: Integer;
  property RowCount, ColCount: Integer;

  procedure SetValue(const V: Variant);
  procedure SetFormula(const F: WideString);
  function SetArrayFormula(const F: WideString): Integer;
  procedure ClearValues;
  procedure Clear;
  procedure ClearAll;

  procedure SetFontIndex(AIdx: Integer);
  procedure SetFillIndex(AIdx: Integer);
  procedure SetBorderIndex(AIdx: Integer);
  procedure SetNumberFormatIndex(AIdx: Integer);
  procedure SetAlignmentIndex(AIdx: Integer);
  procedure SetNumberFormat(const Fmt: WideString);
  procedure SetBorders(Kind: TXLSXBorderEdgeKind; Style: TXLSXBorderStyle); overload;
  procedure SetBorders(Kind: TXLSXBorderEdgeKind; Style: TXLSXBorderStyle; Color: LongWord); overload;
  procedure SetLocked(ALocked: Boolean);
  procedure SetFormulaHidden(AHidden: Boolean);

  procedure AutoFitColumns(AMinWidth: Double = 0; AMaxWidth: Double = 0);
  procedure AutoFitRows(AMinHeight: Double = 0; AMaxHeight: Double = 0);
  procedure Merge; overload;
  procedure Merge(Across: OleVariant); overload;
  procedure Unmerge;
  function Offset(DRow, DCol: Integer): TXLSXRange;
  function Resize(NewRowCount, NewColCount: Integer): TXLSXRange;
  procedure Sort(AKeyColumn: Integer; ADescending: Boolean = False); overload;
  procedure Sort(const AKeyColumns: array of Integer; const ADescending: array of Boolean); overload;
  procedure ApplyBuiltinStyle(AStyle: TXLSXBuiltinCellStyle);
  procedure CopyTo(ADestRow, ADestCol: Integer);
  procedure MoveTo(ADestRow, ADestCol: Integer);
  procedure InsertRows;
  procedure DeleteRows;
  procedure InsertCols;
  procedure DeleteCols;
  procedure ApplyFormatPatch(ASource: TXLSXRange; AParts: TXLSXFormatParts);
  function CopyToClipboard: Boolean;
  function SaveAsPDF(const FileName: WideString): Integer; overload;
  function SaveAsPDF(Stream: TStream): Integer; overload;
  function SetDynamicArrayFormula(const F: WideString): Integer;
end;

Members

Member Description
Create(ASheet, ARow1, ACol1, ARow2, ACol2) Builds a new worksheet-owned range and normalizes reversed corners so Row1/Col1 are always the upper-left coordinate
RefA1 傳回 the range reference as A1 text. Single-cell ranges return a single cell reference such as B2
SaveAsHTML Options overloads accept TXLSXHtmlExportOptions for Title, WriteDocument, IncludeStyles, IncludeBOM, and TableClass. Set WriteDocument to False for a table fragment; set IncludeBOM to False ,適用於把輸出內嵌進既有 UTF-8 回應的場合。所選範圍的 HTML 匯出也提供相同選項。匯出的輸出會把儲存格超連結保留為 <a> anchors and anchored images as inline data: 圖片 URI(當它們落在選取矩形內時)。選取矩形內的儲存格註解也會以跳脫過的 <td> title attributes. When IncludeStyles is enabled, explicit ColWidth values are emitted as <col> widths and explicit RowHeight values as <tr> height styles. Rich-text cell runs are emitted as escaped <span> elements with inline font, color, emphasis, and underline styles when IncludeStyles is enabled. Cell borders are emitted as CSS border-left, border-right, border-top, and border-bottom rules when IncludeStyles is enabled. Hidden rows and columns marked by RowHidden / ColHidden, plus explicit zero-width columns, are skipped in the generated table and <colgroup>. Vertical alignment settings are emitted as CSS vertical-align rules when IncludeStyles is enabled
SetValue / SetFormula 透過工作表的儲存格集合,把同一個值或公式文字寫入範圍內的每個儲存格
SetArrayFormula 在範圍上設定多儲存格 CSE 陣列公式。左上角儲存格保存公式文字並作為溢位範圍的錨點,其餘儲存格則指回該根儲存格,讓每個儲存格算出矩陣結果中屬於自己的元素。傳入公式文字時不需外圍大括號
ClearValues / Clear / ClearAll ClearValues and Clear remove values and formulas while preserving style indexes. ClearAll also clears style indexes
SetFontIndex / SetFillIndex / SetBorderIndex / SetNumberFormatIndex / SetAlignmentIndex 把以 1 為起始的活頁簿調色盤索引套用到範圍內的每個儲存格。傳入 0 可清除對應的每儲存格索引
SetNumberFormat 在活頁簿調色盤中查詢或附加數字格式字串,然後把得到的索引(以 1 為起始)套用到範圍。空格式會清除每個儲存格自己的索引
SetBorders Composes border edges over the range. TXLSXBorderEdgeKind accepts xlsxEdgeAll, xlsxEdgeOutline, xlsxEdgeInside, xlsxEdgeInsideHorizontal, xlsxEdgeInsideVertical, xlsxEdgeTop, xlsxEdgeBottom, xlsxEdgeLeft, xlsxEdgeRight, xlsxEdgeDiagonalUp, and xlsxEdgeDiagonalDown。請使用外框與內部框線選擇器建立表格框線,不會覆寫不相干的邊框設定
SetLocked / SetFormulaHidden 重複使用活頁簿的保護樣式集區,將儲存格保護旗標套用到範圍內的每個儲存格
AutoFitColumns / AutoFitRows 轉送給工作表的自動調整輔助方法,作用範圍是此範圍的欄或列界限。可選的寬度與高度限制會一併傳遞,讓報表版面保持在設計界限內
Merge / Unmerge Merge and Merge(False) create one merged rectangle. Merge(True) creates one merged range per row, matching Excel's merge-across behavior. Unmerge removes an exact merged-range match
Offset / Resize 傳回 another worksheet-owned range shifted from the current bounds or resized to the requested row and column count
Sort 依單一關鍵欄,或依優先順序排列的多個關鍵欄,排序此範圍內的列 ADescending 為每個排序鍵選擇遞增或遞減;省略的項目預設遞增。空白值排在最後
ApplyBuiltinStyle 把 Excel 內建儲存格樣式的外觀套用到範圍內的每個儲存格。此輔助方法以直接格式化的方式寫入:把對應的字型與填滿加入活頁簿集區,再將得到的索引指派給儲存格
CopyTo / MoveTo 將整個範圍複製或移動到目標左上角儲存格。值、公式、Rich Text 內容、樣式索引、註解、超連結與繪圖錨點,都會透過工作表的範圍複製與範圍移動輔助方法轉送
InsertRows / DeleteRows / InsertCols / DeleteCols 對範圍界限執行整列或整欄的插入與刪除。這些輔助方法會轉送給工作表的列/欄位移操作,讓公式、參照、篩選、版面設定範圍、圖表範圍、名稱、圖片與圖表錨點在編輯後仍與網格對齊
ApplyFormatPatch 使用 TXLSXFormatParts 旗標,從另一個來源範圍把選定的格式樣式套用到此範圍
CopyToClipboard Copies the range values and styles to the Windows clipboard
SaveAsPDF 使用同處理序 PDF 轉譯引擎,將儲存格範圍匯出為 PDF 檔或串流
SetDynamicArrayFormula 在範圍上設定動態陣列公式,支援 Excel 的動態溢位(spill)行為

Ownership and bounds

範圍由建立它的工作表所擁有,並隨該工作表一併釋放。應用程式碼不應呼叫 Free on a TXLSXRange ,並且在工作表或活頁簿銷毀後不應繼續持有範圍參照
數值座標以 1 為起始。建立範圍時會將顛倒的對角正規化,並把界限限制在 XLSX 工作表的上限內

範例

var
  Wb: TXLSXWorkbook;
  Ws: TXLSXWorksheet;
  Body: TXLSXRange;
begin
  Wb := TXLSXWorkbook.Create;
  try
    Ws := Wb.Sheets.Add('Report');
    Body := Ws.Range['B2:D20'];
    Body.SetNumberFormat('#,##0.00');
    Body.SetBorders(xlsxEdgeOutline, xlsxBorderThin, $FF808080);
    Body.Sort(2);
    Body.ApplyBuiltinStyle(xbsAccent1);
    Ws.Range['F2:G3'].SetArrayFormula('MMULT(A2:B3,D2:E3)');
    Body.AutoFitColumns;
    Body.SaveAsHTML('report-fragment.html');
  finally
    Wb.Free;
  end;
end;

請參閱