HotXLS 文档

TXLSXExternalLink / TXLSXExternalLinks classes

Workbook-level collection of external workbook references (OOXML externalLinks part). Each entry describes one linked .xlsx file by its target URL and an optional list of sheet names drawn from the cached metadata inside the link part. The collection lives on TXLSXWorkbook.ExternalLinks Declared in lxHandleX

TXLSXExternalLink declaration

type
  TXLSXExternalLink = class
    constructor Create(const ATarget: WideString);
    destructor Destroy; override;
    property Target: WideString;
    property SheetNames: TStringList;
  end;
      

TXLSXExternalLink members

Target URL of the referenced external workbook. For a workbook in the same directory use a relative path such as '../Reports/Sales.xlsx'; for a network location use a full UNC or file URI. SaveAs writes this verbatim as the relationship target and into xl/externalLinks/externalLinkN.xml.rels
SheetNames Optional list of sheet names inside the external workbook. When populated, SaveAs emits <sheetNames> children inside the <externalBook> element so Excel can resolve cross-workbook references without opening the linked file. Populated automatically by Open when round-tripping an existing workbook. Leave empty when building a link from scratch; Excel will resolve the sheet names the next time the external file is opened

TXLSXExternalLinks declaration

type
  TXLSXExternalLinks = class
    constructor Create;
    destructor Destroy; override;
    function Add(const ATarget: WideString): TXLSXExternalLink;
    function IndexOfTarget(const ATarget: WideString): Integer;
    function FindByTarget(const ATarget: WideString): TXLSXExternalLink;
    function DeleteByTarget(const ATarget: WideString): Boolean;
    procedure Delete(Index: Integer);
    procedure Clear;
    property Count: Integer;
    property Items[Index: Integer]: TXLSXExternalLink; default;
  end;
      

TXLSXExternalLinks members

Add(Target) Appends a new external link with the given target URL and returns the new TXLSXExternalLink instance. Populate SheetNames on the returned object if known
IndexOfTarget / FindByTarget Locate the first external workbook link whose Target matches the requested path or URL. Matching is case-insensitive
DeleteByTarget / Delete / Clear Delete one link by target, delete one link by 0-based index, or clear the whole collection
Count / Items[Index] Inspect external links by 0-based index. Items is the default property

SaveAs / Open behavior

When Workbook.ExternalLinks.Count > 0, SaveAs emits:
  • An <externalReferences> block in xl/workbook.xml with one <externalReference r:id="rIdN"/> per link
  • A relationship entry in xl/_rels/workbook.xml.rels pointing to externalLinks/externalLinkN.xml
  • xl/externalLinks/externalLinkN.xml containing the <externalBook> element with the target relation and optional <sheetNames>
  • xl/externalLinks/externalLinkN.xml.rels with the actual Target URL
Open round-trips Target and SheetNames; cached cell values inside <sheetDataSet> are not preserved by the current release

示例

var
  wb: TXLSXWorkbook;
  link: TXLSXExternalLink;
begin
  wb := TXLSXWorkbook.Create;
  try
    link := wb.ExternalLinks.Add('../Data/Source.xlsx');
    link.SheetNames.Add('Sheet1');
    link.SheetNames.Add('Sheet2');
    wb.Sheets.Add('Report');
    wb.Sheets[0].Cells.Item[1, 1].Formula :=
      '[Source.xlsx]Sheet1!$A$1';
    wb.SaveAs('report.xlsx');
  finally
    wb.Free;
  end;
end;
    

请参阅