Tasuta

Иcпользование API на Delphi 7

Tekst
Märgi loetuks
Šrift:Väiksem АаSuurem Aa

2. РАБОТА С ПРИЛОЖЕНИЕМ «MS EXCEL»

unit Main;

….

  private

    XLApp: Variant;

  public

implementation

uses … ComObj, Variants;

const cXL1 = -4167; cXL2 = 1;

procedure TForm1.FormDestroy(Sender: TObject);

{ Уничтожение формы }

begin

  { Если открыт нужный лист Excel, то … }

  if not VarIsEmpty(XLApp) then begin

    XLApp.DisplayAlerts := False; // если True – то спрашивает о сохр.

    XLApp.Quit; // Закрытие Excel'a

  end;

end;

procedure TForm1.Button1Click(Sender: TObject);

var R, C, S: Variant; i: Integer;

begin

  XLApp:= CreateOleObject('Excel.Application'); // Подключение к Excel

  XLApp.Visible := True; // Включение Excel на экран

  XLApp.Workbooks.Add(cXL1); // Добавляем новый лист

  // Задаем листу название

  XLApp.Workbooks[1].WorkSheets[1].Name := 'Delphi Data';

  // Вставка данных в лист

  S := XLApp.Workbooks[1].WorkSheets['Delphi Data'];

  for i := 1 to 10 do S.Cells[i, 1] := i;

  S.Cells[i, 1] := '=Sum(A1:A10)'; // Вставка формулы

  // Работа с областью (рангом)

  R:=XLApp.Workbooks[1].WorkSheets['Delphi Data'].Range['C1:F25'];

  R.Formula := '=RAND()'; // Вставка формулы в ранг

  R.Columns.Interior.ColorIndex := 3; // Цвет закраски фона

  R.Font.Color := clGreen; // Цвет ранга

  R.Borders.LineStyle := cXL2; // Ячейки обрамленные

  // Работа с колонкой и шрифтом

  C:=XLApp.Workbooks[1].WorkSheets['Delphi Data'].Columns;

  C.Columns[1].ColumnWidth := 5; C.Columns.Item[1].Font.Bold := True;

  C.Columns[1].Font.Color := clBlue;

end;

end.

3. ИМПОРТ/ЭКСПОРТ ДАННЫХ ИЗ EXCEL

unit Unit1;

interface

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls, OleServer, ExcelXP, comobj;

type

  TForm1 = class(TForm)

    ee: TExcelApplication;

    Button1: TButton;

    StringGrid1: TStringGrid;

    Button2: TButton;

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;

var Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

// Импорт данных из Excel в StringGrid

var i: integer; s1,s2: string;

begin

  StringGrid1.ColWidths[0]:=120; StringGrid1.ColWidths[1]:=320;

  ee.Workbooks.Add('D:\Report11.xls ', 0);

  ee.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;

  StringGrid1.RowCount:=ee.ActiveCell.Row;

  for i:=0 to ee.ActiveCell.Row do begin

    s1:=ee.Cells.Item[8+i, 2]; s2:=ee.Cells.Item[8+i, 3];

    if s1='' then break;

    StringGrid1.Cells[0, i]:=s1; StringGrid1.Cells[1, i]:=s2;

  end; StringGrid1.RowCount:=i; ee.Disconnect;

end;

procedure TForm1.Button2Click(Sender: TObject);

// Экспорт данных из StringGrid в Excel

var e,r: Variant; i: integer;

begin

  e:=CreateOleObject('Excel.Application');

  e.Visible:=true; e.WorkBooks.Add;

  r:=e.Workbooks[1].WorkSheets[1];

  r.Columns[1].ColumnWidth := 12;

  r.Columns[2].ColumnWidth := 22;

  for i:=0 to StringGrid1.RowCount-1 do begin

    e.Cells.Item[i+1, 2]:=StringGrid1.Cells[0, i];

    e.Cells.Item[i+1, 3]:=StringGrid1.Cells[1, i];

  end;

  //e.Quit;

end;

end.