uses Excel97;
procedure ExcelOut;
var workbook,sheet:variant;
xlapp,q:variant;
begin
xlapp:=GetExcelObject('Excel.Application');
xlapp.visible:=true; //此属性为true,导出时会打开一个Excel文件,看到一行一行的写入数据
if xlapp.workbooks.count<=0 then
xlapp.workbooks.add;
xlapp.worksheets.add;
sheet:=xlapp.ActiveWorkBook.activesheet;
sheet.cells[1,6].font.color:=clgreen;//单元格字体颜色
sheet.cells[1,6].font.size:=20;//单元格字体大小
sheet.cells[1,6].font.bold:=true;//单元格字体样式
sheet.cells[1,6]:='表头'; //单元格内容
sheet.rows[5].font.bold:=true;//设置整行字体样式
sheet.rows[5].HorizontalAlignment := xlCenter;//设置整行每单元格内容居中
sheet.rows[5].Interior.Color:=clyellow;//设置整行背景色
q:=sheet.Range['A3','W5'];
q.borders.linestyle:=1;//设置某一区域边框可见
q:=sheet.Range['B3','E3'];
q.Merge;//合并单元格B3--E3
xlapp.ActiveWorkBook.SaveAs(savedialog1.FileName);//保存文件
xlapp.ActiveWorkBook.close;
xlapp.quit;//关闭退出,否则线程里会留下Excel.exe
end;
Delphi中数据集导出为Excel
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240813180353098-2140851696.jpg)