急!!!TQUERY的数据怎样用execel打印出来?
如何把 TQUERY查询的数据用execel打印出来,
我知道是用server下的Execel组件,但不知道具体是哪个空间,
能不能说详细点,最好给点代码。
感谢万分!!!
问题点数:100、回复次数:2Top
1 楼zfmich()回复于 2002-08-02 16:58:54 得分 100
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 ;
你可以参考一下,不过好象速度不是很理想。。
如果有改进意见,麻烦告诉我。Top
2 楼zfmich()回复于 2002-08-02 16:59:29 得分 0
忘了告诉你,上面的代码没有用控件。Top




