HotXLS Docs

SaveAs method

Saves changes to the Workbook in a different file or stream. Yields 1 if it succeeds

Syntax

function SaveAs(FileName: WideString): Integer;
function SaveAs(FileName: WideString; FileFormat: TXLSFileFormat): Integer;
function SaveAs(Stream: TStream);
function SaveAs(Stream: TStream; FileFormat: TXLSFileFormat): Integer;
FileName WideString. A string giving the name of the file to be saved. You can include a full path; if you don't, Workbook saves the file in the current folder
FileFormat Optional TXLSFileFormat. A value giving the format of the file to be saved. The default is xlExcel97
Stream TStream. Saves Workbook to a stream specified in the Stream parameter

Overview

XLS file output completes serialisation in a sibling staging file before replacing the destination; a failed commit leaves the existing destination untouched and reports the captured native error through the workbook diagnostics

On Windows filesystems that support POSIX rename, replacement can succeed while a metadata or reader handle permitting deletion remains open; an existing reader keeps seeing the original file object, and a new reader of the destination name sees the completed replacement

The destination must permit delete access and delete sharing, and readonly targets are still rejected; a failed commit is not retried, whilst missing targets, reparse paths and unsupported filesystems use the legacy commit path selected before replacement

Atomic replacement protects the publication of a single completed file; it does not provide compare-and-swap isolation against another process concurrently replacing the same name

FileFormat can be one of these TXLSFileFormat constants
xlExcel97 MS Excel 97,2000,XP,2003 file format
xlExcel5 MS Excel 5,95 file format
xlHTML HTML file format
xlRTF Rich Text Format (RTF)
xlCSV Comma separated values file format (CSV)
xlText Tab separated values file format (TSV)
xlUnicodeCSV Comma separated unicode values file format (CSV)
xlUnicodeText Tab separated unicode values file format (TSV)

Example

This example creates a new Workbook with a single Worksheet and then saves the Workbook

Var
  Workbook: IXLSWorkbook;
begin
  Workbook := TXLSWorkbook.Create; {create new Workbook}
  Workbook.Worksheets.Add;
  {...}
  //Save Workbook as Excel 97 file format 
  Workbook.SaveAs('C:\book.xls');
  //Save Workbook as Excel 5-95 file format 
  Workbook.SaveAs('C:\book5.xls', xlExcel5);
  //Save Workbook as HTML 
  Workbook.SaveAs('C:\book.html', xlHTML);
  //Save Workbook as RTF file 
  Workbook.SaveAs('C:\book.rtf', xlRTF);
  //Save active Worksheet as comma separated values (CSV)
  Workbook.SaveAs('C:\book.csv', xlCSV);
  //Save active Worksheet as tab separated values (TSV)
  Workbook.SaveAs('C:\book.tsv', xlText);
  //Save active Worksheet as comma separated unicode values (CSV)
  Workbook.SaveAs('C:\book.csv', xlUnicodeCSV);
  //Save active Worksheet as tab separated unicode values (TSV)
  Workbook.SaveAs('C:\book.tsv', xlUnicodeText);
end;
This example saves Workbook into the BLOB field

var
  MS: TMemoryStream;
begin
  if not (ClientDataSet1.State in [dsInsert, dsEdit]) then
    ClientDataSet1.Insert;
  MS := TMemoryStream.Create();
  try
    FWorkbook.SaveAs(MS);
    ClientDataSet1Documents.LoadFromStream(MS);
  finally
    MS.Free;
  end;
  ClientDataSet1.Post;
end;