
tCreateExcelObjects(fullFileName,Caption)
{
  _XlsObj = CreateObject("Excel.Application");
  _XlsObj.Caption = OemToAnsi(Caption);
  _XlsObj.Workbooks.Add(static_cast_to_string(fullFileName));

  return 0;
}

tMakeExcelVisible()
{
  _XlsObj.Visible = true;
  return 0;
}

tGetExcelWshByIndex(i)
{
//_WshObj = _XlsObj.Workbooks.Item(1).Worksheets.Item(i);
  _WshObj = _XlsObj.Worksheets(i);
  _WshIndex = i;
  return 0;
}

tWriteStringToExcelCell(cell,str)
{
  FreeSpaceString(str,3,"");
  _WshObj.Range(static_cast_to_string(cell)).Value = static_cast_to_string(str);
  return 0;
}

tWriteNumToExcelCell2(i,j,str)
{
  FreeSpaceString(str,3,"");
  _WshObj.Cells(i,j).Value = static_cast_to_string(str);
  return 0;
}

tWriteNumToExcelCell(cell,num)
{
  _WshObj.Range(static_cast_to_string(cell)).Value = num;
  return 0;
}

tHorizontalAlignmentExcelCell(cell,n)
{
  _WshObj.Range(static_cast_to_string(cell)).HorizontalAlignment = n;
  return 0;
}

tFontSizeExcelCell(cell,n)
{
  _WshObj.Range(static_cast_to_string(cell)).Font.Size = n;
  return 0;
}

tWshExcelSelect()
{
  _WshObj.Select();
  return 0;
}

tCopyExcelRow(n)
{
  _WshObj.Rows(n-1).Select();
  _XlsObj.Selection.Copy(_WshObj.Rows(n));
  return 0;
}

tCopyExcelRows(n,count)
{
  char str[255];
  sprintf(str,"%d:%d",n-count,n-1);
  _WshObj.Rows(static_cast_to_string(str)).Select();
  _XlsObj.Selection.Copy(_WshObj.Rows(n));
  return 0;
}

tCopyExcelRows2(n1,n2,count)
{
  char str[255];
  sprintf(str,"%d:%d",n1,n1+count-1);
  _WshObj.Rows(static_cast_to_string(str)).Select();
  _XlsObj.Selection.Copy(_WshObj.Rows(n2));
  return 0;
}

tCopyExcelColumns(n,count)
{
  var cell1 = _WshObj.Cells(1,n-count);
  var cell2 = _WshObj.Cells(512,n-1);
  var cell3 = _WshObj.Cells(1,n);
  _WshObj.Range(cell1,cell2).Copy();
  _WshObj.Range(cell3,cell3).Select();
  _WshObj.Paste();
  return 0;
}

tInsertExcelRows(n,count)
{
  for ( int i = 0; i < count; i++ ) 
    _WshObj.Rows(n).EntireRow.Insert();
  return 0;
}

tInsertExcelColumns(n,count)
{
  for ( int i = 0; i < count; i++ ) 
    _WshObj.Columns(n).Insert();
  return 0;
}


tDeleteExcelRows(n,count)
{
  for ( int i = 0; i < count; i++ ) 
    _WshObj.Rows(n).Delete();
  return 0;
}

tDeleteExcelColumns(n,count)
{
  for ( int i = 0; i < count; i++ ) 
    _WshObj.Columns(n).Delete();
  return 0;
}

tWshExcelCellSelect(n,m)
{
  _WshObj.Cells(n,m).Select();
  return 0;
}

tWshExcelHide()
{
  _WshObj.Visible = false;
  return 0;
}

//////////////////////////////////////

tCopyExcelWshByIndex(i,str) 
{
  _XlsObj.Worksheets(i).Copy(_XlsObj.Worksheets(i));
  _XlsObj.Worksheets(i).Name=OemToAnsi(str);
  _WshIndex = i;
  return 0;
}

tBordersExcelCell(Cell,l,r,t,b,n) 
{
   // левая граница - линия
   if(l) _WshObj.Range(static_cast_to_string(Cell)).Borders(1).LineStyle = n;
   // правая граница - линия
   if(r) _WshObj.Range(static_cast_to_string(Cell)).Borders(2).LineStyle = n;
   // верхняя граница - линия
   if(t) _WshObj.Range(static_cast_to_string(Cell)).Borders(3).LineStyle = n;
   // нижняя граница - линия
   if(b) _WshObj.Range(static_cast_to_string(Cell)).Borders(4).LineStyle = n;

   return 0;
}
   
tMergeCellsExcelCell(cell,b) 
{
  _WshObj.Range(static_cast_to_string(cell)).MergeCells = b;
  return 0;
}

tMergeCellsExcel2(row1,col1,row2,col2,b)
{
  _WshObj.Range(_WshObj.Cells(row1,col1),_WshObj.Cells(row2,col2)).MergeCells = b;
  return 0;
}

tExcelPageBreaks(row)
{
  char str[256]; 
  sprintf(str,"%d:%d",row,row);
  var comRange = _WshObj.Range(static_cast_to_string(str));
  var hBreak = _WshObj.HPageBreaks();
  hBreak.Add(comRange);
  return 0;
}

tExcelRowHeight(row,h)
{
  char str[256]; 
  sprintf(str,"%d:%d",row,row);  
  _WshObj.Rows(static_cast_to_string(str)).Select();
  _XlsObj.Selection.RowHeight = h; 
  return 0;
}

tExcelColumnWidth(col,h)
{
  try 
  { 
   char str[256]; 
   sprintf(str,"%s:%s",col,col);  
   _WshObj.Columns(static_cast_to_string(str)).Select();
   _XlsObj.Selection.ColumnWidth = h; 
  }
  catch(...)
  {
   _WshObj.Columns(col).Select();
   _XlsObj.Selection.ColumnWidth = h;
  }

  return 0;
}

tHorizontalAlignmentExcel(cell,i) 
{
  _WshObj.Range(static_cast_to_string(cell)).HorizontalAlignment = i;  
  return 0;
}

tSaveExcelObjects(FileName)
{
  _WshObj.SaveAs(FileName); 
  return 0;
}

tUnderlineExcel(cell,i)
{
  bool b = static_cast_to_bool(i);
  _WshObj.Range(static_cast_to_string(cell)).Font.Underline = b;  
  return 0;
}

tAddExcelWorkbook(n,fullFileName,tmpnm)
{
  _XlsObj.Workbooks.Add(fullFileName); // создаем новую книгу на основе шаблона
  _XlsObj.Workbooks(n).SaveAs(tmpnm);

  return 0;
}

tCopyExcelWshByName(i,name)
{
  _XlsObj.Worksheets(i).Copy(_XlsObj.Worksheets(i));
  _WshObj = _XlsObj.Worksheets(i);
  _WshIndex = i;
  _WshObj.Name = name;

  return 0;
}

tSaveExcelWorkbook(n,tmpnm)
{
  _XlsObj.Workbooks(n).SaveAs(tmpnm);

  return 0;
}

tGetWshCountExcel(&tCount)
{
  tCount = _XlsObj.Worksheets.Count();
  return 0;
}

tWshExcelVisible(b)
{
  _WshObj.Visible = b;
  return 0;
}

tCopyExcelRowsWsh(wsh1,wsh2,n1,n2,count)
{
  _WshObj = _XlsObj.Worksheets.Item(wsh2);
  _WshObj.Select();
  _WshObj.Rows(n2).Select();

  _WshObj = _XlsObj.Worksheets.Item(wsh1);
  _WshObj.Select();
  char str[255];
  sprintf(str,"%d:%d",n1,n1+count-1);
  _WshObj.Rows(static_cast_to_string(str)).Select();
  _XlsObj.Selection.Copy();//_WshObj.Rows(n2));

  _WshObj = _XlsObj.Worksheets.Item(wsh2);
  _WshObj.Select();
  _WshObj.Rows(n2).Select();
  _WshObj.Paste();

  return 0;
}

tExcelClearContents(row1,col1,row2,col2,LineS)
{
  _WshObj.Range(_WshObj.Cells(row1,col1),_WshObj.Cells(row2,col2)).Select();
  _XlsObj.Selection().ClearContents();
  if ( LineS ) _XlsObj.Selection().Borders().LineStyle=0;                   
  return 0;
}

tExelQuit()
{
  _XlsObj.Quit();

  return 0;
}

tExcelWshCount()
{
  return _XlsObj.Worksheets.Count();
}

tExcelRefWshByIndex(i)
{
  return _XlsObj.Worksheets(i);
}

tDelExcelCol(sheet,n,count)
{
  for ( int i = 0; i < count; i++ ) 
    sheet.Columns(n).Delete();
  return 0;
}

tFreeExcelClipboard()
{
   _WshObj.Range("A1").Select();
   _XlsObj.Selection.Copy();
   _XlsObj.CutCopyMode = false;
}
