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.WorkSheets.Name := 'Delphi Data';
// Вставка данных в лист
S := XLApp.Workbooks.WorkSheets;
for i := 1 to 10 do S.Cells := i;
S.Cells := '=Sum(A1:A10)'; // Вставка формулы
// Работа с областью (рангом)
R:=XLApp.Workbooks.WorkSheets.Range;
R.Formula := '=RAND()'; // Вставка формулы в ранг
R.Columns.Interior.ColorIndex := 3; // Цвет закраски фона
R.Font.Color := clGreen; // Цвет ранга
R.Borders.LineStyle := cXL2; // Ячейки обрамленные
// Работа с колонкой и шрифтом
C:=XLApp.Workbooks.WorkSheets.Columns;
C.Columns.ColumnWidth := 5; C.Columns.Item.Font.Bold := True;
C.Columns.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:=120; StringGrid1.ColWidths:=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; s2:=ee.Cells.Item;
if s1='' then break;
StringGrid1.Cells:=s1; StringGrid1.Cells:=s2;
end; StringGrid1.RowCount:=i; ee.Disconnect;
end;<
Bu ve 399 TRY karşılığında 2 kitap daha