Delphi class TDataSetToExcel

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.

Delphi class TDataSetToExcel unit MahExcel; 
Delphi class TDataSetToExcel
interface  
Delphi class TDataSetToExceluses Windows, SysUtils, DB, Math; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ============================================================================= 
Delphi class TDataSetToExcel
//  TDataSet to Excel without OLE or Excel required 
Delphi class TDataSetToExcel
//  
Delphi class TDataSetToExcel
//  For a good reference on Excel BIFF? file format see site 
Delphi class TDataSetToExcel
//   http://sc.openoffice.org/excelfileformat.pdf  
Delphi class TDataSetToExcel
//  
Delphi class TDataSetToExcel
//  Mike Heydon Dec 2002 
Delphi class TDataSetToExcel
//  ============================================================================= 
Delphi class TDataSetToExcel

Delphi class TDataSetToExceltype 
Delphi class TDataSetToExcel     
//  TDataSetToExcel 
Delphi class TDataSetToExcel
     TDataSetToExcel  =   class (TObject) 
Delphi class TDataSetToExcel     
protected  
Delphi class TDataSetToExcel       procedure WriteToken(AToken : word; ALength : word); 
Delphi class TDataSetToExcel       procedure WriteFont(
const  AFontName :  string ; AFontHeight, 
Delphi class TDataSetToExcel                           AAttribute : word); 
Delphi class TDataSetToExcel       procedure WriteFormat(
const  AFormatStr :  string ); 
Delphi class TDataSetToExcel     
private  
Delphi class TDataSetToExcel       FRow : word; 
Delphi class TDataSetToExcel       FDataFile : file; 
Delphi class TDataSetToExcel       FFileName : 
string
Delphi class TDataSetToExcel       FDataSet : TDataSet; 
Delphi class TDataSetToExcel     
public  
Delphi class TDataSetToExcel       constructor Create(ADataSet : TDataSet; 
const  AFileName :  string ); 
Delphi class TDataSetToExcel       function WriteFile : boolean; 
Delphi class TDataSetToExcel     end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ----------------------------------------------------------------------------- 
Delphi class TDataSetToExcel
implementation 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
const  
Delphi class TDataSetToExcel      
//  XL Tokens 
Delphi class TDataSetToExcel
      XL_DIM        =  $ 00
Delphi class TDataSetToExcel      XL_BOF       
=  $ 09
Delphi class TDataSetToExcel      XL_EOF       
=  $0A; 
Delphi class TDataSetToExcel      XL_DOCUMENT  
=  $ 10
Delphi class TDataSetToExcel      XL_FORMAT    
=  $1E; 
Delphi class TDataSetToExcel      XL_COLWIDTH  
=  $ 24
Delphi class TDataSetToExcel      XL_FONT      
=  $ 31
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel      
//  XL Cell Types 
Delphi class TDataSetToExcel
      XL_INTEGER    =  $ 02
Delphi class TDataSetToExcel      XL_DOUBLE    
=  $ 03
Delphi class TDataSetToExcel      XL_STRING    
=  $ 04
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel      
//  XL Cell Formats 
Delphi class TDataSetToExcel
      XL_INTFORMAT  =  $ 81
Delphi class TDataSetToExcel      XL_DBLFORMAT 
=  $ 82
Delphi class TDataSetToExcel      XL_XDTFORMAT 
=  $ 83
Delphi class TDataSetToExcel      XL_DTEFORMAT 
=  $ 84
Delphi class TDataSetToExcel      XL_TMEFORMAT 
=  $ 85
Delphi class TDataSetToExcel      XL_HEADBOLD  
=  $ 40
Delphi class TDataSetToExcel      XL_HEADSHADE 
=  $F8; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ======================== 
Delphi class TDataSetToExcel
//  Create the class 
Delphi class TDataSetToExcel
//  ======================== 
Delphi class TDataSetToExcel

Delphi class TDataSetToExcelconstructor TDataSetToExcel.Create(ADataSet : TDataSet; 
Delphi class TDataSetToExcel                                   
const  AFileName :  string ); 
Delphi class TDataSetToExcelbegin 
Delphi class TDataSetToExcel  FDataSet :
=  ADataSet; 
Delphi class TDataSetToExcel  FFileName :
=  ChangeFileExt(AFilename, ' .xls ' ); 
Delphi class TDataSetToExcelend; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel
//  Write a Token Descripton Header 
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel

Delphi class TDataSetToExcelprocedure TDataSetToExcel.WriteToken(AToken : word; ALength : word); 
Delphi class TDataSetToExcelvar aTOKBuffer : array [
0 .. 1 ] of word; 
Delphi class TDataSetToExcelbegin 
Delphi class TDataSetToExcel  aTOKBuffer[
0 ] : =  AToken; 
Delphi class TDataSetToExcel  aTOKBuffer[
1 ] : =  ALength; 
Delphi class TDataSetToExcel  Blockwrite(FDataFile,aTOKBuffer,SizeOf(aTOKBuffer)); 
Delphi class TDataSetToExcelend; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel
//  Write the font information 
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel

Delphi class TDataSetToExcelprocedure TDataSetToExcel.WriteFont(
const  AFontName :  string
Delphi class TDataSetToExcel                                    AFontHeight,AAttribute : word); 
Delphi class TDataSetToExcelvar iLen : 
byte
Delphi class TDataSetToExcelbegin 
Delphi class TDataSetToExcel  AFontHeight :
=  AFontHeight  *   20
Delphi class TDataSetToExcel  WriteToken(XL_FONT,
5   +  length(AFontName)); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,AFontHeight,
2 ); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,AAttribute,
2 ); 
Delphi class TDataSetToExcel  iLen :
=  length(AFontName); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,iLen,
1 ); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,AFontName[
1 ],iLen); 
Delphi class TDataSetToExcelend; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel
//  Write the format information 
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel

Delphi class TDataSetToExcelprocedure TDataSetToExcel.WriteFormat(
const  AFormatStr :  string ); 
Delphi class TDataSetToExcelvar iLen : 
byte
Delphi class TDataSetToExcelbegin 
Delphi class TDataSetToExcel  WriteToken(XL_FORMAT,
1   +  length(AFormatStr)); 
Delphi class TDataSetToExcel  iLen :
=  length(AFormatStr); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,iLen,
1 ); 
Delphi class TDataSetToExcel  BlockWrite(FDataFile,AFormatStr[
1 ],iLen); 
Delphi class TDataSetToExcelend; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel
//  Write the XL file from data set 
Delphi class TDataSetToExcel
//  ==================================== 
Delphi class TDataSetToExcel

Delphi class TDataSetToExcelfunction TDataSetToExcel.WriteFile : boolean; 
Delphi class TDataSetToExcelvar bRetvar : boolean; 
Delphi class TDataSetToExcel    aDOCBuffer : array [
0 .. 1 ] of word; 
Delphi class TDataSetToExcel    aDIMBuffer : array [
0 .. 3 ] of word; 
Delphi class TDataSetToExcel    aAttributes : array [
0 .. 2 ] of  byte
Delphi class TDataSetToExcel    i : integer; 
Delphi class TDataSetToExcel    iColNum, 
Delphi class TDataSetToExcel    iDataLen : 
byte
Delphi class TDataSetToExcel    sStrData : 
string
Delphi class TDataSetToExcel    fDblData : 
double
Delphi class TDataSetToExcel    wWidth : word; 
Delphi class TDataSetToExcelbegin 
Delphi class TDataSetToExcel  bRetvar :
=   true
Delphi class TDataSetToExcel  FRow :
=   0
Delphi class TDataSetToExcel  FillChar(aAttributes,SizeOf(aAttributes),
0 ); 
Delphi class TDataSetToExcel  AssignFile(FDataFile,FFileName); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel  
try  
Delphi class TDataSetToExcel    Rewrite(FDataFile,
1 ); 
Delphi class TDataSetToExcel    
//  Beginning of File 
Delphi class TDataSetToExcel
    WriteToken(XL_BOF, 4 ); 
Delphi class TDataSetToExcel    aDOCBuffer[
0 ] : =   0
Delphi class TDataSetToExcel    aDOCBuffer[
1 ] : =  XL_DOCUMENT; 
Delphi class TDataSetToExcel    Blockwrite(FDataFile,aDOCBuffer,SizeOf(aDOCBuffer)); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Font Table 
Delphi class TDataSetToExcel
    WriteFont( ' Arial ' , 10 , 0 ); 
Delphi class TDataSetToExcel    WriteFont(
' Arial ' , 10 , 1 ); 
Delphi class TDataSetToExcel    WriteFont(
' Courier New ' , 11 , 0 ); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Column widths 
Delphi class TDataSetToExcel
     for  i : =   0  to FDataSet.FieldCount  -   1   do  begin 
Delphi class TDataSetToExcel      wWidth :
=  (FDataSet.Fields[i].DisplayWidth  +   1 *   256
Delphi class TDataSetToExcel      
if  FDataSet.FieldDefs[i].DataType  =  ftDateTime then inc(wWidth, 2000 ); 
Delphi class TDataSetToExcel      
if  FDataSet.FieldDefs[i].DataType  =  ftDate then inc(wWidth, 1050 ); 
Delphi class TDataSetToExcel      
if  FDataSet.FieldDefs[i].DataType  =  ftTime then inc(wWidth, 100 ); 
Delphi class TDataSetToExcel      WriteToken(XL_COLWIDTH,
4 ); 
Delphi class TDataSetToExcel      iColNum :
=  i; 
Delphi class TDataSetToExcel      BlockWrite(FDataFile,iColNum,
1 ); 
Delphi class TDataSetToExcel      BlockWrite(FDataFile,iColNum,
1 ); 
Delphi class TDataSetToExcel      BlockWrite(FDataFile,wWidth,
2 ); 
Delphi class TDataSetToExcel    end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Column Formats 
Delphi class TDataSetToExcel
    WriteFormat( ' General ' ); 
Delphi class TDataSetToExcel    WriteFormat(
' 0 ' ); 
Delphi class TDataSetToExcel    WriteFormat(
' ###,###,##0.00 ' ); 
Delphi class TDataSetToExcel    WriteFormat(
' dd-mmm-yyyy hh:mm:ss ' ); 
Delphi class TDataSetToExcel    WriteFormat(
' dd-mmm-yyyy ' ); 
Delphi class TDataSetToExcel    WriteFormat(
' hh:mm:ss ' ); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Dimensions 
Delphi class TDataSetToExcel
    WriteToken(XL_DIM, 8 ); 
Delphi class TDataSetToExcel    aDIMBuffer[
0 ] : =   0
Delphi class TDataSetToExcel    aDIMBuffer[
1 ] : =  Min(FDataSet.RecordCount,$FFFF); 
Delphi class TDataSetToExcel    aDIMBuffer[
2 ] : =   0
Delphi class TDataSetToExcel    aDIMBuffer[
3 ] : =  Min(FDataSet.FieldCount  -   1 ,$FFFF); 
Delphi class TDataSetToExcel    Blockwrite(FDataFile,aDIMBuffer,SizeOf(aDIMBuffer)); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Column Headers 
Delphi class TDataSetToExcel
     for  i : =   0  to FDataSet.FieldCount  -   1   do  begin 
Delphi class TDataSetToExcel      sStrData :
=  FDataSet.Fields[i].DisplayName; 
Delphi class TDataSetToExcel      iDataLen :
=  length(sStrData); 
Delphi class TDataSetToExcel      WriteToken(XL_STRING,iDataLen 
+   8 ); 
Delphi class TDataSetToExcel      WriteToken(FRow,i); 
Delphi class TDataSetToExcel      aAttributes[
1 ] : =  XL_HEADBOLD; 
Delphi class TDataSetToExcel      aAttributes[
2 ] : =  XL_HEADSHADE; 
Delphi class TDataSetToExcel      BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel      BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen)); 
Delphi class TDataSetToExcel      
if  iDataLen  >   0  then BlockWrite(FDataFile,sStrData[ 1 ],iDataLen); 
Delphi class TDataSetToExcel      aAttributes[
2 ] : =   0
Delphi class TDataSetToExcel    end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  Data Rows 
Delphi class TDataSetToExcel
     while  not FDataSet.Eof  do  begin 
Delphi class TDataSetToExcel      inc(FRow); 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel      
for  i : =   0  to FDataSet.FieldCount  -   1   do  begin 
Delphi class TDataSetToExcel        
case  FDataSet.FieldDefs[i].DataType of 
Delphi class TDataSetToExcel          ftBoolean, 
Delphi class TDataSetToExcel          ftWideString, 
Delphi class TDataSetToExcel          ftFixedChar, 
Delphi class TDataSetToExcel          ftString    : begin 
Delphi class TDataSetToExcel                          sStrData :
=  FDataSet.Fields[i].AsString; 
Delphi class TDataSetToExcel                          iDataLen :
=  length(sStrData); 
Delphi class TDataSetToExcel                          WriteToken(XL_STRING,iDataLen 
+   8 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =   0
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,iDataLen,SizeOf(iDataLen)); 
Delphi class TDataSetToExcel                          
if  iDataLen  >   0  then 
Delphi class TDataSetToExcel                            BlockWrite(FDataFile,sStrData[
1 ],iDataLen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel          ftAutoInc, 
Delphi class TDataSetToExcel          ftSmallInt, 
Delphi class TDataSetToExcel          ftInteger, 
Delphi class TDataSetToExcel          ftWord, 
Delphi class TDataSetToExcel          ftLargeInt  : begin 
Delphi class TDataSetToExcel                          fDblData :
=  FDataSet.Fields[i].AsFloat; 
Delphi class TDataSetToExcel                          iDataLen :
=  SizeOf(fDblData); 
Delphi class TDataSetToExcel                          WriteToken(XL_DOUBLE,
15 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =  XL_INTFORMAT; 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,fDblData,iDatalen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel          ftFloat, 
Delphi class TDataSetToExcel          ftCurrency, 
Delphi class TDataSetToExcel          ftBcd      : begin 
Delphi class TDataSetToExcel                          fDblData :
=  FDataSet.Fields[i].AsFloat; 
Delphi class TDataSetToExcel                          iDataLen :
=  SizeOf(fDblData); 
Delphi class TDataSetToExcel                          WriteToken(XL_DOUBLE,
15 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =  XL_DBLFORMAT; 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,fDblData,iDatalen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel          ftDateTime : begin 
Delphi class TDataSetToExcel                          fDblData :
=  FDataSet.Fields[i].AsFloat; 
Delphi class TDataSetToExcel                          iDataLen :
=  SizeOf(fDblData); 
Delphi class TDataSetToExcel                          WriteToken(XL_DOUBLE,
15 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =  XL_XDTFORMAT; 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,fDblData,iDatalen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel          ftDate     : begin 
Delphi class TDataSetToExcel                          fDblData :
=  FDataSet.Fields[i].AsFloat; 
Delphi class TDataSetToExcel                          iDataLen :
=  SizeOf(fDblData); 
Delphi class TDataSetToExcel                          WriteToken(XL_DOUBLE,
15 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =  XL_DTEFORMAT; 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,fDblData,iDatalen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel          ftTime     : begin 
Delphi class TDataSetToExcel                          fDblData :
=  FDataSet.Fields[i].AsFloat; 
Delphi class TDataSetToExcel                          iDataLen :
=  SizeOf(fDblData); 
Delphi class TDataSetToExcel                          WriteToken(XL_DOUBLE,
15 ); 
Delphi class TDataSetToExcel                          WriteToken(FRow,i); 
Delphi class TDataSetToExcel                          aAttributes[
1 ] : =  XL_TMEFORMAT; 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,aAttributes,SizeOf(aAttributes)); 
Delphi class TDataSetToExcel                          BlockWrite(FDataFile,fDblData,iDatalen); 
Delphi class TDataSetToExcel                        end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel        end; 
Delphi class TDataSetToExcel      end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel      FDataSet.Next; 
Delphi class TDataSetToExcel    end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel    
//  End of File 
Delphi class TDataSetToExcel
    WriteToken(XL_EOF, 0 ); 
Delphi class TDataSetToExcel    CloseFile(FDataFile); 
Delphi class TDataSetToExcel  except 
Delphi class TDataSetToExcel    bRetvar :
=   false
Delphi class TDataSetToExcel  end; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel  Result :
=  bRetvar; 
Delphi class TDataSetToExcelend; 
Delphi class TDataSetToExcel
Delphi class TDataSetToExcel
Delphi class TDataSetToExcelend.

你可能感兴趣的:(Delphi)