Question/Problem/Abstract:
See Also : Article_4724.asp - (Freeform Excel Worksheet) 
This class will produce an Excel Spreadsheet from a TDataSet. No OLE is required or Excel Installation needed to create the file. The one problem with Excel OLE is that is tends to be rather Sloooow. The class uses standard Delphi I/O functions and is considerably faster than the OLE calls.
Example.
var XL : TDataSetToExcel;
begin
XL := TDataSetToExcel.Create(MyQuery,'c:\temp\test.xls');
XL.WriteFile;
XL.Free;
end;
The columns are neatly sized, Numerics are formatted in "Courier" and obey "###,###,##0.00" for floats and "0" for integers. Dates are formatted "dd-MMM-yyyy hh:nn:ss". Column headers are in Bold and are boxed and shaded.
 unit MahExcel;
unit MahExcel; 
 interface
interface 
 uses Windows, SysUtils, DB, Math;
uses Windows, SysUtils, DB, Math; 

 // =============================================================================
// ============================================================================= 
 // TDataSet to Excel without OLE or Excel required
// TDataSet to Excel without OLE or Excel required 
 //
// 
 // For a good reference on Excel BIFF? file format see site
// For a good reference on Excel BIFF? file format see site 
 // http://sc.openoffice.org/excelfileformat.pdf
// http://sc.openoffice.org/excelfileformat.pdf 
 //
// 
 // Mike Heydon Dec 2002
// Mike Heydon Dec 2002 
 // =============================================================================
// ============================================================================= 

 type
type 
 // TDataSetToExcel
     // TDataSetToExcel 
 TDataSetToExcel = class(TObject)
     TDataSetToExcel = class(TObject) 
 protected
     protected 
 procedure WriteToken(AToken : word; ALength : word);
       procedure WriteToken(AToken : word; ALength : word); 
 procedure WriteFont(const AFontName : string; AFontHeight,
       procedure WriteFont(const AFontName : string; AFontHeight, 
 AAttribute : word);
                           AAttribute : word); 
 procedure WriteFormat(const AFormatStr : string);
       procedure WriteFormat(const AFormatStr : string); 
 private
     private 
 FRow : word;
       FRow : word; 
 FDataFile : file;
       FDataFile : file; 
 FFileName : string;
       FFileName : string; 
 FDataSet : TDataSet;
       FDataSet : TDataSet; 
 public
     public 
 constructor Create(ADataSet : TDataSet; const AFileName : string);
       constructor Create(ADataSet : TDataSet; const AFileName : string); 
 function WriteFile : boolean;
       function WriteFile : boolean; 
 end;
     end; 


 // -----------------------------------------------------------------------------
// ----------------------------------------------------------------------------- 
 implementation
implementation 

 const
const 
 // XL Tokens
      // XL Tokens 
 XL_DIM       = $00;
      XL_DIM       = $00; 
 XL_BOF       = $09;
      XL_BOF       = $09; 
 XL_EOF       = $0A;
      XL_EOF       = $0A; 
 XL_DOCUMENT  = $10;
      XL_DOCUMENT  = $10; 
 XL_FORMAT    = $1E;
      XL_FORMAT    = $1E; 
 XL_COLWIDTH  = $24;
      XL_COLWIDTH  = $24; 
 XL_FONT      = $31;
      XL_FONT      = $31; 

 // XL Cell Types
      // XL Cell Types 
 XL_INTEGER   = $02;
      XL_INTEGER   = $02; 
 XL_DOUBLE    = $03;
      XL_DOUBLE    = $03; 
 XL_STRING    = $04;
      XL_STRING    = $04; 

 // XL Cell Formats
      // XL Cell Formats 
 XL_INTFORMAT = $81;
      XL_INTFORMAT = $81; 
 XL_DBLFORMAT = $82;
      XL_DBLFORMAT = $82; 
 XL_XDTFORMAT = $83;
      XL_XDTFORMAT = $83; 
 XL_DTEFORMAT = $84;
      XL_DTEFORMAT = $84; 
 XL_TMEFORMAT = $85;
      XL_TMEFORMAT = $85; 
 XL_HEADBOLD  = $40;
      XL_HEADBOLD  = $40; 
 XL_HEADSHADE = $F8;
      XL_HEADSHADE = $F8; 

 // ========================
// ======================== 
 // Create the class
// Create the class 
 // ========================
// ======================== 

 constructor TDataSetToExcel.Create(ADataSet : TDataSet;
constructor TDataSetToExcel.Create(ADataSet : TDataSet; 
 const AFileName : string);
                                   const AFileName : string); 
 begin
begin 
 FDataSet := ADataSet;
  FDataSet := ADataSet; 
 FFileName := ChangeFileExt(AFilename,'.xls');
  FFileName := ChangeFileExt(AFilename,'.xls'); 
 end;
end; 

 // ====================================
// ==================================== 
 // Write a Token Descripton Header
// Write a Token Descripton Header 
 // ====================================
// ==================================== 

 procedure TDataSetToExcel.WriteToken(AToken : word; ALength : word);
procedure TDataSetToExcel.WriteToken(AToken : word; ALength : word); 
 var aTOKBuffer : array [0..1] of word;
var aTOKBuffer : array [0..1] of word; 
 begin
begin 
 aTOKBuffer[0] := AToken;
  aTOKBuffer[0] := AToken; 
 aTOKBuffer[1] := ALength;
  aTOKBuffer[1] := ALength; 
 Blockwrite(FDataFile,aTOKBuffer,SizeOf(aTOKBuffer));
  Blockwrite(FDataFile,aTOKBuffer,SizeOf(aTOKBuffer)); 
 end;
end; 

 // ====================================
// ==================================== 
 // Write the font information
// Write the font information 
 // ====================================
// ==================================== 

 procedure TDataSetToExcel.WriteFont(const AFontName : string;
procedure TDataSetToExcel.WriteFont(const AFontName : string; 
 AFontHeight,AAttribute : word);
                                    AFontHeight,AAttribute : word); 
 var iLen : byte;
var iLen : byte; 
 begin
begin 
 AFontHeight := AFontHeight * 20;
  AFontHeight := AFontHeight * 20; 
 WriteToken(XL_FONT,5 + length(AFontName));
  WriteToken(XL_FONT,5 + length(AFontName)); 
 BlockWrite(FDataFile,AFontHeight,2);
  BlockWrite(FDataFile,AFontHeight,2); 
 BlockWrite(FDataFile,AAttribute,2);
  BlockWrite(FDataFile,AAttribute,2); 
 iLen := length(AFontName);
  iLen := length(AFontName); 
 BlockWrite(FDataFile,iLen,1);
  BlockWrite(FDataFile,iLen,1); 
 BlockWrite(FDataFile,AFontName[1],iLen);
  BlockWrite(FDataFile,AFontName[1],iLen); 
 end;
end; 

 // ====================================
// ==================================== 
 // Write the format information
// Write the format information 
 // ====================================
// ==================================== 

 procedure TDataSetToExcel.WriteFormat(const AFormatStr : string);
procedure TDataSetToExcel.WriteFormat(const AFormatStr : string); 
 var iLen : byte;
var iLen : byte; 
 begin
begin 
 WriteToken(XL_FORMAT,1 + length(AFormatStr));
  WriteToken(XL_FORMAT,1 + length(AFormatStr)); 
 iLen := length(AFormatStr);
  iLen := length(AFormatStr); 
 BlockWrite(FDataFile,iLen,1);
  BlockWrite(FDataFile,iLen,1); 
 BlockWrite(FDataFile,AFormatStr[1],iLen);
  BlockWrite(FDataFile,AFormatStr[1],iLen); 
 end;
end; 

 // ====================================
// ==================================== 
 // Write the XL file from data set
// Write the XL file from data set 
 // ====================================
// ==================================== 

 function TDataSetToExcel.WriteFile : boolean;
function TDataSetToExcel.WriteFile : boolean; 
 var bRetvar : boolean;
var bRetvar : boolean; 
 aDOCBuffer : array [0..1] of word;
    aDOCBuffer : array [0..1] of word; 
 aDIMBuffer : array [0..3] of word;
    aDIMBuffer : array [0..3] of word; 
 aAttributes : array [0..2] of byte;
    aAttributes : array [0..2] of byte; 
 i : integer;
    i : integer; 
 iColNum,
    iColNum, 
 iDataLen : byte;
    iDataLen : byte; 
 sStrData : string;
    sStrData : string; 
 fDblData : double;
    fDblData : double; 
 wWidth : word;
    wWidth : word; 
 begin
begin 
 bRetvar := true;
  bRetvar := true; 
 FRow := 0;
  FRow := 0; 
 FillChar(aAttributes,SizeOf(aAttributes),0);
  FillChar(aAttributes,SizeOf(aAttributes),0); 
 AssignFile(FDataFile,FFileName);
  AssignFile(FDataFile,FFileName); 

 try
  try 
 Rewrite(FDataFile,1);
    Rewrite(FDataFile,1); 
 // Beginning of File
    // Beginning of File 
 WriteToken(XL_BOF,4);
    WriteToken(XL_BOF,4); 
 aDOCBuffer[0] := 0;
    aDOCBuffer[0] := 0; 
 aDOCBuffer[1] := XL_DOCUMENT;
    aDOCBuffer[1] := XL_DOCUMENT; 
 Blockwrite(FDataFile,aDOCBuffer,SizeOf(aDOCBuffer));
    Blockwrite(FDataFile,aDOCBuffer,SizeOf(aDOCBuffer)); 

 // Font Table
    // Font Table 
 WriteFont('Arial',10,0);
    WriteFont('Arial',10,0); 
 WriteFont('Arial',10,1);
    WriteFont('Arial',10,1); 
 WriteFont('Courier New',11,0);
    WriteFont('Courier New',11,0); 

 // Column widths
    // Column widths 
 for i := 0 to FDataSet.FieldCount - 1 do begin
    for i := 0 to FDataSet.FieldCount - 1 do begin 
 wWidth := (FDataSet.Fields[i].DisplayWidth + 1) * 256;
      wWidth := (FDataSet.Fields[i].DisplayWidth + 1) * 256; 
 if FDataSet.FieldDefs[i].DataType = ftDateTime then inc(wWidth,2000);
      if FDataSet.FieldDefs[i].DataType = ftDateTime then inc(wWidth,2000); 
 if FDataSet.FieldDefs[i].DataType = ftDate then inc(wWidth,1050);
      if FDataSet.FieldDefs[i].DataType = ftDate then inc(wWidth,1050); 
 if FDataSet.FieldDefs[i].DataType = ftTime then inc(wWidth,100);
      if FDataSet.FieldDefs[i].DataType = ftTime then inc(wWidth,100); 
 WriteToken(XL_COLWIDTH,4);
      WriteToken(XL_COLWIDTH,4); 
 iColNum := i;
      iColNum := i; 
 BlockWrite(FDataFile,iColNum,1);
      BlockWrite(FDataFile,iColNum,1); 
 BlockWrite(FDataFile,iColNum,1);
      BlockWrite(FDataFile,iColNum,1); 
 BlockWrite(FDataFile,wWidth,2);
      BlockWrite(FDataFile,wWidth,2); 
 end;
    end; 

 // Column Formats
    // Column Formats 
 WriteFormat('General');
    WriteFormat('General'); 
 WriteFormat('0');
    WriteFormat('0'); 
 WriteFormat('###,###,##0.00');
    WriteFormat('###,###,##0.00'); 
 WriteFormat('dd-mmm-yyyy hh:mm:ss');
    WriteFormat('dd-mmm-yyyy hh:mm:ss'); 
 WriteFormat('dd-mmm-yyyy');
    WriteFormat('dd-mmm-yyyy'); 
 WriteFormat('hh:mm:ss');
    WriteFormat('hh:mm:ss'); 

 // Dimensions
    // Dimensions 
 WriteToken(XL_DIM,8);
    WriteToken(XL_DIM,8); 
 aDIMBuffer[0] := 0;
    aDIMBuffer[0] := 0; 
 aDIMBuffer[1] := Min(FDataSet.RecordCount,$FFFF);
    aDIMBuffer[1] := Min(FDataSet.RecordCount,$FFFF); 
 aDIMBuffer[2] := 0;
    aDIMBuffer[2] := 0; 
 aDIMBuffer[3] := Min(FDataSet.FieldCount - 1,$FFFF);
    aDIMBuffer[3] := Min(FDataSet.FieldCount - 1,$FFFF); 
 Blockwrite(FDataFile,aDIMBuffer,SizeOf(aDIMBuffer));
    Blockwrite(FDataFile,aDIMBuffer,SizeOf(aDIMBuffer)); 

 // Column Headers
    // Column Headers 
 for i := 0 to FDataSet.FieldCount - 1 do begin
    for i := 0 to FDataSet.FieldCount - 1 do begin 
 sStrData := FDataSet.Fields[i].DisplayName;
      sStrData := FDataSet.Fields[i].DisplayName; 
 iDataLen := length(sStrData);
      iDataLen := length(sStrData); 
 WriteToken(XL_STRING,iDataLen + 8);
      WriteToken(XL_STRING,iDataLen + 8); 
 WriteToken(FRow,i);
      WriteToken(FRow,i); 
 aAttributes[1] := XL_HEADBOLD;
      aAttributes[1] := XL_HEADBOLD; 
 aAttributes[2] := XL_HEADSHADE;
      aAttributes[2] := XL_HEADSHADE; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
      BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));
      BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen)); 
 if iDataLen > 0 then BlockWrite(FDataFile,sStrData[1],iDataLen);
      if iDataLen > 0 then BlockWrite(FDataFile,sStrData[1],iDataLen); 
 aAttributes[2] := 0;
      aAttributes[2] := 0; 
 end;
    end; 

 // Data Rows
    // Data Rows 
 while not FDataSet.Eof do begin
    while not FDataSet.Eof do begin 
 inc(FRow);
      inc(FRow); 

 for i := 0 to FDataSet.FieldCount - 1 do begin
      for i := 0 to FDataSet.FieldCount - 1 do begin 
 case FDataSet.FieldDefs[i].DataType of
        case FDataSet.FieldDefs[i].DataType of 
 ftBoolean,
          ftBoolean, 
 ftWideString,
          ftWideString, 
 ftFixedChar,
          ftFixedChar, 
 ftString    : begin
          ftString    : begin 
 sStrData := FDataSet.Fields[i].AsString;
                          sStrData := FDataSet.Fields[i].AsString; 
 iDataLen := length(sStrData);
                          iDataLen := length(sStrData); 
 WriteToken(XL_STRING,iDataLen + 8);
                          WriteToken(XL_STRING,iDataLen + 8); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := 0;
                          aAttributes[1] := 0; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));
                          BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen)); 
 if iDataLen > 0 then
                          if iDataLen > 0 then 
 BlockWrite(FDataFile,sStrData[1],iDataLen);
                            BlockWrite(FDataFile,sStrData[1],iDataLen); 
 end;
                        end; 

 ftAutoInc,
          ftAutoInc, 
 ftSmallInt,
          ftSmallInt, 
 ftInteger,
          ftInteger, 
 ftWord,
          ftWord, 
 ftLargeInt  : begin
          ftLargeInt  : begin 
 fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat; 
 iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData); 
 WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := XL_INTFORMAT;
                          aAttributes[1] := XL_INTFORMAT; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen); 
 end;
                        end; 

 ftFloat,
          ftFloat, 
 ftCurrency,
          ftCurrency, 
 ftBcd      : begin
          ftBcd      : begin 
 fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat; 
 iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData); 
 WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := XL_DBLFORMAT;
                          aAttributes[1] := XL_DBLFORMAT; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen); 
 end;
                        end; 

 ftDateTime : begin
          ftDateTime : begin 
 fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat; 
 iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData); 
 WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := XL_XDTFORMAT;
                          aAttributes[1] := XL_XDTFORMAT; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen); 
 end;
                        end; 

 ftDate     : begin
          ftDate     : begin 
 fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat; 
 iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData); 
 WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := XL_DTEFORMAT;
                          aAttributes[1] := XL_DTEFORMAT; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen); 
 end;
                        end; 

 ftTime     : begin
          ftTime     : begin 
 fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat; 
 iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData); 
 WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15); 
 WriteToken(FRow,i);
                          WriteToken(FRow,i); 
 aAttributes[1] := XL_TMEFORMAT;
                          aAttributes[1] := XL_TMEFORMAT; 
 BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
 BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen); 
 end;
                        end; 


 end;
        end; 
 end;
      end; 

 FDataSet.Next;
      FDataSet.Next; 
 end;
    end; 

 // End of File
    // End of File 
 WriteToken(XL_EOF,0);
    WriteToken(XL_EOF,0); 
 CloseFile(FDataFile);
    CloseFile(FDataFile); 
 except
  except 
 bRetvar := false;
    bRetvar := false; 
 end;
  end; 

 Result := bRetvar;
  Result := bRetvar; 
 end;
end; 


 end.
end.
This class will produce an Excel Spreadsheet from a TDataSet. No OLE is required or Excel Installation needed to create the file. The one problem with Excel OLE is that is tends to be rather Sloooow. The class uses standard Delphi I/O functions and is considerably faster than the OLE calls.
Example.
var XL : TDataSetToExcel;
begin
XL := TDataSetToExcel.Create(MyQuery,'c:\temp\test.xls');
XL.WriteFile;
XL.Free;
end;
The columns are neatly sized, Numerics are formatted in "Courier" and obey "###,###,##0.00" for floats and "0" for integers. Dates are formatted "dd-MMM-yyyy hh:nn:ss". Column headers are in Bold and are boxed and shaded.
 unit MahExcel;
unit MahExcel;  interface
interface  uses Windows, SysUtils, DB, Math;
uses Windows, SysUtils, DB, Math; 
 // =============================================================================
// =============================================================================  // TDataSet to Excel without OLE or Excel required
// TDataSet to Excel without OLE or Excel required  //
//  // For a good reference on Excel BIFF? file format see site
// For a good reference on Excel BIFF? file format see site  // http://sc.openoffice.org/excelfileformat.pdf
// http://sc.openoffice.org/excelfileformat.pdf  //
//  // Mike Heydon Dec 2002
// Mike Heydon Dec 2002  // =============================================================================
// ============================================================================= 
 type
type  // TDataSetToExcel
     // TDataSetToExcel  TDataSetToExcel = class(TObject)
     TDataSetToExcel = class(TObject)  protected
     protected  procedure WriteToken(AToken : word; ALength : word);
       procedure WriteToken(AToken : word; ALength : word);  procedure WriteFont(const AFontName : string; AFontHeight,
       procedure WriteFont(const AFontName : string; AFontHeight,  AAttribute : word);
                           AAttribute : word);  procedure WriteFormat(const AFormatStr : string);
       procedure WriteFormat(const AFormatStr : string);  private
     private  FRow : word;
       FRow : word;  FDataFile : file;
       FDataFile : file;  FFileName : string;
       FFileName : string;  FDataSet : TDataSet;
       FDataSet : TDataSet;  public
     public  constructor Create(ADataSet : TDataSet; const AFileName : string);
       constructor Create(ADataSet : TDataSet; const AFileName : string);  function WriteFile : boolean;
       function WriteFile : boolean;  end;
     end; 

 // -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------  implementation
implementation 
 const
const  // XL Tokens
      // XL Tokens  XL_DIM       = $00;
      XL_DIM       = $00;  XL_BOF       = $09;
      XL_BOF       = $09;  XL_EOF       = $0A;
      XL_EOF       = $0A;  XL_DOCUMENT  = $10;
      XL_DOCUMENT  = $10;  XL_FORMAT    = $1E;
      XL_FORMAT    = $1E;  XL_COLWIDTH  = $24;
      XL_COLWIDTH  = $24;  XL_FONT      = $31;
      XL_FONT      = $31; 
 // XL Cell Types
      // XL Cell Types  XL_INTEGER   = $02;
      XL_INTEGER   = $02;  XL_DOUBLE    = $03;
      XL_DOUBLE    = $03;  XL_STRING    = $04;
      XL_STRING    = $04; 
 // XL Cell Formats
      // XL Cell Formats  XL_INTFORMAT = $81;
      XL_INTFORMAT = $81;  XL_DBLFORMAT = $82;
      XL_DBLFORMAT = $82;  XL_XDTFORMAT = $83;
      XL_XDTFORMAT = $83;  XL_DTEFORMAT = $84;
      XL_DTEFORMAT = $84;  XL_TMEFORMAT = $85;
      XL_TMEFORMAT = $85;  XL_HEADBOLD  = $40;
      XL_HEADBOLD  = $40;  XL_HEADSHADE = $F8;
      XL_HEADSHADE = $F8; 
 // ========================
// ========================  // Create the class
// Create the class  // ========================
// ======================== 
 constructor TDataSetToExcel.Create(ADataSet : TDataSet;
constructor TDataSetToExcel.Create(ADataSet : TDataSet;  const AFileName : string);
                                   const AFileName : string);  begin
begin  FDataSet := ADataSet;
  FDataSet := ADataSet;  FFileName := ChangeFileExt(AFilename,'.xls');
  FFileName := ChangeFileExt(AFilename,'.xls');  end;
end; 
 // ====================================
// ====================================  // Write a Token Descripton Header
// Write a Token Descripton Header  // ====================================
// ==================================== 
 procedure TDataSetToExcel.WriteToken(AToken : word; ALength : word);
procedure TDataSetToExcel.WriteToken(AToken : word; ALength : word);  var aTOKBuffer : array [0..1] of word;
var aTOKBuffer : array [0..1] of word;  begin
begin  aTOKBuffer[0] := AToken;
  aTOKBuffer[0] := AToken;  aTOKBuffer[1] := ALength;
  aTOKBuffer[1] := ALength;  Blockwrite(FDataFile,aTOKBuffer,SizeOf(aTOKBuffer));
  Blockwrite(FDataFile,aTOKBuffer,SizeOf(aTOKBuffer));  end;
end; 
 // ====================================
// ====================================  // Write the font information
// Write the font information  // ====================================
// ==================================== 
 procedure TDataSetToExcel.WriteFont(const AFontName : string;
procedure TDataSetToExcel.WriteFont(const AFontName : string;  AFontHeight,AAttribute : word);
                                    AFontHeight,AAttribute : word);  var iLen : byte;
var iLen : byte;  begin
begin  AFontHeight := AFontHeight * 20;
  AFontHeight := AFontHeight * 20;  WriteToken(XL_FONT,5 + length(AFontName));
  WriteToken(XL_FONT,5 + length(AFontName));  BlockWrite(FDataFile,AFontHeight,2);
  BlockWrite(FDataFile,AFontHeight,2);  BlockWrite(FDataFile,AAttribute,2);
  BlockWrite(FDataFile,AAttribute,2);  iLen := length(AFontName);
  iLen := length(AFontName);  BlockWrite(FDataFile,iLen,1);
  BlockWrite(FDataFile,iLen,1);  BlockWrite(FDataFile,AFontName[1],iLen);
  BlockWrite(FDataFile,AFontName[1],iLen);  end;
end; 
 // ====================================
// ====================================  // Write the format information
// Write the format information  // ====================================
// ==================================== 
 procedure TDataSetToExcel.WriteFormat(const AFormatStr : string);
procedure TDataSetToExcel.WriteFormat(const AFormatStr : string);  var iLen : byte;
var iLen : byte;  begin
begin  WriteToken(XL_FORMAT,1 + length(AFormatStr));
  WriteToken(XL_FORMAT,1 + length(AFormatStr));  iLen := length(AFormatStr);
  iLen := length(AFormatStr);  BlockWrite(FDataFile,iLen,1);
  BlockWrite(FDataFile,iLen,1);  BlockWrite(FDataFile,AFormatStr[1],iLen);
  BlockWrite(FDataFile,AFormatStr[1],iLen);  end;
end; 
 // ====================================
// ====================================  // Write the XL file from data set
// Write the XL file from data set  // ====================================
// ==================================== 
 function TDataSetToExcel.WriteFile : boolean;
function TDataSetToExcel.WriteFile : boolean;  var bRetvar : boolean;
var bRetvar : boolean;  aDOCBuffer : array [0..1] of word;
    aDOCBuffer : array [0..1] of word;  aDIMBuffer : array [0..3] of word;
    aDIMBuffer : array [0..3] of word;  aAttributes : array [0..2] of byte;
    aAttributes : array [0..2] of byte;  i : integer;
    i : integer;  iColNum,
    iColNum,  iDataLen : byte;
    iDataLen : byte;  sStrData : string;
    sStrData : string;  fDblData : double;
    fDblData : double;  wWidth : word;
    wWidth : word;  begin
begin  bRetvar := true;
  bRetvar := true;  FRow := 0;
  FRow := 0;  FillChar(aAttributes,SizeOf(aAttributes),0);
  FillChar(aAttributes,SizeOf(aAttributes),0);  AssignFile(FDataFile,FFileName);
  AssignFile(FDataFile,FFileName); 
 try
  try  Rewrite(FDataFile,1);
    Rewrite(FDataFile,1);  // Beginning of File
    // Beginning of File  WriteToken(XL_BOF,4);
    WriteToken(XL_BOF,4);  aDOCBuffer[0] := 0;
    aDOCBuffer[0] := 0;  aDOCBuffer[1] := XL_DOCUMENT;
    aDOCBuffer[1] := XL_DOCUMENT;  Blockwrite(FDataFile,aDOCBuffer,SizeOf(aDOCBuffer));
    Blockwrite(FDataFile,aDOCBuffer,SizeOf(aDOCBuffer)); 
 // Font Table
    // Font Table  WriteFont('Arial',10,0);
    WriteFont('Arial',10,0);  WriteFont('Arial',10,1);
    WriteFont('Arial',10,1);  WriteFont('Courier New',11,0);
    WriteFont('Courier New',11,0); 
 // Column widths
    // Column widths  for i := 0 to FDataSet.FieldCount - 1 do begin
    for i := 0 to FDataSet.FieldCount - 1 do begin  wWidth := (FDataSet.Fields[i].DisplayWidth + 1) * 256;
      wWidth := (FDataSet.Fields[i].DisplayWidth + 1) * 256;  if FDataSet.FieldDefs[i].DataType = ftDateTime then inc(wWidth,2000);
      if FDataSet.FieldDefs[i].DataType = ftDateTime then inc(wWidth,2000);  if FDataSet.FieldDefs[i].DataType = ftDate then inc(wWidth,1050);
      if FDataSet.FieldDefs[i].DataType = ftDate then inc(wWidth,1050);  if FDataSet.FieldDefs[i].DataType = ftTime then inc(wWidth,100);
      if FDataSet.FieldDefs[i].DataType = ftTime then inc(wWidth,100);  WriteToken(XL_COLWIDTH,4);
      WriteToken(XL_COLWIDTH,4);  iColNum := i;
      iColNum := i;  BlockWrite(FDataFile,iColNum,1);
      BlockWrite(FDataFile,iColNum,1);  BlockWrite(FDataFile,iColNum,1);
      BlockWrite(FDataFile,iColNum,1);  BlockWrite(FDataFile,wWidth,2);
      BlockWrite(FDataFile,wWidth,2);  end;
    end; 
 // Column Formats
    // Column Formats  WriteFormat('General');
    WriteFormat('General');  WriteFormat('0');
    WriteFormat('0');  WriteFormat('###,###,##0.00');
    WriteFormat('###,###,##0.00');  WriteFormat('dd-mmm-yyyy hh:mm:ss');
    WriteFormat('dd-mmm-yyyy hh:mm:ss');  WriteFormat('dd-mmm-yyyy');
    WriteFormat('dd-mmm-yyyy');  WriteFormat('hh:mm:ss');
    WriteFormat('hh:mm:ss'); 
 // Dimensions
    // Dimensions  WriteToken(XL_DIM,8);
    WriteToken(XL_DIM,8);  aDIMBuffer[0] := 0;
    aDIMBuffer[0] := 0;  aDIMBuffer[1] := Min(FDataSet.RecordCount,$FFFF);
    aDIMBuffer[1] := Min(FDataSet.RecordCount,$FFFF);  aDIMBuffer[2] := 0;
    aDIMBuffer[2] := 0;  aDIMBuffer[3] := Min(FDataSet.FieldCount - 1,$FFFF);
    aDIMBuffer[3] := Min(FDataSet.FieldCount - 1,$FFFF);  Blockwrite(FDataFile,aDIMBuffer,SizeOf(aDIMBuffer));
    Blockwrite(FDataFile,aDIMBuffer,SizeOf(aDIMBuffer)); 
 // Column Headers
    // Column Headers  for i := 0 to FDataSet.FieldCount - 1 do begin
    for i := 0 to FDataSet.FieldCount - 1 do begin  sStrData := FDataSet.Fields[i].DisplayName;
      sStrData := FDataSet.Fields[i].DisplayName;  iDataLen := length(sStrData);
      iDataLen := length(sStrData);  WriteToken(XL_STRING,iDataLen + 8);
      WriteToken(XL_STRING,iDataLen + 8);  WriteToken(FRow,i);
      WriteToken(FRow,i);  aAttributes[1] := XL_HEADBOLD;
      aAttributes[1] := XL_HEADBOLD;  aAttributes[2] := XL_HEADSHADE;
      aAttributes[2] := XL_HEADSHADE;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
      BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));
      BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));  if iDataLen > 0 then BlockWrite(FDataFile,sStrData[1],iDataLen);
      if iDataLen > 0 then BlockWrite(FDataFile,sStrData[1],iDataLen);  aAttributes[2] := 0;
      aAttributes[2] := 0;  end;
    end; 
 // Data Rows
    // Data Rows  while not FDataSet.Eof do begin
    while not FDataSet.Eof do begin  inc(FRow);
      inc(FRow); 
 for i := 0 to FDataSet.FieldCount - 1 do begin
      for i := 0 to FDataSet.FieldCount - 1 do begin  case FDataSet.FieldDefs[i].DataType of
        case FDataSet.FieldDefs[i].DataType of  ftBoolean,
          ftBoolean,  ftWideString,
          ftWideString,  ftFixedChar,
          ftFixedChar,  ftString    : begin
          ftString    : begin  sStrData := FDataSet.Fields[i].AsString;
                          sStrData := FDataSet.Fields[i].AsString;  iDataLen := length(sStrData);
                          iDataLen := length(sStrData);  WriteToken(XL_STRING,iDataLen + 8);
                          WriteToken(XL_STRING,iDataLen + 8);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := 0;
                          aAttributes[1] := 0;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));
                          BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen));  if iDataLen > 0 then
                          if iDataLen > 0 then  BlockWrite(FDataFile,sStrData[1],iDataLen);
                            BlockWrite(FDataFile,sStrData[1],iDataLen);  end;
                        end; 
 ftAutoInc,
          ftAutoInc,  ftSmallInt,
          ftSmallInt,  ftInteger,
          ftInteger,  ftWord,
          ftWord,  ftLargeInt  : begin
          ftLargeInt  : begin  fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat;  iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData);  WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := XL_INTFORMAT;
                          aAttributes[1] := XL_INTFORMAT;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen);  end;
                        end; 
 ftFloat,
          ftFloat,  ftCurrency,
          ftCurrency,  ftBcd      : begin
          ftBcd      : begin  fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat;  iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData);  WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := XL_DBLFORMAT;
                          aAttributes[1] := XL_DBLFORMAT;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen);  end;
                        end; 
 ftDateTime : begin
          ftDateTime : begin  fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat;  iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData);  WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := XL_XDTFORMAT;
                          aAttributes[1] := XL_XDTFORMAT;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen);  end;
                        end; 
 ftDate     : begin
          ftDate     : begin  fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat;  iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData);  WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := XL_DTEFORMAT;
                          aAttributes[1] := XL_DTEFORMAT;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen);  end;
                        end; 
 ftTime     : begin
          ftTime     : begin  fDblData := FDataSet.Fields[i].AsFloat;
                          fDblData := FDataSet.Fields[i].AsFloat;  iDataLen := SizeOf(fDblData);
                          iDataLen := SizeOf(fDblData);  WriteToken(XL_DOUBLE,15);
                          WriteToken(XL_DOUBLE,15);  WriteToken(FRow,i);
                          WriteToken(FRow,i);  aAttributes[1] := XL_TMEFORMAT;
                          aAttributes[1] := XL_TMEFORMAT;  BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));
                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes));  BlockWrite(FDataFile,fDblData,iDatalen);
                          BlockWrite(FDataFile,fDblData,iDatalen);  end;
                        end; 

 end;
        end;  end;
      end; 
 FDataSet.Next;
      FDataSet.Next;  end;
    end; 
 // End of File
    // End of File  WriteToken(XL_EOF,0);
    WriteToken(XL_EOF,0);  CloseFile(FDataFile);
    CloseFile(FDataFile);  except
  except  bRetvar := false;
    bRetvar := false;  end;
  end; 
 Result := bRetvar;
  Result := bRetvar;  end;
end; 

 end.
end. 
                    
                     
                    
                 
                    
                 
 
         
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号