需要把数据导出到 Excel 中
学会录制宏,帮助很大
{ 保存成 excel }
procedure TFormPayBank.FSaveExcel(ds:TDataSet;haveWidth:boolean);
var
ExcelApp: Variant;
r,c:integer; // row column
rc:integer;
begin
//r := 1;
//c := 1;
screen.Cursor := crHourGlass;
self.Caption := '正在导出数据......';
ExcelApp := CreateOleObject( 'Excel.Application');
try
ExcelApp.Caption := '银行报盘';
ExcelApp.WorkBooks.Add;
ExcelApp.WorkBooks[1].Activate;
ds.First;
rc := ds.RecordCount;
for r := 1 to rc do
begin
for c := 1 to ds.FieldCount-1 do
begin
ExcelApp.Cells[r,c] := ds.Fields[c-1].AsString
end;
ds.Next;
end;
if haveWidth then
begin
ExcelApp.ActiveSheet.Columns[1].ColumnWidth := 19;
ExcelApp.ActiveSheet.Columns[1].NumberFormatLocal := '@';
//ExcelApp.ActiveSheet.Range('''A1:A'+intTostr(rc)+'''').HorizontalAlignment:='xlLeft';
ExcelApp.ActiveSheet.Columns[2].ColumnWidth := 16;
ExcelApp.ActiveSheet.Columns[3].ColumnWidth := 42;
end;
//ExcelApp.SaveAs('c:/a.xls');
ExcelApp.Visible := true;
finally
//ExcelApp.WorkBooks.Close;
ExcelApp := null;
screen.Cursor := crDefault;
self.Caption := '银行报盘';
end;
end;