procedure ExportToExcel(ADOQry: TADOQuery);
var
MyExcel: Variant;
WorkBook: OleVariant;
WorkSheet: OleVariant;
i,j:integer;
begin
try
MyExcel:=CreateOleObject('Excel.Application');
MyExcel.Application.WorkBooks.Add;
MyExcel.Caption:='将数据导入到EXCEL表中';
MyExcel.Application.Visible:=true;
WorkBook:=MyExcel.Application.workbooks[1];
Worksheet:=workbook.worksheets.item[1];
except
Application.MessageBox ('对不起,请确认您是否安装了EXCEL!','提示',MB_OK+MB_ICONINFORMATION);
end;
i:=1;
{
// With worksheet do
// begin
worksheet.cell.Range(worksheet.Cells(1, 1), worksheet.Cells(1, ADOQry.FieldCount)).Font.Name := Font
//'设标题为黑体字
Range(worksheet.Cells(1, 1), worksheet.Cells(1, ADOQry.FieldCount)).Font.Bold = True
//'标题字体加粗
Range(worksheet.Cells(1, 1), worksheet.Cells(Irowcount + 1, Icolcount)).Borders.LineStyle = xlContinuous
//'设表格边框样式
// end;
}
with ADOQry do
begin
for j:=0 to FieldCount-1 do
worksheet.cells[1,j+1]:=Fields[j].FieldName;
First;
while not Eof do
begin
inc(i);
try
for j:=0 to fieldcount-1 do
begin
worksheet.cells[i,j+1].NumberFormatLocal :='@';
worksheet.cells[i,j+1]:=Fields[j].asstring;
end;
Next;
except
Application.MessageBox('EXCEL非法操作!','提示',MB_OK+MB_ICONINFORMATION);
abort;
end;
end;
end;
end;
[
]