Field-Map Scripting

4.6.4.20 HeaderAndFooterExample

Header and footer example


An Excel file with 7 sheets is created in this example. There is a Content sheets and 6 sheets with header and footer examples.



The code of the example is as follows:

Var
  xls :TExcelWrapper;
  lst: TStringListWrapper;
  Col, Row, Cols, Rows:integer; 
Begin

  xls:=TExcelWrapper.Create;
  try
    xls.CreateFile('HeaderFooterExample');
    
   {------------------Formats-------------------}
   xls.AddUserFormat('Title_h1',14,'BOLD','0,0,0',0,0,'CENTER','CENTER',false);     
    
   {------------------Sheet: Content }
    xls.AddSheet('',true);
    {Create table of content}
    xls.CreateHeaderOfTableContent;       
    lst:=TStringListWrapper.Create;
    lst.Add(format('%s|%s',['Simple header, no footer','Example1']));
    lst.Add(format('%s|%s',['Simple header, simple footer','Example2']));
    lst.Add(format('%s|%s',['Header with codes, footer with codes 1','Example3']));
    lst.Add(format('%s|%s',['Header with codes, footer with codes 2','Example4']));
    lst.Add(format('%s|%s',['Set header and footer','Example5'])); 
    lst.Add(format('%s|%s',['First header and footer','Example6']));           
    xls.CreateTableOfContent(lst);    

   {------------------Sheet: Simple header, no footer}
    xls.AddSheet('Example1',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 1: Simple header, no footer','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer}
    xls.SetHeader('My header');
    {no expliciety defined footer}  
    
    {managing formats}
    {setting cell values}
    {etc....}
    
   {------------------Sheet: Simple header, simple footer}
    xls.AddSheet('Example2',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 2: Simple header, simple footer','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer}
    xls.SetHeader('&L&"Calibri,Italic"&9My header'); 
    xls.SetFooter('My footer');  
    
    {managing formats}
    {setting cell values}
    {etc....}    

    {------------------Sheet: Header with codes, footer with codes 1}
    xls.AddSheet('Example3',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 3: Header with codes, footer with codes 1','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer}
    xls.SetHeader(format('&C&"Calibri,Italic"&9Project: %s',[ProjectName]));
    xls.SetFooter('&C&"Calibri,Italic"&9My footer');
    
    {managing formats}
    {setting cell values}
    {etc....}
    
    {------------------Sheet: Header with codes, footer with codes 2}
    xls.AddSheet('Example4',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 4: Header with codes, footer with codes 1','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer}
    xls.SetHeader(format('&L&"Calibri,Italic"&9Project: %s',[ProjectName])); 
    xls.SetFooter('&R&"Calibri,Italic"&9My footer');  
    
    {managing formats}
    {setting cell values}
    {etc....}    
    
   {------------------Sheet: SetHeaderAndFooter}
    xls.AddSheet('Example5',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 5: SetHeaderAndFooter','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer}
    xls.SetHeaderAndFooter('&"Calibri,Italic"&9My left header','','My right header','My left footer','','&"Calibri,Italic"&9My right footer');    

    {managing formats}
    {setting cell values}
    {etc....}     

   {------------------Sheet: First header and footer}
    xls.AddSheet('Example6',true);
    Cols:=8;
    Row:=1;
    
    {hyperlink to Content}
    xls.AddBackToTableOfContent(Row,2,Cols);    
    Inc(Row);    
    
    {Title}
    xls.SetValueAndMerge(Row,1,Row, Cols,'Example 6: First header and footer','Title_h1','','');  
    inc(Row); inc(Row);        
              
    {header and footer} 
    xls.SetHeader('&L&"Calibri,Italic"&9My header');
    xls.SetFooter('&R&"Calibri,Italic"&9My footer');
    xls.SetFirstHeaderAndFooter('My first left header','','My first right header','My first left footer','','My first right footer');    
     
    {managing formats}
    {setting cell values}
    for Rows:=5 to 100 do
      xls.SetValue(Rows,2,Rows,'DEFAULT','','');
    {etc....}
    
    xls.MakeSheetActive('Content');          
        
    try 
      xls.CloseAndSave(true);         
    except
      ShowError('Error on saving file.');
    end;                
    
  finally
    xls.Free;
  end;         
   
end.