int NewLst = 10;     //  18 - для матричного
                     //  10 - для лазерного
int idxK = 0,
    idxV = 1,
    idxS = 2,
    idxT = 3,
    idxTT = 4,
    CountIdx=5;
int MySvodFld[CountIdx];

int ind1  = 0,
    ind2  = 1,
    ind3  = 2,
    ind4  = 3,
    ind5  = 4,
    ind6  = 5,
    ind7  = 6,
    ind8  = 7,
    ind9  = 8,
    ind10 = 9,
    CountInd=10;
double Fld[CountInd],
      temp5;

int in1  = 0,
    in2  = 1,
    in3  = 2,
    in4  = 3,
    in5  = 4,
    in6  = 5,
    in7  = 6,
    in8  = 7,
    in9  = 8,
    in10 = 9,
    CountIn=10;
double Fld2[CountIn];

double Fld3[CountIn];
double Fld4[CountIn];

char Buf3[512], Buf4[512];
char Buf5[512], Buf6[512];

//==============================================================
int len = 512,
    n = 12;
char Dir[len]; // Рабочий каталог амбы (обычно ZPL)
string nm;     // Имя Excel шаблона
var WorkDir,   // Рабочий каталог модуля
    xls,       // объект Excel.Application
    wb,        // объект workbook
    wsh;       // объект worksheet

//==============================================================



double SumIt=0, RV_It=0;
int CntVid=0;  // счётчик обработанных видов Н-У для перевода листа
int ListN =1;  // счётчик листов
var CMsg = 0,
    CFile,
    CTabl;
int cr=0, cfld=0, cr_ins=0, rec_ins=0, rec_work=0, CntHead=19, 
    CntFoot = 10, nStart = 12, tmpi=0, CntStr = 0, vp=0;

MySvod()
{
  PreFld2();
  PreFld();
  Setup();
  double Sum[90];   //массив сумм по категориям
  double  RV[90];   //массив РВ по категориям

  char Buf[512], Buf1[512], Buf2[512];   

  char NameP[256];

  var CTabl = CreateObject("CurPrnTbl");
  if ( !FillIdxFldMySvod(CTabl) )  return;
  
  int cr=CTabl.Count_Rcr(); // число рабочих записей в таблице
 
  int cntRcrKat  =0;
  int cntRcrVid  =0;
  string OldKat  ="";
  string OldVid  ="";
  double SumKat  =0,
         SumVid  =0,
         ItSum   =0;

  SetupExcel();
  OldVid  = CTabl.Text(MySvodFld[idxV],0);
 
  for (int zap=0; zap<cr; zap++)
  {
     string NewKat  = CTabl.Text(MySvodFld[idxK],zap);
     string NewVid  = CTabl.Text(MySvodFld[idxV],zap);
     

     if ( NewVid!=OldVid )      // ниже вывод на печать Buf1 и Buf2
       {
        CntVid++;
        PrintTableToExcel(OldVid);
        OldVid=NewVid;        
       }
     //==================================================
     if(CMsg.Msg_Proc(" Подождите. Идёт экспорт данных в Excel.",100.*zap/cr)==ESC)
     {
        AmbaMessageBox("Печать не была завершена!","Предупреждение!",0,0);
        xls.Visible = true;
        return;
     }
     //==================================================

     double KKat = atof(CTabl.TextS("FCkat",zap));
     Sum[KKat]   = atof(CTabl.TextS("Sum",zap));
     RV[KKat]    = atof(CTabl.TextS("T",zap));

     SumIt = SumIt + Sum[KKat];
     RV_It = RV_It + RV[KKat];

     if ( KKat == 1 )            //  руководители
       {
         Fld[5]  = Fld[5]  + RV[KKat];
         Fld2[5] = Fld2[5] + Sum[KKat];
         Fld3[5] = Fld3[5] + RV[KKat];
         Fld4[5] = Fld4[5] + Sum[KKat];

       }  
        
     if ( KKat == 2 )            //  специалисты
       {
         Fld[6]  = Fld[6]  + RV[KKat];
         Fld2[6] = Fld2[6] + Sum[KKat];
         Fld3[6] = Fld3[6] + RV[KKat];
         Fld4[6] = Fld4[6] + Sum[KKat];

       }

     if ( KKat == 3 )            //  прочие
       {
         Fld[7]  = Fld[7]  + RV[KKat];
         Fld2[7] = Fld2[7] + Sum[KKat];
         Fld3[7] = Fld3[7] + RV[KKat];
         Fld4[7] = Fld4[7] + Sum[KKat];

       }

     if ( KKat == 4 )            //  основные раб
       {
         Fld[2]  = Fld[2]  + RV[KKat];
         Fld2[2] = Fld2[2] + Sum[KKat];
         Fld3[2] = Fld3[2] + RV[KKat];
         Fld4[2] = Fld4[2] + Sum[KKat];

       }

     if ( KKat == 5 )            //  вспомогат раб
       {
         Fld[3]  = Fld[3]  + RV[KKat];
         Fld2[3] = Fld2[3] + Sum[KKat];
         Fld3[3] = Fld3[3] + RV[KKat];
         Fld4[3] = Fld4[3] + Sum[KKat];

       }

     if ((KKat != 1) && (KKat != 2) && (KKat != 3) && (KKat != 4) && (KKat != 5))
       {
         Fld[0]  = Fld[0]  + RV[KKat];
         Fld2[0] = Fld2[0] + Sum[KKat];
         Fld3[0] = Fld3[0] + RV[KKat];
         Fld4[0] = Fld4[0] + Sum[KKat];

       }

     Fld[1] = Fld[2] + Fld[3];
     Fld[4] = Fld[5] + Fld[6] + Fld[7];
     Fld2[1] = Fld2[2] + Fld2[3];
     Fld2[4] = Fld2[5] + Fld2[6] + Fld2[7];

     Fld[8]  = Fld[1]  + Fld[4]+ Fld[0];
     Fld2[9] = Fld2[1] + Fld2[4]+ Fld2[0];

     Fld3[1] = Fld3[2] + Fld3[3];
     Fld3[4] = Fld3[5] + Fld3[6];
     Fld4[1] = Fld4[2] + Fld4[3];
     Fld4[4] = Fld4[5] + Fld4[6] + Fld4[7];

  }
  //==================================================
  
  xls.Visible = true;  
 
//==================================================
  PrintTableToExcel(OldVid); // печать двух строк последнего вида
  PrnStr2();
  wsh.Rows(6).Select();  // которую будем копировать
  xls.Selection.Copy(wsh.Rows(n+1)); // куда копировать
  wsh.Rows(6).Delete();
  wsh.Rows(7).Select();
  Date_();
  
}

FillIdxFldMySvod(&CTabl)
{
   MySvodFld[idxK] = CTabl.Find_Name_Fld("FCkat");
   MySvodFld[idxV] = CTabl.Find_Name_Fld("KVOU");
   MySvodFld[idxS] = CTabl.Find_Name_Fld("Sum");
   MySvodFld[idxT] = CTabl.Find_Name_Fld("T");
   MySvodFld[idxTT] = CTabl.Find_Name_Fld("T1");
   for ( int idx=0; idx<CountIdx; ++idx )
   {
      if ( MySvodFld[idx]<0 )
      {   
         AmbaMessageBox("Не найдены нужные столбцы","Ошибка",1,0);
         return 0;
      }
   }
   return 1;
}

Date_()
{
  char s[256];        

  char arg[40], str[40];
  var Mes = CreateObject("KMonth");
  Mes.SetMonth(mtek);
  int iGod = Mes.GetYear();
  int iMes = Mes.GetMonth();

  sprintf(arg,"1,%d",mtek); R_DATA_EX(arg,str,20,0);  
  FreeSpaceString(str,3,"");
  string sMes = static_cast_to_string(str);  

  sprintf(str,"%s %d г.",sMes,iGod); FreeSpaceString(str,3,"");
    
  WriteToCell("H4",OemToAnsi(str));
  
  return 0;
}

PreFld2()
{
  for ( int ind=0; ind<CountInd; ind++ )
   {
     Fld3[ind] = 0;
     Fld4[ind] = 0;
   }
}

PreFld()
{

  for ( int ind=0; ind<CountInd; ind++ )
   {
     Fld[ind] = 0;  
     Fld2[ind] = 0;
   }

}

PrnStr2()
 {        
 
  char c1[9], c2[9], c3[9], c4[9], c5[9], c6[9],
       c7[9], c8[9], c9[9], c10[9], c11[9],
       b1[9], b2[9], b3[9], b4[9], b5[9], b6[9],
       b7[9], b8[9], b9[9], b10[9], b11[9];


  sprintf(c1,"B%d",n-1);
  sprintf(c2,"C%d",n-1);
  sprintf(c3,"D%d",n-1);
  sprintf(c4,"E%d",n-1);
  sprintf(c5,"F%d",n-1);
  sprintf(c6,"G%d",n-1);
  sprintf(c7,"H%d",n-1);
  sprintf(c8,"I%d",n-1);
  sprintf(c9,"J%d",n-1);
  sprintf(c10,"K%d",n-1);
  sprintf(c11,"L%d",n-1);

  sprintf(b1,"B%d",n);
  sprintf(b2,"C%d",n);
  sprintf(b3,"D%d",n);
  sprintf(b4,"E%d",n);
  sprintf(b5,"F%d",n);
  sprintf(b6,"G%d",n);
  sprintf(b7,"H%d",n);
  sprintf(b8,"I%d",n);
  sprintf(b9,"J%d",n);
  sprintf(b10,"K%d",n);
  sprintf(b11,"L%d",n);
  

  
  WriteToCell(c2,static_cast_to_double(Fld3[1]));  
  WriteToCell(c3,static_cast_to_double(Fld3[2]));
  WriteToCell(c4,static_cast_to_double(Fld3[3]));
  WriteToCell(c5,static_cast_to_double(Fld3[4]));
  WriteToCell(c6,static_cast_to_double(Fld3[5]));
  WriteToCell(c7,static_cast_to_double(Fld3[6]));
  WriteToCell(c8,static_cast_to_double(Fld3[7]));
  WriteToCell(c9,static_cast_to_double(Fld3[0]));
  WriteToCell(c10,static_cast_to_double(RV_It));
  WriteToCell(c11,static_cast_to_double(Fld3[9]));

  
  WriteToCell(b2,static_cast_to_double(Fld4[1]));  
  WriteToCell(b3,static_cast_to_double(Fld4[2]));
  WriteToCell(b4,static_cast_to_double(Fld4[3]));
  WriteToCell(b5,static_cast_to_double(Fld4[4]));
  WriteToCell(b6,static_cast_to_double(Fld4[5]));
  WriteToCell(b7,static_cast_to_double(Fld4[6]));
  WriteToCell(b8,static_cast_to_double(Fld4[7]));
  WriteToCell(b9,static_cast_to_double(Fld4[0]));
  WriteToCell(b10,static_cast_to_double(Fld4[8]));
  WriteToCell(b11,static_cast_to_double(SumIt));
  

 }


  //==================================================================

 PrintTableToExcel(Frst)
{ // печать строки в Excel
  wsh.Rows(n).EntireRow.Insert();
  wsh.Rows(n-1).Select();  // которую будем копировать
  xls.Selection.Copy(wsh.Rows(n+1)); // куда копировать
  //wsh.Rows(n).EntireRow.Insert();
  wsh.Rows(n).Select();  // которую будем копировать
  xls.Selection.Copy(wsh.Rows(n+2)); // куда копировать
  
 
  
  char c1[9], c2[9], c3[9], c4[9], c5[9], c6[9],
       c7[9], c8[9], c9[9], c10[9], c11[9],
       b1[9], b2[9], b3[9], b4[9], b5[9], b6[9],
       b7[9], b8[9], b9[9], b10[9], b11[9];


  sprintf(c1,"B%d",n-1);
  sprintf(c2,"C%d",n-1);
  sprintf(c3,"D%d",n-1);
  sprintf(c4,"E%d",n-1);
  sprintf(c5,"F%d",n-1);
  sprintf(c6,"G%d",n-1);
  sprintf(c7,"H%d",n-1);
  sprintf(c8,"I%d",n-1);
  sprintf(c9,"J%d",n-1);
  sprintf(c10,"K%d",n-1);
  sprintf(c11,"L%d",n-1);

  sprintf(b1,"B%d",n);
  sprintf(b2,"C%d",n);
  sprintf(b3,"D%d",n);
  sprintf(b4,"E%d",n);
  sprintf(b5,"F%d",n);
  sprintf(b6,"G%d",n);
  sprintf(b7,"H%d",n);
  sprintf(b8,"I%d",n);
  sprintf(b9,"J%d",n);
  sprintf(b10,"K%d",n);
  sprintf(b11,"L%d",n);
  

  WriteToCell(c1,atoi(Frst));       
  WriteToCell(c2,static_cast_to_double(Fld[1]));  
  WriteToCell(c3,static_cast_to_double(Fld[2]));
  WriteToCell(c4,static_cast_to_double(Fld[3]));
  WriteToCell(c5,static_cast_to_double(Fld[4]));
  WriteToCell(c6,static_cast_to_double(Fld[5]));
  WriteToCell(c7,static_cast_to_double(Fld[6]));
  WriteToCell(c8,static_cast_to_double(Fld[7]));
  WriteToCell(c9,static_cast_to_double(Fld[0]));
  WriteToCell(c10,static_cast_to_double(Fld[8]));
  WriteToCell(c11,static_cast_to_double(Fld[9]));

  WriteToCell(b1,atoi(Frst));       
  WriteToCell(b2,static_cast_to_double(Fld2[1]));  
  WriteToCell(b3,static_cast_to_double(Fld2[2]));
  WriteToCell(b4,static_cast_to_double(Fld2[3]));
  WriteToCell(b5,static_cast_to_double(Fld2[4]));
  WriteToCell(b6,static_cast_to_double(Fld2[5]));
  WriteToCell(b7,static_cast_to_double(Fld2[6]));
  WriteToCell(b8,static_cast_to_double(Fld2[7]));
  WriteToCell(b9,static_cast_to_double(Fld2[0]));
  WriteToCell(b10,static_cast_to_double(Fld2[8]));
  WriteToCell(b11,static_cast_to_double(Fld2[9]));

  
  n=n+2;
  PreFld();
  return 0;
}

SetupExcel()
{
  xls = CreateObject("Excel.Application");
  GetCurrentDirectory(len,Dir);
  nm = "t_razved\\KB.xls";
  WorkDir = Dir + "\\";
  xls.Caption = OemToAnsi("Ведомость");
  xls.Workbooks.Add(WorkDir+nm);
  wb = xls.Workbooks(1);
  wsh = wb.Worksheets.Item(1);

  return 0;
}
            

WriteToCell(cel,s)
{
  wsh.Range(static_cast_to_string(cel)).Value = static_cast_to_string(s);
  return 0;
}

Setup()
{ 
  int bTime = false;   // false-показывать процент,
                       // true-показывать оставшееся время

  CMsg = CreateObject("ProgressBar");
  CMsg.Initial(0,100,1);
  CMsg.SetShowTime(static_cast_to_int(bTime));

  CFile = CreateObject("CurPrnFile"); // объект класса для работы с текущим файлом печати
  CFile.SetFlagBrowse(0);             // запрещаем просмотр

  CTabl = CreateObject("CurPrnTbl");  // объект класса для работы с текущей таблицей
  cr = CTabl.Count_Rcr();             // число рабочих записей в таблице   
  cfld = CTabl.Count_Fld();           // число полей в таблице
  cr_ins = CTabl.GetCountInsertRcr(); // число выделенных записей

  if ( cr_ins ) rec_work = cr_ins;    // число записей которые должны быть обработаны
  else          rec_work = cr;

  tmpi = rec_work + CntHead + CntFoot;

  if ( tmpi > CntStr * 2 )
    vp = 2;

  if ( tmpi > CntStr && tmpi <= CntStr * 2 )
    vp = 1;

  if ( tmpi <= CntStr || rec_work <= 1 )
    vp = 0;

  return 0;
}
//=============================================================