Freeform Excel Worksheet (No OLE or EXCEL required)

简介:
None.gif Question / Problem / Abstract:
None.gif
None.gifSee also : Article_3475.asp 
-  (TDataSet to Excel) 
None.gif
None.gifThis Class allows you to create an Excel Worksheet 
in  much the 
None.gifsame way 
as  you create a TStringGrid. ie. Cell[Column,Row]. 
None.gif
None.gif
-------------------------------------------------------------------------  
None.gifFeatures 
None.gif
-------------------------------------------------------------------------  
None.gif
None.gifFreeform cell access with DataType,FontIndex,FormatString, 
None.gifAlignment,Pattern and BorderStyle. 
None.gifNOTE : The col and row indexes are ZERO based 
in  the same way 
None.gif       
as  cells  in  a TStringGrid 
None.gif
None.gif
4  Mapable system fonts (Preset to dot.gif.) 
None.gif       Default   
=  Arial  10  regular         : FontIndex  =   0  
None.gif       Alt_1     
=  Arial  10  bold            : FontIndex  =   1  
None.gif       Alt_2     
=  Courier New  11  regular   : FontIndex  =   2  
None.gif       Alt_3     
=  Courier New  11  bold      : FontIndex  =   3  
None.gif
None.gifUser definable cell formats 
using  Excel syntax (Defaults  set  to dot.gif.) 
None.gif       String    
=   ' General '  
None.gif       Integer   
=   ' 0 '  
None.gif       Double    
=   ' ###,###,##0.00 '  
None.gif       DateTime  
=   ' dd-mmm-yyyy hh:mm:ss '  
None.gif       Date      
=   ' dd-mmm-yyyy '  
None.gif       Time      
=   ' hh:mm:ss '  
None.gif
None.gifSet individual Column Widths and Row Heights. 
None.gif
None.gif
-------------------------------------------------------------------------  
None.gifExample Code Snippet 
None.gif
-------------------------------------------------------------------------  
None.gif
None.gifuses MahWorksheet; 
None.gif
None.gifprocedure ExcelDemo; 
None.gifvar i : integer; 
None.gif    oWorksheet : TExcelWorkSheet; 
None.gif    oCell : TExcelCell; 
None.gifbegin 
None.gif  oWorksheet :
=  TExcelWorkSheet.Create; 
None.gif
None.gif  
//  Override mappable font 2 and 3 
None.gif
  oWorksheet.SetFont_2( ' Times Roman ' , 12 , [fsBold,fsUnderline],XL_BLUE); 
None.gif  oWorksheet.SetFont_3(
' Ms Serif ' );  //  accept other defaults 
None.gif
None.gif  
//  Set a column width 
None.gif
  oWorksheet.ColumnWidth( 3 , 50 );    //  Excel Col D 
None.gif
None.gif  
//  Set a row height 
None.gif
  oWorksheet.RowHeight( 25 , 40 );     //  Excel Row 26 
None.gif
  oWorksheet.RowHeight( 26 , 30 );     //  Excel Row 27 
None.gif
None.gif  
//  Set a cell via the procedural way 
None.gif
  oWorksheet.SetCell( 3 , 25 ,xlString, ' Hello World ' ,XL_FONT_2, 
None.gif                     
' General ' ,xalLeft, true ,[xbTop,xbBottom]); 
None.gif
None.gif  
//  Do the same thing via object oriented 
None.gif
  oCell : =  oWorksheet.NewCell( 3 , 16 ); 
None.gif  oCell.DataType :
=  xlDateTime; 
None.gif  oCell.Data :
=  Now; 
None.gif
None.gif  
//  Change the data in cell 
None.gif
  oCell : =  oWorksheet.GetCell( 3 , 25 ); 
None.gif  oCell.Data :
=   ' Hello World with Borders '
None.gif  oCell.BorderStyle :
=  [xbLeft,xbRight,xbTop,xbBottom]; 
None.gif  oCell.Align :
=  xalCenter; 
None.gif
None.gif  
//  Write out a column of integers 
None.gif
   for  i : =   1000  to  1255   do  begin 
None.gif    oCell :
=  oWorksheet.NewCell( 6 ,i  -   1000 ); 
None.gif    oCell.DataType :
=  xlInteger; 
None.gif    oCell.Data :
=  i; 
None.gif    oCell.FormatString :
=   ' ###,##0 ' ;   //  overide default '0' 
None.gif
    oCell.FontIndex : =  XL_FONT_1; 
None.gif  end; 
None.gif
None.gif  
//  Blank out a cell 
None.gif
  oWorksheet.BlankCell( 6 , 20 ); 
None.gif
None.gif  
//  Save our work 
None.gif
  oWorksheet.SaveToFile( ' c:\temp\test ' ); 
None.gif  FreeAndNil(oWorksheet); 
None.gifend;
None.gif

 

None.gif Answer:
None.gif
None.gifunit MahWorksheet; 
None.gif
interface  
None.gifuses Windows, Classes, SysUtils, Math, Variants, Graphics; 
None.gif
None.gif
//  ========================================================================= 
None.gif
//  Microsoft Excel Worksheet Class 
None.gif
//  Excel 2.1 BIFF2 Specification 
None.gif
//  
None.gif
//  Mike Heydon 2007 
None.gif
//  
None.gif
//  --------------------------------------------------------------------- 
None.gif
//  PUBLIC Methods 
None.gif
//  --------------------------------------------------------------------- 
None.gif
//  function GetCell(ACol,ARow : word) : TExcelCell; 
None.gif
//  function NewCell(ACol,ARow :word) : TExcelCell; 
None.gif
//  function GetFont_Default : TExcelFont; 
None.gif
//  function GetFont_1 : TExcelFont; 
None.gif
//  function GetFont_2 : TExcelFont; 
None.gif
//  function GetFont_3 : TExcelFont; 
None.gif
//  procedure SetFont_Default(const AFontName : string; 
None.gif
//                            AFontSize : byte = 10; 
None.gif
//                            AFontStyle : TFontStyles = []; 
None.gif
//                            AFontColor : word = 0); 
None.gif
//  procedure SetFont_1(const AFontName : string; 
None.gif
//                      AFontSize : byte = 10; 
None.gif
//                      AFontStyle : TFontStyles = []; 
None.gif
//                      AFontColor : word = 0); 
None.gif
//  procedure SetFont_2(const AFontName : string; 
None.gif
//                      AFontSize : byte = 10; 
None.gif
//                      AFontStyle : TFontStyles = []; 
None.gif
//                      AFontColor : word = 0); 
None.gif
//  procedure SetFont_3(const AFontName : string; 
None.gif
//                      AFontSize : byte = 10; 
None.gif
//                      AFontStyle : TFontStyles = []; 
None.gif
//                      AFontColor : word = 0); 
None.gif
//  procedure BlankCell(ACol,ARow : word); 
None.gif
//  procedure SetCell(ACol,ARow : word; 
None.gif
//                    ADataType : TExcelDataType; 
None.gif
//                    AData : Olevariant; 
None.gif
//                    AFontIndex : byte = 0; 
None.gif
//                    AFormatString : string = 'General'; 
None.gif
//                    AAlign : TExcelCellAlign = xalGeneral; 
None.gif
//                    AHasPattern : boolean = false; 
None.gif
//                    ABorderStyle : TExcelBorders = []); 
None.gif
//  procedure ColumnWidth(ACol : byte; AWidth : word); 
None.gif
//  procedure RowHeight(ARow : word; AHeight : byte); 
None.gif
//  procedure SaveToFile(const AFileName : string); 
None.gif
//  
None.gif
//  ========================================================================= 
None.gif

None.gif
None.gif
const  
None.gif     
//  Font Types - 4 Mapable Fonts - TExcelCell.FontIndex 
None.gif
     XL_FONT_DEFAULT  =   0
None.gif     XL_FONT_1       
=   1
None.gif     XL_FONT_2       
=   2
None.gif     XL_FONT_3       
=   3
None.gif
None.gif     
//  Font Colors 
None.gif
     XL_BLACK    : word  =  $ 0000
None.gif     XL_WHITE    : word 
=  $ 0001
None.gif     XL_RED      : word 
=  $ 0002
None.gif     XL_GREEN    : word 
=  $ 0003
None.gif     XL_BLUE     : word 
=  $ 0004
None.gif     XL_YELLOW   : word 
=  $ 0005
None.gif     XL_MAGENTA  : word 
=  $ 0006
None.gif     XL_CYAN     : word 
=  $ 0007
None.gif     XL_SYSTEM   : word 
=  $7FFF; 
None.gif
None.giftype 
None.gif     
//  Border Styles used by TExcelCell.BorderStyle 
None.gif
     TExcelBorderType  =  (xbLeft,xbRight,xbTop,xbBottom); 
None.gif     TExcelBorders    
=   set  of TExcelBorderType; 
None.gif
None.gif     
//  Data types used by TExcelCell.DataType 
None.gif
     TExcelDataType  =  (xlDouble,xlInteger,xlDate,xlTime, 
None.gif                       xlDateTime,xlString); 
None.gif
None.gif     
//  Cell Alignment used by TExcelCell.Align 
None.gif
     TExcelCellAlign  =  (xalGeneral,xalLeft,xalCenter,xalRight); 
None.gif
None.gif     
//  Structure Returned by GetFont_?() 
None.gif
     TExcelFont  =  record 
None.gif       FontName : 
string
None.gif       FontSize : 
byte
None.gif       FontStyle : TFontStyles; 
None.gif       FontColor : word; 
None.gif     end; 
None.gif
None.gif     
//  Cell object of a TExcelWorkSheet 
None.gif
     TExcelCell  =   class (TObject) 
None.gif     
private  
None.gif       FRow,FCol : word; 
None.gif     
public  
None.gif       DataType : TExcelDataType; 
None.gif       Data : Olevariant; 
None.gif       FontIndex : 
byte
None.gif       FormatString : 
string
None.gif       Align : TExcelCellAlign; 
None.gif       HasPattern : boolean; 
None.gif       BorderStyle : TExcelBorders; 
None.gif       constructor Create; 
None.gif     end; 
None.gif
None.gif     
//  Main TExcelWorkSheet Class 
None.gif
     TExcelWorkSheet  =   class (TObject) 
None.gif     
private  
None.gif       FFile : file; 
None.gif       FMaxRow,FMaxCol : word; 
None.gif       FRowHeights,FFontTable, 
None.gif       FUsedRows,FFormats, 
None.gif       FColWidths,FCells : TStringList; 
None.gif       function _GetFont(AFontNum : 
byte ) : TExcelFont; 
None.gif       function _CalcSize(AIndex : integer) : word; 
None.gif       procedure _SetColIdx(AListIdx : integer; ARow : word; 
None.gif                           
out  AFirst : word;  out  ALast : word); 
None.gif       procedure _SaveFontTable; 
None.gif       procedure _SaveColWidths; 
None.gif       procedure _SaveFormats; 
None.gif       procedure _SaveDimensions; 
None.gif       procedure _SaveRowBlocks; 
None.gif       procedure _SaveCells(ARowFr,ARowTo : word); 
None.gif       procedure _WriteToken(AToken : word; ADataLen : word); 
None.gif       procedure _WriteFont(
const  AFontName :  string ; AFontHeight, 
None.gif                            AAttribute : word); 
None.gif       procedure _SetFont(AFontNum : 
byte const  AFontName :  string
None.gif                          AFontSize : 
byte ; AFontStyle : TFontStyles; 
None.gif                          AFontColor : word); 
None.gif     
public  
None.gif       constructor Create; 
None.gif       destructor Destroy; 
override
None.gif       function GetCell(ACol,ARow : word) : TExcelCell; 
None.gif       function NewCell(ACol,ARow :word) : TExcelCell; 
None.gif       function GetFont_Default : TExcelFont; 
None.gif       function GetFont_1 : TExcelFont; 
None.gif       function GetFont_2 : TExcelFont; 
None.gif       function GetFont_3 : TExcelFont; 
None.gif       procedure SetFont_Default(
const  AFontName :  string
None.gif                                 AFontSize : 
byte   =   10
None.gif                                 AFontStyle : TFontStyles 
=  []; 
None.gif                                 AFontColor : word 
=   0 ); 
None.gif       procedure SetFont_1(
const  AFontName :  string
None.gif                           AFontSize : 
byte   =   10
None.gif                           AFontStyle : TFontStyles 
=  []; 
None.gif                           AFontColor : word 
=   0 ); 
None.gif       procedure SetFont_2(
const  AFontName :  string
None.gif                           AFontSize : 
byte   =   10
None.gif                           AFontStyle : TFontStyles 
=  []; 
None.gif                           AFontColor : word 
=   0 ); 
None.gif       procedure SetFont_3(
const  AFontName :  string
None.gif                           AFontSize : 
byte   =   10
None.gif                           AFontStyle : TFontStyles 
=  []; 
None.gif                           AFontColor : word 
=   0 ); 
None.gif       procedure BlankCell(ACol,ARow : word); 
None.gif       procedure SetCell(ACol,ARow : word; 
None.gif                         ADataType : TExcelDataType; 
None.gif                         AData : Olevariant; 
None.gif                         AFontIndex : 
byte   =   0
None.gif                         AFormatString : 
string   =   ' General '
None.gif                         AAlign : TExcelCellAlign 
=  xalGeneral; 
None.gif                         AHasPattern : boolean 
=   false
None.gif                         ABorderStyle : TExcelBorders 
=  []); 
None.gif       procedure ColumnWidth(ACol : 
byte ; AWidth : word); 
None.gif       procedure RowHeight(ARow : word; AHeight : 
byte ); 
None.gif       procedure SaveToFile(
const  AFileName :  string ); 
None.gif     end; 
None.gif
None.gif
None.gif
//  ----------------------------------------------------------------------------- 
None.gif
implementation 
None.gif
None.gif
const  
None.gif      
//  XL Tokens 
None.gif
      XL_DIM       : word  =  $ 0000
None.gif      XL_BOF       : word 
=  $ 0009
None.gif      XL_EOF       : word 
=  $000A; 
None.gif      XL_ROW       : word 
=  $ 0008
None.gif      XL_DOCUMENT  : word 
=  $ 0010
None.gif      XL_FORMAT    : word 
=  $001E; 
None.gif      XL_COLWIDTH  : word 
=  $ 0024
None.gif      XL_FONT      : word 
=  $ 0031
None.gif      XL_FONTCOLOR : word 
=  $ 0045
None.gif
None.gif      
//  XL Cell Types 
None.gif
      XL_INTEGER    =  $ 02
None.gif      XL_DOUBLE    
=  $ 03
None.gif      XL_STRING    
=  $ 04
None.gif
None.gif
None.giftype 
None.gif     
//  Used when writing in RowBlock mode 
None.gif
     TRowRec  =  packed record 
None.gif       RowIdx,FirstCell,LastCell : word; 
None.gif       Height : word; 
None.gif       NotUsed : word; 
None.gif       Defs : 
byte
None.gif       OSet : word; 
None.gif     end; 
None.gif
None.gif
//  ========================================================================= 
None.gif
//  Free Form Excel Spreadsheet 
None.gif
//  ========================================================================= 
None.gif
None.gif
//  ========================================================= 
None.gif
//  Create a ne Excel Cell Object and initialise defaults 
None.gif
//  ========================================================= 
None.gif
constructor TExcelCell.Create; 
None.gifbegin 
None.gif  inherited Create; 
None.gif
None.gif  FRow :
=   0
None.gif  FCol :
=   0
None.gif  DataType :
=  xlString; 
None.gif  FontIndex :
=   0
None.gif  FormatString :
=   ' General '
None.gif  Align :
=  xalGeneral; 
None.gif  HasPattern :
=   false
None.gif  BorderStyle :
=  []; 
None.gifend; 
None.gif
None.gif
//  ============================================== 
None.gif
//  Create and Destroy TExcelWorkSheet Class 
None.gif
//  ============================================== 
None.gif

None.gifconstructor TExcelWorkSheet.Create; 
None.gifbegin 
None.gif  inherited Create; 
None.gif
None.gif  FColWidths :
=  TStringList.Create; 
None.gif  FRowHeights :
=  TStringList.Create; 
None.gif  FUsedRows :
=  TStringList.Create; 
None.gif  FUsedRows.Sorted :
=   true
None.gif  FUsedRows.Duplicates :
=  dupIgnore; 
None.gif  FFormats :
=  TStringList.Create; 
None.gif  FFormats.Sorted :
=   true
None.gif  FFormats.Duplicates :
=  dupIgnore; 
None.gif  FCells :
=  TStringList.Create; 
None.gif  FCells.Sorted :
=   true
None.gif  FCells.Duplicates :
=  dupIgnore; 
None.gif  FFontTable :
=  TStringList.Create; 
None.gif  FFontTable.AddObject(
' Arial|10|0 ' ,nil); 
None.gif  FFontTable.AddObject(
' Arial|10|1 ' ,nil); 
None.gif  FFontTable.AddObject(
' Courier New|11|0 ' ,nil); 
None.gif  FFontTable.AddObject(
' Courier New|11|1 ' ,nil); 
None.gifend; 
None.gif
None.gif
None.gifdestructor TExcelWorkSheet.Destroy; 
None.gifvar i : integer; 
None.gifbegin 
None.gif  
for  i : =   0  to FCells.Count  -   1   do  
None.gif    TExcelCell(FCells.Objects[i]).Free; 
None.gif  FreeAndNil(FCells); 
None.gif  FreeAndNil(FColWidths); 
None.gif  FreeAndNil(FFormats); 
None.gif  FreeAndNil(FFontTable); 
None.gif  FreeAndNil(FUsedRows); 
None.gif  FreeAndNil(FRowHeights); 
None.gif
None.gif  inherited Destroy; 
None.gifend; 
None.gif
None.gif
//  ===================================================== 
None.gif
//  INTERNAL - Write out a Token and Data length record 
None.gif
//  ===================================================== 
None.gif

None.gifprocedure TExcelWorkSheet._WriteToken(AToken : word; ADataLen : word); 
None.gifvar aWord : array [
0 .. 1 ] of word; 
None.gifbegin 
None.gif  aWord[
0 ] : =  AToken; 
None.gif  aWord[
1 ] : =  ADataLen; 
None.gif  Blockwrite(FFile,aWord,SizeOf(aWord)); 
None.gifend; 
None.gif
None.gif
None.gif
//  ======================================= 
None.gif
//  INTERNAL - Write out a FONT record 
None.gif
//  ======================================= 
None.gif

None.gifprocedure TExcelWorksheet._WriteFont(
const  AFontName :  string
None.gif                                     AFontHeight,AAttribute : word); 
None.gifvar iLen : 
byte
None.gifbegin 
None.gif  AFontHeight :
=  AFontHeight  *   20
None.gif  _WriteToken(XL_FONT,
5   +  length(AFontName)); 
None.gif  BlockWrite(FFile,AFontHeight,
2 ); 
None.gif  BlockWrite(FFile,AAttribute,
2 ); 
None.gif  iLen :
=  length(AFontName); 
None.gif  BlockWrite(FFile,iLen,
1 ); 
None.gif  BlockWrite(FFile,AFontName[
1 ],iLen); 
None.gifend; 
None.gif
None.gif
None.gif
//  ==================================================================== 
None.gif
//  INTERNAL - Write out the Font Table 
None.gif
//  Also create a table of used rows and rows that have height changed. 
None.gif
//  Also set the Max Row and Col used for DIMENSION Record 
None.gif
//  Also create the user defined format strings table 
None.gif
//  ==================================================================== 
None.gif

None.gifprocedure TExcelWorkSheet._SaveFontTable; 
None.gifvar i,iAttr,iSize, 
None.gif    iRow,iIdx : integer; 
None.gif    iColor : word; 
None.gif    sKey,sName : 
string
None.gif    oCell : TexcelCell; 
None.gifbegin 
None.gif  FMaxRow :
=   0
None.gif  FMaxCol :
=   0
None.gif  FFormats.Clear; 
None.gif  FUsedRows.Clear; 
None.gif
None.gif  
//  Add any new formats - Get Unique Rows Used 
None.gif
   for  i : =   0  to FCells.Count  -   1   do  begin 
None.gif    oCell :
=  TExcelCell(FCells.Objects[i]); 
None.gif    
if  not SameText( ' General ' ,oCell.FormatString) then 
None.gif      FFormats.Add(oCell.FormatString); 
None.gif    FUsedRows.Add(FormatFloat(
' 00000 ' ,oCell.FRow)); 
None.gif    FMaxRow :
=  Min(oCell.FRow,$FFFF); 
None.gif    FMaxCol :
=  Min(oCell.FCol,$FFFF); 
None.gif  end; 
None.gif
None.gif  
//  Add any custom row heights 
None.gif
   for  i : =   0  to FRowHeights.Count  -   1   do  begin 
None.gif    iRow :
=  StrToInt(FRowHeights[i]); 
None.gif    sKey :
=  FormatFloat( ' 00000 ' ,iRow); 
None.gif    iSize :
=  word(FRowHeights.Objects[i]); 
None.gif
None.gif    
if  FUsedRows.Find(sKey,iIdx) then 
None.gif      FUsedRows.Objects[iIdx] :
=  TObject(iSize) 
None.gif    
else  
None.gif      FUsedRows.AddObject(sKey,TObject(iSize)); 
None.gif  end; 
None.gif
None.gif  
//  Write Font Table 
None.gif
   for  i : =   0  to FFontTable.Count  -   1   do  begin 
None.gif    sKey :
=  FFontTable[i]; 
None.gif    sName :
=  copy(sKey, 1 ,pos( ' | ' ,sKey)  -   1 ); 
None.gif    sKey :
=  copy(sKey,pos( ' | ' ,skey)  +   1 , 2096 ); 
None.gif    iSize :
=  StrToInt(copy(sKey, 1 ,pos( ' | ' ,sKey)  -   1 )); 
None.gif    iAttr :
=  StrToInt(copy(sKey,pos( ' | ' ,skey)  +   1 , 2096 )); 
None.gif    _WriteFont(sName,iSize,iAttr); 
None.gif    _WriteToken(XL_FONTCOLOR,
2 ); 
None.gif    iColor :
=  word(FFontTable.Objects[i]); 
None.gif    Blockwrite(FFile,iColor,
2 ); 
None.gif  end; 
None.gif
None.gifend; 
None.gif
None.gif
None.gif
//  ======================================================== 
None.gif
//  INTERNAL - Write out the default + user format strings 
None.gif
//  ======================================================== 
None.gif

None.gifprocedure TExcelWorkSheet._SaveFormats; 
None.gifvar i : integer; 
None.gif    iLen : 
byte
None.gif    sFormat : 
string
None.gifbegin 
None.gif  
//  FFormats already loaded in _SaveFontTable 
None.gif
  FFormats.Add( ' 0 ' );                      //  Integer Default 
None.gif
  FFormats.Add( ' ###,###,##0.00 ' );         //  Double Default 
None.gif
  FFormats.Add( ' dd-mmm-yyyy hh:mm:ss ' );   //  DateTime Default 
None.gif
  FFormats.Add( ' dd-mmm-yyyy ' );            //  Date Default 
None.gif
  FFormats.Add( ' hh:mm:ss ' );               //  Time default 
None.gif
None.gif  
//  Add General Default index 0 
None.gif
  sFormat : =   ' General '
None.gif  _WriteToken(XL_FORMAT,
1   +  length(sFormat)); 
None.gif  iLen :
=  length(sFormat); 
None.gif  Blockwrite(FFile,iLen,
1 ); 
None.gif  Blockwrite(FFile,sFormat[
1 ],iLen); 
None.gif
None.gif  
for  i : =   0  to FFormats.Count  -   1   do  begin 
None.gif    sFormat :
=  trim(FFormats[i]); 
None.gif
None.gif    
if  not SameText(sFormat, ' General ' ) then begin 
None.gif      _WriteToken(XL_FORMAT,
1   +  length(sFormat)); 
None.gif      iLen :
=  length(sFormat); 
None.gif      Blockwrite(FFile,iLen,
1 ); 
None.gif      Blockwrite(FFile,sFormat[
1 ],iLen); 
None.gif    end; 
None.gif  end; 
None.gifend; 
None.gif
None.gif
None.gif
//  ============================================= 
None.gif
//  INTERNAL - Write out DIMENSION Record 
None.gif
//  ============================================= 
None.gif

None.gifprocedure TExcelWorkSheet._SaveDimensions; 
None.gifvar aDIMBuffer : array [
0 .. 3 ] of word; 
None.gifbegin 
None.gif  _WriteToken(XL_DIM,
8 ); 
None.gif  aDIMBuffer[
0 ] : =   0
None.gif  aDIMBuffer[
1 ] : =  FMaxRow; 
None.gif  aDIMBuffer[
2 ] : =   0
None.gif  aDIMBuffer[
3 ] : =  FMaxCol; 
None.gif  Blockwrite(FFile,aDIMBuffer,SizeOf(aDIMBuffer)); 
None.gifend; 
None.gif
None.gif
None.gif
//  ===================================== 
None.gif
//  INTERNAL - Save Cell Records 
None.gif
//  ===================================== 
None.gif

None.gifprocedure TExcelWorkSheet._SaveCells(ARowFr,ARowTo : word); 
None.gifvar i,iIdx : integer; 
None.gif    iRow,iCol : word; 
None.gif    iDataLen,iFmtIdx, 
None.gif    iBorders, 
None.gif    iShade,iAlign, 
None.gif    iFntIdx,iFmtFnt : 
byte
None.gif    oCell : TExcelCell; 
None.gif    dDblData : 
double
None.gif    sStrData : 
string
None.gif    aAttributes : array [
0 .. 2 ] of  byte
None.gifbegin 
None.gif  aAttributes[
0 ] : =   0 ;   //  No reference to XF 
None.gif

None.gif  
for  i : =   0  to FCells.Count  -   1   do  begin 
None.gif    oCell :
=  TExcelCell(FCells.Objects[i]); 
None.gif    
//  Row and Col resolve 
None.gif
    iRow : =  oCell.FRow; 
None.gif
None.gif    
if  iRow  >=  ARowFr then begin 
None.gif      
if  iRow  >  ARowTo then  break
None.gif      iCol :
=  oCell.FCol; 
None.gif      
if  iCol  >   255  then iCol : =   255
None.gif
None.gif      
//  Format IDX resolve - set defaults for numerics/dates 
None.gif
      iFmtIdx : =   0
None.gif
None.gif      
if  SameText( ' General ' ,oCell.FormatString) and 
None.gif         (oCell.DataType 
<>  xlString) then begin 
None.gif      
case  oCell.DataType of 
None.gif        xlInteger   : oCell.FormatString :
=   ' 0 '
None.gif        xlDateTime  : oCell.FormatString :
=   ' dd-mmm-yyyy hh:mm:ss '
None.gif        xlTime      : oCell.FormatString :
=   ' hh:mm:ss '
None.gif        xlDate      : oCell.FormatString :
=   ' dd-mmm-yyyy '
None.gif        xlDouble    : oCell.FormatString :
=   ' ###,###,##0.00 '
None.gif        end; 
None.gif      end; 
None.gif
None.gif      
if  FFormats.Find(oCell.FormatString,iIdx) then begin 
None.gif        
if  iIdx  >   62  then iIdx : =   62
None.gif        iFmtIdx :
=  iIdx  +   1
None.gif      end; 
None.gif
None.gif      
//  Font IDX resolve and or with format 
None.gif
      iFntIdx : =  oCell.FontIndex shl  6
None.gif      iFmtFnt :
=  iFmtIdx or iFntIdx; 
None.gif
None.gif      
//  Shading and alignment and borders 
None.gif
      iShade : =   0
None.gif      
if  oCell.HasPattern then iShade : =  $ 80
None.gif      iAlign :
=   byte (oCell.Align); 
None.gif      iBorders :
=   0
None.gif      
if  xbLeft  in  oCell.BorderStyle then iBorders : =  iBorders or $ 08
None.gif      
if  xbRight  in  oCell.BorderStyle then iBorders : =  iBorders or $ 10
None.gif      
if  xbTop  in  oCell.BorderStyle then iBorders : =  iBorders or $ 20
None.gif      
if  xbBottom  in  oCell.BorderStyle then iBorders : =  iBorders or $ 40
None.gif
None.gif      
//  Resolve Data Type 
None.gif
       case  oCell.DataType of 
None.gif        xlInteger, 
None.gif        xlDateTime, 
None.gif        xlTime, 
None.gif        xlDate, 
None.gif        xlDouble  : begin 
None.gif                      dDblData :
=  oCell.Data; 
None.gif                      iDataLen :
=  SizeOf( double ); 
None.gif                      _WriteToken(XL_DOUBLE,
15 ); 
None.gif                      _WriteToken(iRow,iCol); 
None.gif                      aAttributes[
1 ] : =  iFmtFnt; 
None.gif                      aAttributes[
2 ] : =  iAlign or iShade or iBorders; 
None.gif                      Blockwrite(FFile,aAttributes,SizeOf(aAttributes)); 
None.gif                      Blockwrite(FFile,dDblData,iDatalen); 
None.gif                    end; 
None.gif
None.gif        xlString  : begin 
None.gif                      sStrData :
=  oCell.Data; 
None.gif                      iDataLen :
=  length(sStrData); 
None.gif                      _WriteToken(XL_STRING,iDataLen 
+   8 ); 
None.gif                      _WriteToken(iRow,iCol); 
None.gif                      aAttributes[
1 ] : =  iFmtFnt; 
None.gif                      aAttributes[
2 ] : =  iAlign or iShade or iBorders; 
None.gif                      Blockwrite(FFile,aAttributes,SizeOf(aAttributes)); 
None.gif                      Blockwrite(FFile,iDataLen,SizeOf(iDataLen)); 
None.gif                      
if  iDataLen  >   0  then Blockwrite(FFile,sStrData[ 1 ],iDataLen); 
None.gif                    end; 
None.gif      end; 
None.gif    end; 
None.gif  end; 
None.gifend; 
None.gif
None.gif
None.gif
None.gif
//  ======================================================= 
None.gif
//  INTERNAL - Calulate the size of the cell record + data 
None.gif
//  ======================================================= 
None.gif

None.giffunction TExcelWorkSheet._CalcSize(AIndex : integer) : word; 
None.gifvar iResult : word; 
None.gif    oCell : TExcelCell; 
None.gifbegin 
None.gif  iResult :
=   0
None.gif  oCell :
=  TExcelCell(FCells.Objects[AIndex]); 
None.gif
None.gif  
case  oCell.DataType of 
None.gif    xlInteger, 
None.gif    xlDateTime, 
None.gif    xlTime, 
None.gif    xlDate, 
None.gif    xlDouble  : iResult :
=   19
None.gif
None.gif    xlString  : iResult :
=  length(oCell.Data)  +   12
None.gif  end; 
None.gif
None.gif  Result :
=  iResult; 
None.gifend; 
None.gif
None.gif
None.gif
//  ================================================================ 
None.gif
//  INTERNAL - Fint fisrt and last used column ro ROW Record 
None.gif
//  Only used when writing in RowBlock mode (_SaveRowBlocks) 
None.gif
//  ================================================================ 
None.gif

None.gifprocedure TExcelWorkSheet._SetColIdx(AListIdx : integer; 
None.gif                                     ARow : word; 
None.gif                                     
out  AFirst : word; 
None.gif                                     
out  ALast : word); 
None.gifvar sKey : 
string
None.gif    i,iIdx, 
None.gif    iRow : integer; 
None.gif    iDataSize : word; 
None.gifbegin 
None.gif  FUsedRows.Objects[AListIdx] :
=  nil; 
None.gif  iDataSize :
=   0
None.gif  iIdx :
=   - 1
None.gif  AFirst :
=   0
None.gif  ALast :
=   0
None.gif
None.gif  
//  Find first row-col combo 
None.gif
   for  i : =   0  to FCells.Count  -   1   do  begin 
None.gif    sKey :
=  FCells[i]; 
None.gif    iRow :
=  StrToInt( ' $ '   +  copy(sKey, 1 , 4 )); 
None.gif
None.gif    
if  iRow  =  ARow then begin 
None.gif      iIdx :
=  i; 
None.gif      
break
None.gif    end; 
None.gif  end; 
None.gif
None.gif  
//  Found rows? 
None.gif
   if  iIdx  >=   0  then begin 
None.gif    AFirst :
=  StrToInt( ' $ '   +  copy(sKey, 5 , 4 )); 
None.gif    ALast :
=  AFirst; 
None.gif    inc(iDataSize,_CalcSize(iIdx)); 
None.gif    inc(iIdx); 
None.gif
None.gif    
//  Repeat until last row-col 
None.gif
     if  iIdx  <  FCells.Count then begin 
None.gif      
while   true   do  begin 
None.gif        sKey :
=  FCells[iIdx]; 
None.gif        iRow :
=  StrToInt( ' $ '   +  copy(sKey, 1 , 4 )); 
None.gif
None.gif        
if  iRow  =  ARow then begin 
None.gif          ALast :
=  StrToInt( ' $ '   +  copy(sKey, 5 , 4 )); 
None.gif          inc(iDataSize,_CalcSize(iIdx)); 
None.gif        end 
None.gif        
else  
None.gif          
break
None.gif
None.gif        inc(iIdx); 
None.gif        
if  iIdx  =  FCells.Count then  break
None.gif      end; 
None.gif    end; 
None.gif
None.gif    inc(ALast); 
None.gif    FUsedRows.Objects[AListIdx] :
=  TObject(iDataSize); 
None.gif  end; 
None.gifend; 
None.gif
None.gif
//  ================================================================== 
None.gif
//  INTERNAL - Write out row/cells in ROWBLOCK format 
None.gif
//  NOTE : This mode is onley used when at least 1 row has 
None.gif
//  had it's height set by SetRowHeight(), otherwise _SaveCell() 
None.gif
//  is run from first to last cells in sheet (faster) 
None.gif
//  ================================================================== 
None.gif

None.gifprocedure TExcelWorkSheet._SaveRowBlocks; 
None.gif
const  aWINDOW1 : array [ 0 .. 13 ] of  byte   =  ($3d,$ 00 ,$0A,$ 00 ,$ 68 ,$ 01 ,$D2, 
None.gif                                          $
00 ,$DC,$ 41 ,$B8,$ 29 ,$ 00 ,$ 00 ); 
None.gifvar i,iArrIdx, 
None.gif    iIdx,iCount,iLoop : integer; 
None.gif    iFirst,iLast,iHeight : word; 
None.gif    aAttributes : array [
0 .. 2 ] of  byte
None.gif    aRowRec : array of TRowRec; 
None.gifbegin 
None.gif  aAttributes[
0 ] : =   0 ;   //  No reference to XF 
None.gif
  iLoop : =   0
None.gif
None.gif  
//  Process in blocks of 32 rows 
None.gif
   while   true   do  begin 
None.gif    iArrIdx :
=   0
None.gif
None.gif    
if  iLoop  +   31   <  FUsedRows.Count  -   1  then begin 
None.gif      iCount :
=  iLoop  +   31
None.gif      SetLength(aRowRec,
32 ); 
None.gif    end 
None.gif    
else  begin 
None.gif      iCount :
=  FUsedRows.Count  -   1
None.gif      SetLength(aRowRec,iCount 
-  iLoop  +   1 ); 
None.gif    end; 
None.gif
None.gif    
for  i : =  iLoop to iCount  do  begin 
None.gif      aRowRec[iArrIdx].RowIdx :
=  StrToInt(FUsedRows[i]); 
None.gif      _SetColIdx(i,aRowRec[iArrIdx].RowIdx,iFirst,iLast); 
None.gif      aRowRec[iArrIdx].FirstCell :
=  iFirst; 
None.gif      aRowRec[iArrIdx].LastCell :
=  iLast; 
None.gif      aRowRec[iArrIdx].Defs :
=   0
None.gif      aRowRec[iArrIdx].NotUsed :
=   0
None.gif      aRowRec[iArrIdx].Height :
=  $80FF; 
None.gif      iIdx :
=  FRowHeights.IndexOf(IntToStr(aRowRec[iArrIdx].RowIdx)); 
None.gif
None.gif      
if  iIdx  <>   - 1  then begin 
None.gif        iHeight :
=  word(FRowHeights.Objects[iIdx]); 
None.gif        
if  iHeight  <>   0  then aRowRec[iArrIdx].Height : =  iHeight  *   20
None.gif      end; 
None.gif
None.gif      
if  iArrIdx  =   0  then 
None.gif        aRowRec[iArrIdx].OSet :
=  (iCount  -  iLoop)  *  
None.gif                                 (SizeOf(TRowRec) 
+   4
None.gif      
else  
None.gif        aRowRec[iArrIdx].OSet :
=  word(FUsedRows.Objects[i  -   1 ]); 
None.gif
None.gif      _WriteToken(XL_ROW,SizeOf(TRowRec)); 
None.gif      BlockWrite(FFile,aRowRec[iArrIdx],SizeOf(TRowRec)); 
None.gif      inc(iArrIdx); 
None.gif    end; 
None.gif
None.gif    _SaveCells(aRowRec[
0 ].RowIdx,aRowRec[high(aRowRec)].RowIdx); 
None.gif    SetLength(aRowRec,
0 ); 
None.gif    iLoop :
=  iLoop  +  (iCount  -  iLoop  +   1 ); 
None.gif    
if  iLoop  >=  FUsedRows.Count  -   1  then  break
None.gif  end; 
None.gif
None.gif  
//  Write WINDOW1 Record 
None.gif
  BlockWrite(FFile,aWINDOW1,SizeOf(aWINDOW1)); 
None.gifend; 
None.gif
None.gif
None.gif
//  ========================================================= 
None.gif
//  INTERNAL - Write out non-default column widths as 
None.gif
//  set by ColumnWidth() 
None.gif
//  ========================================================= 
None.gif

None.gifprocedure TExcelWorkSheet._SaveColWidths; 
None.gifvar i : integer; 
None.gif    iCol : 
byte
None.gif    iWidth : word; 
None.gifbegin 
None.gif  
for  i : =   0  to FColWidths.Count  -   1   do  begin 
None.gif    iCol :
=  StrToInt(FColWidths[i]); 
None.gif    iWidth :
=   256   *  word(FColWidths.Objects[i]); 
None.gif    _WriteToken(XL_COLWIDTH,
4 ); 
None.gif    Blockwrite(FFile,iCol,
1 ); 
None.gif    Blockwrite(FFile,iCol,
1 ); 
None.gif    Blockwrite(FFile,iWidth,
2 ); 
None.gif  end; 
None.gifend; 
None.gif
None.gif
None.gif
//  ======================================================= 
None.gif
//  INTERNAL Base Font Setting Method - Default and 1..3 
None.gif
//  ======================================================= 
None.gif

None.gifprocedure TExcelWorkSheet._SetFont(AFontNum : 
byte
None.gif                                   
const  AFontName :  string
None.gif                                   AFontSize : 
byte
None.gif                                   AFontStyle : TFontStyles; 
None.gif                                   AFontColor : word); 
None.gifvar sKey : 
string
None.gif    iAttr : integer; 
None.gifbegin 
None.gif  iAttr :
=   0
None.gif  
if  fsBold  in  AFontStyle then iAttr : =  iAttr or  1
None.gif  
if  fsItalic  in  AFontStyle then iAttr : =  iAttr or  2
None.gif  
if  fsUnderline  in  AFontStyle then iAttr : =  iAttr or  4
None.gif  
if  fsStrikeOut  in  AFontStyle then iAttr : =  iAttr or  8
None.gif  sKey :
=  trim(AFontName)  +   ' | '   +  IntToStr(AFontSize)  +  
None.gif          
' | '   +  IntToStr(iAttr); 
None.gif  FFontTable[AFontNum] :
=  sKey; 
None.gif  FFontTable.Objects[AFontNum] :
=  TObject(AFontColor); 
None.gifend; 
None.gif
None.gif
None.gif
//  ======================================================= 
None.gif
//  INTERNAL Base Font Get Info Method - Default and 1..3 
None.gif
//  ======================================================= 
None.gif

None.giffunction TExcelWorkSheet._GetFont(AFontNum : 
byte ) : TExcelFont; 
None.gifvar rResult : TExcelFont; 
None.gif    sKey : 
string
None.gif    iStyle : integer; 
None.gifbegin 
None.gif  rResult.FontStyle :
=  []; 
None.gif  
if  AFontNum  >   3  then AFontNum : =   3
None.gif  sKey :
=  FFontTable[AFontNum]; 
None.gif  rResult.FontName :
=  copy(skey, 1 ,pos( ' | ' ,sKey)  -   1 ); 
None.gif  sKey :
=  copy(sKey,pos( ' | ' ,skey)  +   1 , 2096 ); 
None.gif  rResult.FontSize :
=  StrToInt(copy(sKey, 1 ,pos( ' | ' ,sKey)  -   1 )); 
None.gif  iStyle :
=  StrToInt(copy(sKey,pos( ' | ' ,skey)  +   1 , 2096 )); 
None.gif  rResult.FontColor :
=  integer(FFontTable.Objects[AFontNum]); 
None.gif  
if  iStyle and  1   =   1  then include(rResult.FontStyle,fsBold); 
None.gif  
if  iStyle and  2   =   2  then include(rResult.FontStyle,fsItalic); 
None.gif  
if  iStyle and  4   =   4  then include(rResult.FontStyle,fsUnderline); 
None.gif  
if  iStyle and  8   =   8  then include(rResult.FontStyle,fsStrikeOut); 
None.gif
None.gif  Result :
=  rResult; 
None.gifend; 
None.gif
None.gif
None.gif
//  ===================================== 
None.gif
//  PUBLIC - Font Setting Methods 
None.gif
//  ===================================== 
None.gif

None.gifprocedure TExcelWorkSheet.SetFont_Default(
const  AFontName :  string
None.gif                                          AFontSize : 
byte   =   10
None.gif                                          AFontStyle : TFontStyles 
=  []; 
None.gif                                          AFontColor : word 
=   0 ); 
None.gifbegin 
None.gif  _SetFont(XL_FONT_DEFAULT,AFontName,AFontSize,AFontStyle,AFontColor); 
None.gifend; 
None.gif
None.gif
None.gifprocedure TExcelWorkSheet.SetFont_1(
const  AFontName :  string
None.gif                                    AFontSize : 
byte   =   10
None.gif                                    AFontStyle : TFontStyles 
=  []; 
None.gif                                    AFontColor : word 
=   0 ); 
None.gifbegin 
None.gif  _SetFont(XL_FONT_1,AFontName,AFontSize,AFontStyle,AFontColor); 
None.gifend; 
None.gif
None.gifprocedure TExcelWorkSheet.SetFont_2(
const  AFontName :  string
None.gif                                    AFontSize : 
byte   =   10
None.gif                                    AFontStyle : TFontStyles 
=  []; 
None.gif                                    AFontColor : word 
=   0 ); 
None.gifbegin 
None.gif  _SetFont(XL_FONT_2,AFontName,AFontSize,AFontStyle,AFontColor); 
None.gifend; 
None.gif
None.gifprocedure TExcelWorkSheet.SetFont_3(
const  AFontName :  string
None.gif                                    AFontSize : 
byte   =   10
None.gif                                    AFontStyle : TFontStyles 
=  []; 
None.gif                                    AFontColor : word 
=   0 ); 
None.gifbegin 
None.gif  _SetFont(XL_FONT_3,AFontName,AFontSize,AFontStyle,AFontColor); 
None.gifend; 
None.gif
None.gif
None.gif
//  ====================================== 
None.gif
//  PUBLIC - Font Get Information Methods 
None.gif
//  ====================================== 
None.gif

None.giffunction TExcelWorkSheet.GetFont_Default : TExcelFont; 
None.gifbegin 
None.gif  Result :
=  _GetFont(XL_FONT_DEFAULT); 
None.gifend; 
None.gif
None.giffunction TExcelWorkSheet.GetFont_1 : TExcelFont; 
None.gifbegin 
None.gif  Result :
=  _GetFont(XL_FONT_1); 
None.gifend; 
None.gif
None.giffunction TExcelWorkSheet.GetFont_2 : TExcelFont; 
None.gifbegin 
None.gif  Result :
=  _GetFont(XL_FONT_2); 
None.gifend; 
None.gif
None.giffunction TExcelWorkSheet.GetFont_3 : TExcelFont; 
None.gifbegin 
None.gif  Result :
=  _GetFont(XL_FONT_3); 
None.gifend; 
None.gif
None.gif
None.gif
//  ===================================== 
None.gif
//  Set a single column width 
None.gif
//  ===================================== 
None.gif

None.gifprocedure TExcelWorkSheet.ColumnWidth(ACol : 
byte ; AWidth : word); 
None.gifvar sKey : 
string
None.gif    iIdx : integer; 
None.gifbegin 
None.gif  sKey :
=  IntToStr(ACol); 
None.gif  iIdx :
=  FColWidths.IndexOf(sKey); 
None.gif  
if  AWidth  >   255  then AWidth : =   255
None.gif
None.gif  
if  iIdx  <>   - 1  then 
None.gif    FColWidths.Objects[iIdx] :
=  TObject(AWidth) 
None.gif  
else  
None.gif    FColWidths.AddObject(sKey,TObject(AWidth)); 
None.gifend; 
None.gif
None.gif
None.gif
//  ============================ 
None.gif
//  Set a single row height 
None.gif
//  ============================ 
None.gif

None.gifprocedure TExcelWorkSheet.RowHeight(ARow : word; AHeight : 
byte ); 
None.gifvar sKey : 
string
None.gif    iIdx : integer; 
None.gifbegin 
None.gif  sKey :
=  IntToStr(ARow); 
None.gif  iIdx :
=  FRowHeights.IndexOf(sKey); 
None.gif
None.gif  
if  iIdx  <>   - 1  then 
None.gif    FRowHeights.Objects[iIdx] :
=  TObject(AHeight) 
None.gif  
else  
None.gif    FRowHeights.AddObject(sKey,TObject(AHeight)); 
None.gifend; 
None.gif
None.gif
None.gif
//  ================================================= 
None.gif
//  Get a cell info object 
None.gif
//  NOTE : A reference to the object is returned. 
None.gif
//         No need for user to FREE the object 
None.gif
//  ================================================= 
None.gif

None.giffunction TExcelWorkSheet.GetCell(ACol,ARow :word) : TExcelCell; 
None.gifvar oResult : TExcelCell; 
None.gif    sKey : 
string
None.gif    iIndex : integer; 
None.gifbegin 
None.gif  sKey :
=  IntToHex(ARow, 4 +  IntToHex(ACol, 4 ); 
None.gif
None.gif  
//  Existing ? 
None.gif
   if  FCells.Find(sKey,iIndex) then 
None.gif    oResult :
=  TExcelCell(FCells.Objects[iIndex]) 
None.gif  
else  
None.gif    oResult :
=  nil; 
None.gif
None.gif  Result :
=  oResult; 
None.gifend; 
None.gif
None.gif
//  ==================================================== 
None.gif
//  Add or replace a cell in the worksheet 
None.gif
//  NOTE : A reference to the object is returned. 
None.gif
//         No need for user to FREE the object 
None.gif
//  ==================================================== 
None.gif

None.giffunction TExcelWorkSheet.NewCell(ACol,ARow :word) : TExcelCell; 
None.gifvar oResult : TExcelCell; 
None.gif    sKey : 
string
None.gif    iIndex : integer; 
None.gifbegin 
None.gif  oResult :
=  TExcelCell.Create; 
None.gif  oResult.FRow :
=  ARow; 
None.gif  oResult.FCol :
=  ACol; 
None.gif  
if  ACol  >   255  then oResult.FCol : =   255
None.gif  sKey :
=  IntToHex(ARow, 4 +  IntToHex(ACol, 4 ); 
None.gif
None.gif  
//  Existing ? 
None.gif
   if  FCells.Find(sKey,iIndex) then begin 
None.gif    TExcelCell(FCells.Objects[iIndex]).Free; 
None.gif    FCells.Objects[iIndex] :
=  oResult; 
None.gif  end 
None.gif  
else  
None.gif    FCells.AddObject(sKey,oResult); 
None.gif
None.gif  Result :
=  oResult; 
None.gifend; 
None.gif
None.gif
None.gif
//  ========================================= 
None.gif
//  Blanks out a cell in the worksheet 
None.gif
//  ========================================= 
None.gif

None.gifprocedure TExcelWorkSheet.BlankCell(ACol,ARow :word); 
None.gifvar sKey : 
string
None.gif    iIndex : integer; 
None.gifbegin 
None.gif  sKey :
=  IntToHex(ARow, 4 +  IntToHex(ACol, 4 ); 
None.gif
None.gif  
//  Existing ? 
None.gif
   if  FCells.Find(sKey,iIndex) then begin 
None.gif    TExcelCell(FCells.Objects[iIndex]).Free; 
None.gif    FCells.Delete(iIndex); 
None.gif  end; 
None.gifend; 
None.gif
None.gif
//  =========================================== 
None.gif
//  Procedural way to add or change a cell 
None.gif
//  =========================================== 
None.gif

None.gifprocedure TExcelWorkSheet.SetCell(ACol,ARow : word; 
None.gif                                  ADataType : TExcelDataType; 
None.gif                                  AData : Olevariant; 
None.gif                                  AFontIndex : 
byte   =   0
None.gif                                  AFormatString : 
string   =   ' General '
None.gif                                  AAlign : TExcelCellAlign 
=  xalGeneral; 
None.gif                                  AHasPattern : boolean 
=   false
None.gif                                  ABorderStyle : TExcelBorders 
=  []); 
None.gifvar oCell : TExcelCell; 
None.gif    sKey : 
string
None.gif    iIndex : integer; 
None.gifbegin 
None.gif  oCell :
=  TExcelCell.Create; 
None.gif  oCell.FRow :
=  ARow; 
None.gif  oCell.FCol :
=  ACol; 
None.gif  
if  ACol  >   255  then ACol : =   255
None.gif  oCell.DataType :
=  ADataType; 
None.gif  oCell.Data :
=  AData; 
None.gif  oCell.FontIndex :
=  AFontIndex; 
None.gif  
if  AFontIndex  >   3  then oCell.FontIndex : =   3
None.gif
None.gif  oCell.FormatString :
=  AFormatString; 
None.gif  oCell.Align :
=  AAlign; 
None.gif  oCell.HasPattern :
=  AHasPattern; 
None.gif  oCell.BorderStyle :
=  ABorderStyle; 
None.gif  sKey :
=  IntToHex(ARow, 4 +  IntToHex(ACol, 4 ); 
None.gif
None.gif  
//  Existing ? 
None.gif
   if  FCells.Find(sKey,iIndex) then begin 
None.gif    TExcelCell(FCells.Objects[iIndex]).Free; 
None.gif    FCells.Objects[iIndex] :
=  oCell; 
None.gif  end 
None.gif  
else  
None.gif    FCells.AddObject(sKey,oCell); 
None.gifend; 
None.gif
None.gif
//  ==================================== 
None.gif
//  Save Worksheet as an XLS file 
None.gif
//  ==================================== 
None.gif

None.gifprocedure TExcelWorkSheet.SaveToFile(
const  AFileName :  string ); 
None.gifvar aWord : array [
0 .. 1 ] of word; 
None.gifbegin 
None.gif  AssignFile(FFile,ChangeFileExt(AFileName,
' .xls ' )); 
None.gif  Rewrite(FFile,
1 ); 
None.gif
None.gif  
//  BOF 
None.gif
  _WriteToken(XL_BOF, 4 ); 
None.gif  aWord[
0 ] : =   0
None.gif  aWord[
1 ] : =  XL_DOCUMENT; 
None.gif  Blockwrite(FFile,aWord,SizeOf(aWord)); 
None.gif
None.gif  
//  FONT 
None.gif
  _SaveFontTable; 
None.gif
None.gif  
//  COLWIDTH 
None.gif
  _SaveColWidths; 
None.gif
None.gif  
//  COLFORMATS 
None.gif
  _SaveFormats; 
None.gif
None.gif  
//  DIMENSIONS 
None.gif
  _SaveDimensions; 
None.gif
None.gif  
//  CELLS 
None.gif
   if  FRowHeights.Count  >   0  then 
None.gif    _SaveRowBlocks          
//  Slower 
None.gif
   else  
None.gif    _SaveCells(
0 ,$FFFF);     //  Faster 
None.gif
None.gif  
//  EOF 
None.gif
  _WriteToken(XL_EOF, 0 ); 
None.gif  CloseFile(FFile); 
None.gifend; 
None.gif
None.gifend. 
None.gif


    本文转自 OldHawk  博客园博客,原文链接: http://www.cnblogs.com/taobataoma/archive/2007/06/13/782376.html ,如需转载请自行联系原作者

相关文章
pandas读excel类型文件报错: xlrd.biffh.XLRDError: Excel xlsx file; not supported
pandas读excel类型文件报错: xlrd.biffh.XLRDError: Excel xlsx file; not supported
|
Python
Python无法打开.xlsx文件:xlrd.biffh.XLRDError: Excel xlsx file; not supported
了解Python无法打开.xlsx文件:xlrd.biffh.XLRDError: Excel xlsx file; not supported。
224 0
Python无法打开.xlsx文件:xlrd.biffh.XLRDError: Excel xlsx file; not supported
Python+Excel:xlrd.biffh.XLRDError: Excel xlsx file; not supported,两种解决方案
Python+Excel:xlrd.biffh.XLRDError: Excel xlsx file; not supported,两种解决方案
251 0
Python+Excel:xlrd.biffh.XLRDError: Excel xlsx file; not supported,两种解决方案