Урок на Делфи 78. Экспорт в Excel
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs,Vcl.StdCtrls,ComObj;
type
TForm1 = class(TForm)
Edit1: TEdit;
ListBox1: TListBox;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button4: TButton;
ListBox2: TListBox;
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button4Click(Sender: TObject);
var
Range,Sheet,Excel: Variant;
i:integer;
begin
Excel := CreateOleObject('Excel.Application');
Excel.SheetsInNewWorkbook := 1;
Excel.Workbooks.Add;
Sheet := Excel.Workbooks[1].Sheets[1];
Range := Sheet.Columns;
Range.Columns[1].ColumnWidth := 15;
Range.Columns[2].ColumnWidth := 15;
Range.Columns[3].ColumnWidth := 25;
Range.Columns.Font.Size := 8;
Range := Sheet.Range['a1:d1'];
Range.Font.Size := 15;
Range.Font.Bold := True;
Range.Font.Italic := True;
Range.font.color:=clRed;
Range.Columns.Interior.ColorIndex := 6;
Range.HorizontalAlignment := 3;
Sheet.Cells[1,2] := Edit1.Text;
Range := Sheet.Range['a2:d2'];
Range.Font.Size := 12;
Range.font.color:=clBlue;
Sheet.Cells[2,1] := Edit3.Text;
Sheet.Cells[2,2] := Edit4.Text;
Sheet.Cells[2,3] := Edit2.Text;
for I := 0 to ListBox2.Count-1 do
begin
Sheet.Cells[i+3,1] := ListBox2.Items[i] ;
end;
//Range.Font.Size := 12;
for I := 0 to ListBox1.Count-1 do
begin
Sheet.Cells[i+3,2] := ListBox1.Items[i] ;
end;
Range := Sheet.Range['c3:c7'];
Range.Font.Size := 12;
Range.font.color:=clPurple;
for I := 0 to 4 do
Sheet.Cells[i+3,3] := '';
Screen.Cursor := crDefault;
Excel.Visible := True;
//Excel.WorkBooks[1].SaveAs(ExtractFilePath(Application.ExeName)+'Excel.xlsx');
Excel.ActiveWorkBook.SaveAs(ExtractFilePath(Application.ExeName)+'Excel.xlsx');
end;
end.