求delphi中SaveDialog用把stringGrid 中的内容保存为excel文件的代码

来源:百度知道 编辑:UC知道 时间:2024/06/14 16:25:46
delphi中SaveDialog用把stringGrid 中的内容保存为excel文件的代码
我的stringGrid 中的 内容是动态的,而且是从数据集中筛选出来的,我 要保存的格式是和stringGrid 一样的

procedure Stringgridtoexcel(atitle: string;
Astringgrid: Tstringgrid; Afontsize: integer);
var xlapp, xlsheet: variant;
row, CCC, nnn, jjj: integer;
begin
try
xlapp := createoleobject('excel.application');
except
showmessage('not found excel in your system, so can not create file!');
exit;
end;
try
ccc := Astringgrid.ColCount;
xlapp.workbooks.add; //添加新工作簿
xlapp.visible := false;
xlsheet := xlapp.activesheet;
xlapp.activewindow.windowstate := 2;
xlapp.range[xlsheet.cells[1, 1], xlsheet.cells[1, ccc]].MERGE;
xlsheet.cells[1, 1].value := Atitle; //页头第一行;
xlsheet.cells[1, 1].HorizontalAlignment := -4108;

row := 2;
for jjj := 0 to Astringgrid.RowCount - 1 do
begin
for nnn := 1 to ccc do
xlsheet.cells[