给你一段我自己程序的代码,其实以前的回答有很多例子!
procedure TOfficeform.Button2Click(Sender: TObject);
var MSExcel:Variant;
i,j:Integer;
begin
SaveDialog1.Filter:='*.XLS|*.XLS';
SaveDialog1.DefaultExt:='XLS';
if SaveDialog1.Execute then
begin
MSExcel:=CreateOLEObject('Excel.Application');
MSExcel.WorkBooks.Add;
MSExcel.Visible:=False;
Table1.Open;
j:=Table1.RecordCount;
Table1.First;
for i:=1 to j do
begin
MSExcel.Cells[i,1].NumberFormat:='@';
MSExcel.Cells[i,1].Value:=Table1.FieldByName('CODE').AsString;
MSExcel.Cells[i,2].Value:=Table1.FieldByName('COLOR').AsString;
Table1.Next;
end;
MSExcel.ActiveWorkBook.SaveAs(SaveDialog1.FileName);
MSExcel.ActiveWorkBook.Saved:=True;
MSExcel.Quit;
end;
end;
****************************************
合并单元格:
procedure TForm1.Button1Click(Sender: TObject);
var
ExcelApp,Selection,WorkSheet:Variant;
begin
ExcelApp:=GetActiveOleObject('Excel.Application');
ExcelApp.Visible:=True;
WorkSheet:=ExcelApp.WorkSheets[1];
WorkSheet.Range['A1:C2'].Select;
ExcelApp.Selection.Merge;
end;