unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtnrs,comobj;

type
  TForm1 = class(TForm)
    OleContainer1: TOleContainer;
    Button2: TButton;
    Button4: TButton;
    OpenDialog1: TOpenDialog;
    Label1: TLabel;
    Button7: TButton;
    SaveDialog1: TSaveDialog;
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
const
  xlWBATWorksheet =$FFFFEFB9; // -4167;
  {wdDoNotSaveChanges = 0;
  wdSaveChanges = -1;
  wdPromptToSaveChanges=-2;
  wdWindowStateMaximize = 1;
  wdWord9TableBehavior= 1;
   }
  wdCell=12;
  xlExcel8=56; //Excel8
  xlAddIn=18;	//Microsoft Office Excel Add-In
  xlAddIn8=18; //	Excel 2007 Add-In
  wdColorBrightGreen=65280;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
  Opendialog1.InitialDir:=GetCurrentdir;
  if OpenDialog1.Execute then
  Olecontainer1.CreateObjectFromFile(Opendialog1.FileName,false);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  try
   if OLEContainer1.Modified then OLEContainer1.SaveAsDocument('nowy.xls');
   Olecontainer1.Close;
   Olecontainer1.DestroyObject;
  except
    Showmessage('OlEContainer jest pusty!');
  end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var ExcelA,Arkusz: OLEVariant;
begin
  Opendialog1.InitialDir:=GetCurrentdir;
  if OpenDialog1.Execute then
  begin
   ExcelA:=CreateOleObject('Excel.Application');
   ExcelA.Workbooks.Open(Opendialog1.filename); //Worksheet[1].Open(Opendialog1.FileName);

  Arkusz := ExcelA.Workbooks[1].WorkSheets[1];
  //modyfikuj wczytany plik
  //wypelnieie zakresu komórek kolorem zielonym
   Arkusz.Range['B4','B7'].Interior.Color := wdColorBrightGreen;

  //obliczenie sredniej liczb z komórek B4 do B7, wynik w B6
  //w przykladowym arkuszu w komórkach B4 do B7 są liczby
  Arkusz.Range['B8','B8'].Value:='=AVERAGE(B4:B7)';
   ExcelA.Workbooks.Close;  //tu można zamknąc i zapisa zmiany
  end;
  //można tez tak zapisac zmodyfikowany skoroszytu
  // Savedialog1.InitialDir:=GetCurrentdir;
  //if SaveDialog1.Execute then
  // ExcelA.Workbooks[1].SaveAs(Savedialog1.FileName,xlAddIn);
  //ExcelA.Workbooks[1].Close;
  ExcelA.Quit;
end;

end.
