将查询结果保存(30分)

  • 主题发起人 主题发起人 gzmich
  • 开始时间 开始时间
G

gzmich

Unregistered / Unconfirmed
GUEST, unregistred user!
请问,如何将QUERY部件SQL语言的查询结果保存到Excel文件
 
很高兴今天能和您聊得这么深入,
非常感谢您对我的指导!希望
我们能成为好朋友。

procedure TransToExcel;
var
MsExcel: Variant;
begin
try
MsExcel:=CreateOleObject('Excel.Application');
except
ShowMessage('无法打开Microsoft Excel');
exit;
end;

MsExcel.Visible:=True;
MsExcel.WorkBooks.Add;
if FDataFrom is TStringGrid then
TransStringGridToExcel(FDataFrom As TStringGrid,MsExcel);
if FDataFrom is TDecisionGrid then
TransDecisionGridToExcel(FDataFrom As TDecisionGrid,MsExcel);
if FDataFrom is TDBGrid then
TransDBGridToExcel(FDataFrom As TDBGrid,MsExcel);

end;

procedure TransDecisionGridToExcel(DG1:TDecisionGrid;MsExcel:Variant);
var
i,j:Integer;//循环变量
begin
ColNum:=0; RowNum:=0;
with DG1 do
begin
ColNum:=ColCount;
RowNum:=RowCount;
For j:=-2 to RowNum-3 do
For i:=-1 to ColNum-2 do
MsExcel.Cells[j+3,i+2].Value:=Cells[i,j];
end;{end with}
end;{end Prodedure}
{===========================================================================}




object Query_WZQYD_GZD_SFJL: TQuery
Active = True
CachedUpdates = True
DatabaseName = 'ydgl_prj'
DataSource = DataSource_WZQYD_GZD
RequestLive = True
SQL.Strings = (
'SELECT YD_JL_WZQYD_GZD_SFJL.*'
'FROM YD_JL_WZQYD_GZD_SFJL'
'WHERE YD_JL_WZQYD_GZD_SFJL.TZS_ID=:TZS_ID')
UpdateObject = UpdateSQL_WZQYD_GZD_SFJL
Left = 66
Top = 282
ParamData = <
item
DataType = ftString
Name = 'TZS_ID'
ParamType = ptUnknown
end>
end
object DataSource_WZQYD_GZD_SFJL: TDataSource
DataSet = Query_WZQYD_GZD_SFJL
Left = 222
Top = 282
end
object UpdateSQL_WZQYD_GZD_SFJL: TUpdateSQL
ModifySQL.Strings = (
'update YD_JL_WZQYD_GZD_SFJL'
'set'
' TZS_ID = :TZS_ID,'
' SFJL_ID = :SFJL_ID,'
' XM = :XM,'
' DL = :DL,'
' JE = :JE,'
' PJH = :PJH,'
' RQ = :RQ,'
' SFY = :SFY'
'where'
' TZS_ID = :OLD_TZS_ID')
InsertSQL.Strings = (
'insert into YD_JL_WZQYD_GZD_SFJL'
' (TZS_ID, SFJL_ID, XM, DL, JE, PJH, RQ, SFY)'
'values'
' (:TZS_ID, :SFJL_ID, :XM, :DL, :JE, :PJH, :RQ, :SFY)')
DeleteSQL.Strings = (
'delete from YD_JL_WZQYD_GZD_SFJL'
'where'
' TZS_ID = :OLD_TZS_ID')
Left = 392
Top = 282
end
object Query_WZQYD_GZD_SFJL_ADD: TQuery
DatabaseName = 'ydgl_prj'
Left = 566
Top = 282
end





















1001 新装
2001 改建
3001 定换
4001 故障换表_烧表
4002 故障换表_卡表
4003 故障换表_自走
4004 故障换表_超差
4101 故障换表_峰谷表液晶部分
4102 故障换表_峰谷表电子部分
4103 故障换表_峰谷表机械部分
4104 故障换表_峰谷表死机
4105 故障换表_峰谷表电池欠压
4109 故障换表_峰谷表其他原因
4901 故障换表_其他


drop table da_yyyymmdd;  
/
--如果有,先删除此表
create table da_yyyymmdd(rq char(8) PRIMARY KEY);    --建立表,rq(日期)为主关键字
/
declare
i number;
--循环用
j char(8);
--记录日期值
begin
j:='20000101';
--设初始值
for i in 1..18250 loop
--365*50=600天
insert into da_yyyymmdd values(j);
--insert记录
j:=to_char(to_date(j,'yyyymmdd')+1,'yyyymmdd');
--日期加1天
end loop;
--循环结束
end;
/
commit;





drop table da_yyyymmdd;  
/

create table da_yyyymmdd(rq char(8) PRIMARY KEY);
/

declare
i number;
j char(8);
begin
j:='20000101';
for i in 1..18250 loop
insert into da_yyyymmdd values(j);

j:=to_char(to_date(j,'yyyymmdd')+1,'yyyymmdd');

end loop;

end;
/
commit;


 
太全面了,同意
 
请问你是否试过 TBathMove控件??
 
解決思路:
1.建一臨時表(table1)結構與被查詢表格相同
2.運用TBathMove控件(Source設為Query1,Problemtable1name為table1)把Query1的數據'移到'臨時表中
3.啟動Excel,代碼如下,把臨時表table1中數據轉成Excel文件!

procedure TForm1.Button1Click(Sender: TObject);
var
excel_sheet:variant;
e_s:variant;
i,j:integer;
save_ok:boolean;
begin
Table1.TableName:=ComboBox1.Text;
Table1.Open;
try //啟動Excel
excel_sheet:=CreateOleObject('excel.sheet');
except
showmessage('無法啟動 MS Excel');
exit;
end;
excel_sheet.application.visible:=true; //啟示 MS Excel 執行情況
for i:=0 to table1.FieldCount-1 do
// excel_sheet.cells(1,i+1):=Table1.Fields.FullName;
j:=1;
while not Table1.Eof do
begin
j:=j+1;
for i:=0 to Table1.FieldCount-1 do
// excel_sheet.cells(j,i+1):=Table1.Fields.AsString;
Table1.Next;
end;
Table1.Close;
if messagedlg('轉換成攻! 要存檔嗎',mtinformation,[mbyes,mbno],0)=mryes then
begin
save_ok:=SaveDialog1.Execute;
if save_ok then
excel_sheet.saveas(SaveDialog1.FileName);
end;
excel_sheet.application.quit; //關閉 MS Excel
end;
 
excel也可以作为数据表打开!
 
多人接受答案了。
 
后退
顶部