function ExportDellDataToExcel(AQry: TADOQuery; AExcelFile: string) :integer ;
var
EclApp : Variant;
Begin
Result := -1 ;
try
EclApp := CreateOleObject('Excel.Application');
except
Exit;
end; try
try
EclApp.WorkBooks.Add ;
EclApp.ActiveWorkBook.Saved:=True;
EclApp.WorkSheets[1].Activate;
EclApp.Cells.Font.Name := 'Arial' ;
EclApp.Cells.Font.Color := clBlack ;
EclApp.Cells.Font.Size := 9 ;
EclApp.Cells.Font.Bold := false ;
EclApp.Cells.Font.UnderLine := false ;
EclApp.Visible := false ; DataSetToSheet(EclApp.Activesheet, AQry) ; EclApp.ActiveWorkBook.SaveAs(AExcelFile);
Result := 1 ;
except
Result := 0 ;
end;
finally
EclApp.ActiveWorkBook.Saved:=True;
EclApp.ActiveWorkBook.Close;
eclApp.Quit; {释放VARIANT变量}
eclApp:=Unassigned;
end;
end;
procedure DataSetToSheet(ASheet :Variant; AQry :TADOQuery) ;
var
Row : Integer ;
sStr :string ;
begin
ASheet.Rows.RowHeight := 15;
ASheet.Columns.ColumnWidth := 10 ;
ASheet.Rows[1].Font.Name := 'Arial';
ASheet.Rows[1].Font.Color := clBlack;
ASheet.Rows[1].Font.Size := 9 ;
ASheet.Rows[1].Font.Bold := True;
ASheet.Rows[1].Font.UnderLine := false; ASheet.Range['A1:Z1'].Columns.Interior.Color := clYellow; ASheet.Cells(1, 1) := 'Item' ;
ASheet.Cells(1, 2) := 'Channel' ; Row := 2 ;
with AQry do
begin
First ;
while not Eof do
begin
Application.ProcessMessages ;
ASheet.Cells(Row, 1) := FieldByName('Item').asstring ;
ASheet.Cells(Row, 2) := FieldByName('Channel').asstring ;
Next ;
Inc(Row) ;
end ;
end;
sStr := 'A1:Z' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Borders.Color := clBlack ; sStr := 'A2:A' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'B2:B' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'C2:C' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'D2:D' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clAqua; // $0095E916 ; //
end ;你可以参考一下,不过好象速度不是很理想。。
如果有改进意见,麻烦告诉我。
var
EclApp : Variant;
Begin
Result := -1 ;
try
EclApp := CreateOleObject('Excel.Application');
except
Exit;
end; try
try
EclApp.WorkBooks.Add ;
EclApp.ActiveWorkBook.Saved:=True;
EclApp.WorkSheets[1].Activate;
EclApp.Cells.Font.Name := 'Arial' ;
EclApp.Cells.Font.Color := clBlack ;
EclApp.Cells.Font.Size := 9 ;
EclApp.Cells.Font.Bold := false ;
EclApp.Cells.Font.UnderLine := false ;
EclApp.Visible := false ; DataSetToSheet(EclApp.Activesheet, AQry) ; EclApp.ActiveWorkBook.SaveAs(AExcelFile);
Result := 1 ;
except
Result := 0 ;
end;
finally
EclApp.ActiveWorkBook.Saved:=True;
EclApp.ActiveWorkBook.Close;
eclApp.Quit; {释放VARIANT变量}
eclApp:=Unassigned;
end;
end;
procedure DataSetToSheet(ASheet :Variant; AQry :TADOQuery) ;
var
Row : Integer ;
sStr :string ;
begin
ASheet.Rows.RowHeight := 15;
ASheet.Columns.ColumnWidth := 10 ;
ASheet.Rows[1].Font.Name := 'Arial';
ASheet.Rows[1].Font.Color := clBlack;
ASheet.Rows[1].Font.Size := 9 ;
ASheet.Rows[1].Font.Bold := True;
ASheet.Rows[1].Font.UnderLine := false; ASheet.Range['A1:Z1'].Columns.Interior.Color := clYellow; ASheet.Cells(1, 1) := 'Item' ;
ASheet.Cells(1, 2) := 'Channel' ; Row := 2 ;
with AQry do
begin
First ;
while not Eof do
begin
Application.ProcessMessages ;
ASheet.Cells(Row, 1) := FieldByName('Item').asstring ;
ASheet.Cells(Row, 2) := FieldByName('Channel').asstring ;
Next ;
Inc(Row) ;
end ;
end;
sStr := 'A1:Z' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Borders.Color := clBlack ; sStr := 'A2:A' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'B2:B' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'C2:C' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clOlive;
sStr := 'D2:D' + IntToStr(Row - 1) ;
ASheet.Range[sStr].Columns.Interior.Color := clAqua; // $0095E916 ; //
end ;你可以参考一下,不过好象速度不是很理想。。
如果有改进意见,麻烦告诉我。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货