

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

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

  char 	 kat[100], 
		 pkg[10],
		 n_kat[5];		 
		 		 
  double okl,
		 vred,
		 sovm,
		 rk,
	     it_komp,
		 stag,
		 zv,
		 s_kt,
		 dopr,
		 pr,
		 ints,
		 kach,
	     in_stim,
	     it_stim,
		 otpusk,
		 itogo; 

  var xls=CreateObject("Excel.Application");  
  char Dir[128];                              
  GetCommonZPL(Dir,128);                      
  string nm="srzar.xls";                      
  var WorkDir = Dir + "\\TABVEDRF\\";         
  xls.Caption=OemToAnsi("Среднемесячная зарплата");      
  xls.Workbooks.Add(WorkDir+nm);              

  string name=xls.Workbooks.Item(1).Name;                  // читаем имя workbookа, чтобы обращатья потом к нему по имени
  var WSheet1=xls.Workbooks.Item(name).Worksheets.Item(1); // создаем ссылку на на наш worksheet чтобы избежать впоследствии длинных записей

  string cell; // в эту строку будем записывать имена ячеек worksheetа
  
  int row=11; // стоя на этой строке начнем удалять строки бланка
  char rCel[20]; // буфер для диапазона ячеек

  // удаляем 14 строк бланка
  for (int i=0; i<14; i++) 
  {
    WSheet1.Rows(row).Delete();
  }

  // вставляем нужное нам число строк, равное числу записей таблицы
  for (int i=0; i<cr; i++)
  {
    WSheet1.Rows(row).Insert(); // вставляем строку
  }

	 double itsum1  = 0.,
			itsum2  = 0.,
			itsum3  = 0.,
			itsum4  = 0.,
			itsum5  = 0.,
			itsum6  = 0.,
			itsum7  = 0.,
			itsum8  = 0.,
			itsum9  = 0.,
			itsum10 = 0.,
			itsum11 = 0.,
			itsum12 = 0.,
			itsum13 = 0.,
			itsum14 = 0.,
			itsum15 = 0.,
			itsum16 = 0.;
		
  
  // цикл по записям таблицы
  for (int zap=0; zap<cr; zap++)
  {
	n_kat	= CTabl.TextS("K",zap);
	kat     = CTabl.TextS("FCkat1",zap);
	OemToChar(kat,kat); 
	pkg     = CTabl.TextS("P",zap);
	okl 	= CTabl.TextS("s1o",zap);
	vred	= CTabl.TextS("s2o",zap);
	sovm	= CTabl.TextS("s3o",zap);
	rk		= CTabl.TextS("s4o",zap);
	it_komp = CTabl.TextS("it1o",zap);
	stag    = CTabl.TextS("s5о",zap);
	zv      = CTabl.TextS("it1o",zap);	
	s_kt    = CTabl.TextS("s7o",zap);
	dopr	= CTabl.TextS("s8o",zap);
	pr  	= CTabl.TextS("s9o",zap);
	ints	= CTabl.TextS("s10o",zap);
	kach	= CTabl.TextS("s11o",zap);
	in_stim	= CTabl.TextS("s12o",zap);
    it_stim	= CTabl.TextS("it2o",zap);
    otpusk	= CTabl.TextS("s13",zap);	
    itogo	= CTabl.TextS("it3o",zap);

    int n=zap+11; // номер строки в worksheetе
	
    cell="A" + static_cast_to_string(n);
    WriteToCell(WSheet1, cell, kat);
	
				if(atoi(n_kat)==0)
			{
				sprintf(rCel,"A%d:A%d",n,n);
				WSheet1.Range(static_cast_to_string(rCel)).HorizontalAlignment=4;
				WriteToCell(WSheet1, cell, OemToAnsi("Итого:"));
				sprintf(rCel,"A%d:R%d",n,n);
				WSheet1.Range(static_cast_to_string(rCel)).Font.Bold = true; 
			}


    cell="B" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, pkg);
	
    cell="C" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, okl);      

	cell="D" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, vred);	
	
    cell="E" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, sovm);      

    cell="F" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, rk);

    cell="G" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, it_komp); 
	
    cell="H" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, stag);

    cell="I" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, zv);      

    cell="J" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, s_kt);	
	
    cell="K" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, dopr);     

	cell="L" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, pr); 
	
    cell="M" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, ints);      

    cell="N" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, kach);	

    cell="O" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, in_stim);      

    cell="P" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, it_stim);

    cell="Q" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, otpusk);      

    cell="R" + static_cast_to_string(n); 
    WriteToCell(WSheet1, cell, itogo);	
	
			if(atoi(n_kat)==0)	
				okl 	= 0., 
				vred	= 0., 
				sovm	= 0., 
				rk		= 0., 
				it_komp = 0., 
				stag    = 0., 
				zv      = 0., 
				s_kt    = 0., 
				dopr	= 0., 
				pr  	= 0., 
				ints	= 0., 
				kach	= 0., 
				in_stim	= 0., 
				it_stim	= 0., 
				otpusk	= 0., 
				itogo	= 0.; 
						
				itsum1  = okl + itsum1 ;   
				itsum2  = vred + itsum2 ;	
				itsum3  = sovm + itsum3 ;
				itsum4  = rk + itsum4 ;
				itsum5  = it_komp + itsum5 ;
				itsum6  = stag + itsum6 ;
				itsum7  = zv + itsum7 ;
				itsum8  = s_kt + itsum8 ;
				itsum9  = dopr + itsum9 ;
				itsum10 = pr + itsum10;
				itsum11 = ints + itsum11;
				itsum12 = kach + itsum12;
				itsum13 = in_stim + itsum13;
				itsum14 = it_stim + itsum14;
				itsum15 = otpusk + itsum15;
				itsum16 = itogo	+ itsum16;

  }
		char str[255];
		R_TEXT_FLD("Descriptor",str,255,0); 
        FreeSpaceString(str,3,"");
		WriteToCell(WSheet1,"A6",OemToAnsi(str));

//		AmbaMessageBox(str,"int",1,0);
			 
			 cr = cr+11;

				sprintf(rCel,"A%d:A%d",cr,cr);
				sprintf(cell,"A%d",cr);
				WSheet1.Range(static_cast_to_string(rCel)).HorizontalAlignment=3;
				WriteToCell(WSheet1, cell, OemToAnsi("Итого по ведомости:"));
				sprintf(rCel,"A%d:R%d",cr,cr);
				WSheet1.Range(static_cast_to_string(rCel)).Font.Bold = true;
  
    cell="C" + static_cast_to_string(cr); 
    WriteToCell(WSheet1, cell, itsum1 ); 
	
	cell="D" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum2 );

	cell="E" + static_cast_to_string(cr);	
	WriteToCell(WSheet1, cell, itsum3 ); 
	
	cell="F" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum4 );
	
	cell="G" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum5 );
	
	cell="H" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum6 ); 
	
	cell="I" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum7 ); 
	
	cell="J" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum8 );
	
	cell="K" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum9 ); 
	
	cell="L" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum10); 
	
	cell="M" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum11); 
	
	cell="N" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum12); 
	
	cell="O" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum13); 
	
	cell="P" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum14); 
	
	cell="Q" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum15); 
	
	cell="R" + static_cast_to_string(cr);
	WriteToCell(WSheet1, cell, itsum16); 
	
  WSheet1.Name=OemToAnsi("Зарплата по категориям");; // имя worksheetа (в windows кодировке)
  xls.Visible=true;         // делаем Excel видимым

  return 0;
}

WriteToCell(WSheet,Cell,str)
// WSheet - ссылка на worksheet
// Cell - имя ячейки
// str - значение, которое требуется записать в ячейку Cell рабочего листа WSheet
{
  WSheet.Range(Cell).Value=static_cast_to_string(str);
  return 0;
}
