📄 unit1.pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExcelXP, OleServer, DB, DBTables, Grids,
DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Table1: TTable;
DataSource1: TDataSource;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i,row,column:integer;
begin
try
excelapplication1.Connect;
except
messagedlg('Excel may not be installed',mterror,[mbok],0); //在屏幕中间显示信息
abort; //终止正在打印的打印机,取下所以没有打印的数据
end;
excelapplication1.Visible[0]:=true;
excelapplication1.Caption:='Excel application';
excelapplication1.Workbooks.Add(null,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets[1] as _worksheet);
table1.Open;
row:=1;
while not(table1.Eof) do
begin
column:=1;
for i:=1 to table1.FieldCount do
begin
excelworksheet1.Cells.Item[row,column] :=table1.Fields[i-1].AsString;
column:=column+1;
end;
table1.Next;
row:=row+1;
end;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
excelapplication1.Disconnect;
excelapplication1.Quit;
end;
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
excelworksheet1.PrintPreview;
end;
procedure TForm1.BitBtn4Click(Sender: TObject);
begin
excelworksheet1.PrintOut;
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -