首先,要在form中添加 Excelapplication1,excelworksheet1和 Excelworkbook1 三个控件。
procedure TForm2.Button2Click(Sender: TObject);
var
temp_worksheet:_worksheet;
begin
//连接Excel
Excelapplication1.Connect;
Excelapplication1.Visible[0]:=true;
Excelworkbook1.ConnectTo(Excelapplication1.workbooks.Add(emptyparam,0));temp_worksheet:=Excelworkbook1.Worksheets.add(emptyparam,emptyparam,emptyparam,emptyparam,0) as _worksheet;
excelworksheet1.ConnectTo(temp_worksheet);
excelworksheet1.Activate;
//数据与单元格设置
beginexcelworksheet1.Cells.Item[1,2]:='你好';
//合并单元格
excelworksheet1.Range['a1','c1'].mergecells:=true;
//字体设置
excelworksheet1.Range['a1','c1'].Font.FontStyle:='加粗';
excelworksheet1.Range['a1','c1'].Font.size:=30;
excelworksheet1.Range['a1','c1'].Rowheight:=70;
excelworksheet1.Range['a1','c1'].Borders.lineStyle:=1;
excelworksheet1.Range['a1','c1'].hrizonta1Alignment:=xlcenter;
excelworksheet1.Range['a1','c1'].vertiaclAlignment:=xlcenter;
//与应用程序脱钩
Excelworksheet1.Disconnect;
Excelworkbook1.Disconnect;
Excelapplication1.Disconnect;
end;