ADOQuery查询的结果,如何用excel导出打印(30分)

  • 主题发起人 主题发起人 haozi791007
  • 开始时间 开始时间
H

haozi791007

Unregistered / Unconfirmed
GUEST, unregistred user!
情况是这样:按查询按钮,由ADOQuery查询,并通过DbGrid显示出来。
设计在按导出按钮时,能够将查询结果导出到excel中。
问题:查询结果在excel中没有显示???为什么呀??
 
var
s:tstringlist;
begin
s:=tstringlist.Create;
//用个for循环
s.Add('字段值1'+#9+'字段值2'+#9+'字段值3'+#9+......);每一行数据
......
s.Add('字段值1'+#9+'字段值2'+#9+'字段值3'+#9+......);最后一行数据
s.savetofile('d:/mydata.xls');
end;
 
s:=Tstringlist.Create;
while ADOQuery1.Eofdo
begin
Mystring:=ADOQuery1.Fields[0].AsString;
for i := 1 to ADOQuery1.FieldCount-1do
begin
Mystring:=Mystring+#9+ ADOQuery1.Fields.AsString;
end;
s.Add(Mystring);
ADOQuery1.Next;
end;
s.SaveToFile('d:/mydata.xls');
我这么些对吗?为什么文件里什么也没有???
 
单元应用:
Windows, Messages, SysUtils, Variants, Classes, Graphics,
Controls, Forms,
Dialogs, StdCtrls, DB, DBTables, Grids, DBGrids, ActiveX, ComObj,
Excel2000, OleServer;
测试环境:
  OS:Win2k Pro;Excel2k;Delphi6.0
源程序:  
{
功能描述:把DBGrid输出到Excel表格(支持多Sheet)
调用格式:CopyDbDataToExcel([DBGrid1, DBGrid2]);
}
procedure CopyDbDataToExcel(Args: array of const);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;
I: Integer;
begin
Screen.Cursor := crHourGlass;
if not VarIsEmpty(XLApp) then
begin
XLApp.DisplayAlerts := False;
XLApp.Quit;
VarClear(XLApp);
end;
try
XLApp := CreateOleObject('Excel.Application');
except
Screen.Cursor := crDefault;
Exit;
end;
XLApp.WorkBooks.Add;
XLApp.SheetsInNewWorkbook := High(Args) + 1;
for I := Low(Args) to High(Args)do
begin
XLApp.WorkBooks[1].WorkSheets[I+1].Name :=
TDBGrid(Args.VObject).Name;
Sheet :=
XLApp.Workbooks[1].WorkSheets[TDBGrid(Args.VObject).Name];

if not TDBGrid(Args.VObject).DataSource.DataSet.Active then
begin
Screen.Cursor := crDefault;
Exit;
end;
TDBGrid(Args.VObject).DataSource.DataSet.first;
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1do
Sheet.Cells[1, iCount + 1] :=

TDBGrid(Args.VObject).Columns.Items[iCount].Title.Caption;

jCount := 1;
while not TDBGrid(Args.VObject).DataSource.DataSet.Eofdo
begin
for iCount := 0 to TDBGrid(Args.VObject).Columns.Count - 1
do
Sheet.Cells[jCount + 1, iCount + 1] :=

TDBGrid(Args.VObject).Columns.Items[iCount].Field.AsString;

Inc(jCount);
TDBGrid(Args.VObject).DataSource.DataSet.Next;
end;
end;
XlApp.Visible := True;
Screen.Cursor := crDefault;
end;
 
先谢谢humanc2d4和pandababy两位老兄[:)]
还有一个问题,能不能再delphi中设置excel表格具体cells[]的字体,颜色,大小
还有border的类型呢
 
后退
顶部