📄 unit1.~pas
字号:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, Excel2000,ComObj;
type
TForm1 = class(TForm)
Button1: TButton;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
x1,x2,x3,aa,s:string;
i:integer;
v:variant;
begin
x3:='e:\example.xls'; //文件名
x1:='e:\11.xls';
x2:='e:\22.xls';
if fileexists(x3) then deletefile(x3);
//加上一句拷贝语句,将x1拷贝到x3
copyfile(pchar(x1),pchar(x3),true);
v:=CreateOLEObject('Excel.Application'); //建立OLE对象
v.Workbooks.Open(x3);
v.Visible:=true;
i:=v.ActiveSheet.UsedRange.Rows.Count;
// aa='a'&
//ExcelApplication1.Range[aa].Value:="";
str(i+3,s);
aa:='A'+ s;
//ExcelApplication1.ran
//v.Range[aa].Value:="";
v.Range[aa].Select;
//v.Range(aa).Select;
v.ActiveSheet.OLEObjects.Add(FileName:=x2, Link:=False, DisplayAsIcon:=False).Select;
v.ActiveWorkbook.Saveas(filename:=x3);//文件存盘
{ V.WorkBooks.Add;
if Checkbox1.Checked then
begin
V.Visible:=True;
MainForm.WindowState:=wsMinimized;
//使Excel可见,并将本程序最小化,以观察Excel的运行情况
end
else
begin
V.Visible:=False;
end;
//使Excel窗口不可见
Application.BringToFront; //程序前置
try
try
Cursor:=crSQLWait;
Table1.DisableControls;
For i:=0 to Table1.FieldCount-1 do //字段数
//注意:Delphi中的数组的下标是从0开始的,
// 而Excel的表格是从1开始编号
begin
V.Goto('R1'+'C'+IntToStr(i+1)); //Excel的表格是从1开始编号
V.ActiveCell.FormulaR1C1:=Table1.Fields[i].FieldName;//传送字段名
end;
j:=2;
Table1.First;
while not Table1.EOF do
begin
For i:=0 to Table1.FieldCount-1 do //字段数
begin
V.Goto('R'+IntToStr(j)+'C'+IntToStr(i+1));
V.ActiveCell.FormulaR1C1:=Table1.Fields[i].AsString;//传送内容
end;
Table1.Next;
j:=j+1;
end;
V.ActiveSheet.Protect(DrawingObjects:=True,Contents:=True,Scenarios:=True);//设置保护
ShowMessage('数据库到Excel的数据传输完毕!');
v.ActiveWorkBook.Saveas(filename:=s);//文件存盘
except //发生错误时
ShowMessage('没有发现Excel!');
end;
finally
Cursor:=crDefault;
Table1.First;
Table1.EnableControls;
end; }
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -