//:

//    MAINFORM.DFM
      object Group_Group: TRadioGroup
        Left = 8
        Top = 215
        Width = 241
        Height = 65
        Caption = #1042#1080#1076' '#1092#1086#1088#1084#1099' '#1087#1088#1086#1090#1086#1082#1086#1083#1072':'
        ItemIndex = 1
        Items.Strings = (
          #1050#1086#1084#1087#1072#1082#1090#1085#1072#1103
          #1053#1072#1075#1083#1103#1076#1085#1072#1103
          'Excel')
        TabOrder = 2
      end


//  MAINFORM.PAS
             //  HTML
             if Group_Group.ItemIndex = 2
             then begin
             fname := GetEnvironmentVariable ('TEMP')+'\'+ Data_Grid.Cells [0,Data_Grid.Row]+'.xls';
             f.makeXLS (Log,Label2.Caption,Debug_Label.Caption,EMail_Label.Caption,Data_Grid.Cells [1,Data_Grid.Row],Data_Grid.Cells [0,Data_Grid.Row],fname,0);
             end
             else begin
             fname := GetEnvironmentVariable ('TEMP')+'\'+ Data_Grid.Cells [0,Data_Grid.Row]+'.html';
             f.makeHTML (Log,Label2.Caption,Debug_Label.Caption,EMail_Label.Caption,Data_Grid.Cells [1,Data_Grid.Row],Data_Grid.Cells [0,Data_Grid.Row],fname,Group_Group.ItemIndex);
             end;
             //  
             ShellExecute (0,'open',PChar (fname),'','',sw_maximize);



//SVERKA.PAS


//
             procedure makeXLS (Log : TStringList; ProgramVersion,DebugBuild,ProgramLink,StrName,RegNumb,FileName : string; groupBy : integer);


function f2s2 (f : Currency) : string;
var
 s   : string;
begin
 Str (f:15:2,s);
 //   
 while s [1] = ' ' do s := Copy (s,2,Length (s)-1);
 f2s2 := s;
end;


procedure TFileSet.makeXLS;
var
 f                   : TextFile;
 i,i1,i2             : integer;
 PackIndex           : string;
 NS,NN,US,UN         : Currency;
 NINS,NINN,NIUS,NIUN : Currency;
 SS,SN               : Currency;

begin
 AssignFile (f,FileName);
 rewrite (f);
 //  
 writeln (f,'<?xml version="1.0" encoding="windows-1251"?>');
 writeln (f,'<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<DocumentProperties Author="" LastAuthor="" Created="" LastSaved="" Company="" Version="1.0" />');
 writeln (f,'<Styles>');
 writeln (f,'<Style ss:ID="Bold" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<Font ss:Bold="1" />');
 writeln (f,'</Style>');
 writeln (f,'<Style ss:ID="Borders" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:Indent="0" ss:WrapText="1" />');
 writeln (f,'<Borders>');
 writeln (f,'<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'</Borders>');
 writeln (f,'<NumberFormat ss:Format="0.00_ ;[Red]\-0.00\ ;\ \-"/>');
 writeln (f,'</Style>');
 writeln (f,'<Style ss:ID="BordersBold" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<Font ss:Bold="1" />');
 writeln (f,'<Alignment ss:Horizontal="Center" ss:Vertical="Center" ss:Indent="0" ss:WrapText="1" />');
 writeln (f,'<Borders>');
 writeln (f,'<Border ss:Position="Left" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Top" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Right" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'<Border ss:Position="Bottom" ss:LineStyle="Continuous" ss:Weight="1" />');
 writeln (f,'</Borders>');
 writeln (f,'<NumberFormat ss:Format="0.00_ ;[Red]\-0.00\ ;\ \-"/>');
 writeln (f,'</Style>');
 writeln (f,'</Styles>');
//  1
 writeln (f,'<Worksheet ss:Name="" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<Table x:FullColumns="1" x:FullRows="1" xmlns:x="urn:schemas-microsoft-com:office:excel">');
 writeln (f,'<Column ss:Width="600" />');
 //  
 write (f,'<Row><Cell ss:StyleID="Bold"><Data ss:Type="String">');
 write (f,'         ');
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell><Data ss:Type="String">');
 write (f,' VL:'+ProgramVersion+DebugBuild);
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell><Data ss:Type="String">');
 write (f,'    : '+DateToStr (Date)+' '+TimeToStr (Time));
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell ss:StyleID="Bold"><Data ss:Type="String">');
 write (f,'  :');
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell><Data ss:Type="String">');
 write (f,' : '+StrName);
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell><Data ss:Type="String">');
 write (f,'   : '+RegNumb);
 writeln (f,'</Data></Cell></Row>');
 write (f,'<Row><Cell ss:StyleID="Bold"><Data ss:Type="String">');
 write (f,'   .    :');
 writeln (f,'</Data></Cell></Row>');
 for i := 0 to Length (Recs) - 1
  do begin
      write (f,'<Row><Cell><Data ss:Type="String">');
      write (f,Recs [i].FileName+' ('+Recs [i].VidFormy+' '+Recs [i].SvedType+'  '+getPerName (Recs [i].Year,Recs [i].Per)+',  '+Recs [i].CatCode+')');
      writeln (f,'</Data></Cell></Row>');
     end;
 write (f,'<Row><Cell ss:StyleID="Bold"><Data ss:Type="String">');
 write (f,' :');
 writeln (f,'</Data></Cell></Row>');
 for i := 0 to Length (Periods) - 1
  do begin
      write (f,'<Row><Cell><Data ss:Type="String">');
      write (f,getPerName (Periods [i].Year,Periods [i].Quarter));
      writeln (f,'</Data></Cell></Row>');
     end;
 // ,   ,  
 if Log.Count > 0
  then begin
        write (f,'<Row><Cell><Data ss:Type="String">');
        write (f,'  ,   :');
        writeln (f,'</Data></Cell></Row>');
        for i := 0 to Log.Count - 1
         do begin
             write (f,'<Row><Cell><Data ss:Type="String">');
             write (f,Log.Strings [i]);
             writeln (f,'</Data></Cell></Row>');
            end;
       end;
 writeln (f,'</Table>');
 writeln (f,'</Worksheet>');
//  2
 writeln (f,'<Worksheet ss:Name="" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">');
 writeln (f,'<Table x:FullColumns="1" x:FullRows="1" xmlns:x="urn:schemas-microsoft-com:office:excel">');
 writeln (f,'<Column ss:Width="90" />');
 writeln (f,'<Column ss:Width="210" />');
 writeln (f,'<Column ss:Width="120" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 writeln (f,'<Column ss:Width="60" />');
 //  
 writeln (f,'<Row>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="2"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="2"><Data ss:Type="String">, , </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="2"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="2"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeAcross="4"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeAcross="4"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'</Row>');
 writeln (f,'<Row>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:Index="5" ss:MergeAcross="1"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeAcross="2"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeAcross="1"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeAcross="2"><Data ss:Type="String"> </Data></Cell>');
 writeln (f,'</Row>');
 writeln (f,'<Row>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:Index="5"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'<Cell ss:StyleID="BordersBold"><Data ss:Type="String"></Data></Cell>');
 writeln (f,'</Row>');
 //  
 for i := 0 to Length (Sotrs)-1
  do begin
      writeln (f,'<Row>');
      write (f,'<Cell ss:StyleID="Borders" ss:MergeDown="' +IntToStr (Length (Periods)-1)+ '"><Data ss:Type="String">');
      write (f,Sotrs [i].StrNumb);
      writeln (f,'</Data></Cell>');
      write (f,'<Cell ss:StyleID="Borders" ss:MergeDown="' +IntToStr (Length (Periods)-1)+ '"><Data ss:Type="String">');
      write (f,Sotrs [i].SurName+' '+Sotrs [i].Name+' '+Sotrs [i].FatherName);
      writeln (f,'</Data></Cell>');
      NINS := 0;
      NIUS := 0;
      NINN := 0;
      NIUN := 0;
      for i1 := 0 to Length (Periods) - 1
       do begin
           GetNU (i,Periods [i1].Year,Periods [i1].Quarter,PackIndex,NS,NN,US,UN);
           NINS := NINS + NS;
           NIUS := NIUS + US;
           SS := NINS - NIUS;
           NINN := NINN + NN;
           NIUN := NIUN + UN;
           SN := NINN - NIUN;
           if PackIndex = '' then PackIndex := '-';
           if i1 > 0 then write (f,'<Row><Cell ss:StyleID="Borders" ss:Index="3">')
           else write (f,'<Cell ss:StyleID="Borders">');
           write (f,'<Data ss:Type="String">');
           write (f,getPerName (Periods [i1].Year,Periods [i1].Quarter));
           writeln (f,'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="String">');
           writeln (f,PackIndex+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NS)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (US)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NINS)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NIUS)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (SS)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NN)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (UN)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NINN)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (NIUN)+'</Data></Cell>');
           write (f,'<Cell ss:StyleID="Borders"><Data ss:Type="Number">');
           writeln (f,f2s2 (SN)+'</Data></Cell>');
           writeln (f,'</Row>');
          end;
     end;
 // 
 i2 := Length (Periods) * Length (Sotrs);
 writeln (f,'<Row>');
 writeln (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="' +IntToStr (Length (Periods)-1)+ '" />');
 write (f,'<Cell ss:StyleID="BordersBold" ss:MergeDown="' +IntToStr (Length (Periods)-1)+ '"><Data ss:Type="String">');
 writeln (f,':</Data></Cell>');
 for i1 := 0 to Length (Periods) - 1
  do begin
      if i1 > 0 then write (f,'<Row><Cell ss:StyleID="BordersBold" ss:Index="3">')
      else write (f,'<Cell ss:StyleID="BordersBold">');
      write (f,'<Data ss:Type="String">');
      write (f,getPerName (Periods [i1].Year,Periods [i1].Quarter));
      writeln (f,'</Data></Cell>');
      writeln (f,'<Cell ss:StyleID="BordersBold" />');
      //
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=SUMIF(R[-'+IntToStr (i2+i1)+']C3:R[-'+IntToStr (1+i1)+']C3,RC3,R[-'+IntToStr (i2+i1)+']C:R[-'+IntToStr (1+i1)+']C)" />');
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=SUMIF(R[-'+IntToStr (i2+i1)+']C3:R[-'+IntToStr (1+i1)+']C3,RC3,R[-'+IntToStr (i2+i1)+']C:R[-'+IntToStr (1+i1)+']C)" />');
      if i1 > 0 then writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=R[-1]C+RC[-2]" />')
      else writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]" />');
      if i1 > 0 then writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=R[-1]C+RC[-2]" />')
      else writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]" />');
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]-RC[-1]" />');
      //
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=SUMIF(R[-'+IntToStr (i2+i1)+']C3:R[-'+IntToStr (1+i1)+']C3,RC3,R[-'+IntToStr (i2+i1)+']C:R[-'+IntToStr (1+i1)+']C)" />');
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=SUMIF(R[-'+IntToStr (i2+i1)+']C3:R[-'+IntToStr (1+i1)+']C3,RC3,R[-'+IntToStr (i2+i1)+']C:R[-'+IntToStr (1+i1)+']C)" />');
      if i1 > 0 then writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=R[-1]C+RC[-2]" />')
      else writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]" />');
      if i1 > 0 then writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=R[-1]C+RC[-2]" />')
      else writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]" />');
      writeln (f,'<Cell ss:StyleID="BordersBold" ss:Formula="=RC[-2]-RC[-1]" />');
      writeln (f,'</Row>');
     end;
 writeln (f,'</Table>');
//   
 writeln (f,' <AutoFilter x:Range="C3:C3"');
 writeln (f,'  xmlns="urn:schemas-microsoft-com:office:excel">');
 writeln (f,' </AutoFilter>');
 writeln (f,'</Worksheet>');
 //  
 writeln (f,'</Workbook>');
 CloseFile (f);
end;
