//用代码将查询结果导入excel
Procedure TFm_Total_Rep_Cable.Make_SQL;
var
FieldStr,SqlStr:String;
i:integer;
begin
//检查日期是否合法
if Edit1.Text<>'' then
Try
StrToDateTime(Edit1.Text);
except
Application.MessageBox(PChar('"'+Edit1.Text+'"不是一个合法的日期型式。'),'日期非法',Mb_Ok+Mb_IconInformation);
Edit1.Text:='';
Exit;
end;
if Edit2.Text<>'' then
Try
StrToDateTime(Edit2.Text);
except
Application.MessageBox(PChar('"'+Edit2.Text+'"不是一个合法的日期型式。'),'日期非法',Mb_Ok+Mb_IconInformation);
Edit2.Text:='';
Exit;
end;
SqlStr:='select Power_Station_List.变电站名称,Wire_List.线路名称,Cable.电缆名称,Cable.型号,Cable.截面,Cable.长度,Cable.投运日期 from Power_Station_List,Cable,Wire_List Where Cable.所属线路=Wire_List.id and Wire_List.出线变电站=Power_Station_List.id';
if Combobox1.Text<>'' then
SqlStr:=SqlStr+' And 类型='''+Combobox1.Text+'''';
if ((Edit1.Text<>'') and (Edit2.Text<>'')) then
SqlStr:=SqlStr+' and (投运日期>'''+Edit1.Text+''' And 投运日期<'''+Edit2.Text+''')';
if ((CheckBox3.Checked) and (Not CheckBox7.Checked)) then
SqlStr:=SqlStr+' and 属性=''局''';
if ((Not CheckBox3.Checked) and (CheckBox7.Checked)) then
SqlStr:=SqlStr+' and 属性=''用''';
SqlStr:=SqlStr+' Order by Power_Station_List.变电站名称,Wire_List.线路名称';
if CheckBox1.Checked then SqlStr:=SqlStr+',Cable.投运日期';
Memo1.Text:=SqlStr;
end;
procedure TFm_Total_Rep_Cable.SpeedButton3Click(Sender: TObject);
var
aSheet:Variant;
i,j,Loop1,Loop2:integer;
begin
Make_SQL;
if SaveDialog1.Execute then
begin
try
try
ProgressBar1.Visible:=True;
ProgressBar1.Position:=0;
ADODataSet1.Close;
ADODataSet1.CommandText:=Memo1.Text;
ADODataSet1.Open;
Loop1:=ADODataSet1.RecordCount;
Loop2:=ADODataSet1.FieldDefs.Count;
ProgressBar1.Max:=Loop1;
EA1.Connect;
EA1.Visible[0]:=False;
EA1.Workbooks.Add(xlWBATWorkSheet,0);
asheet:=EA1.Worksheets.Item[1];
for J:=1 to Loop2 do //写标题
begin
aSheet.Cells[1,j].Value:=ADODataSet1.Fields.Fields[j-1].FieldName;
end;
for i:=1 to Loop1 do //写单元格
begin
for J:=1 to Loop2 do
begin
aSheet.Cells[i+1,j].Value:=ADODataSet1.Fields.Fields[j-1].AsString;
end;
ADODataSet1.Next;
ProgressBar1.Position:=i;
Application.ProcessMessages;
end;
aSheet.SaveAs(SaveDialog1.FileName);
ProgressBar1.Position:=0;
ProgressBar1.Visible:=False;
Application.MessageBox('数据已成功导入!','导入成功',MB_Ok+Mb_IconInformation);
except
Application.MessageBox('数据导入失败!','导入失败',MB_Ok+Mb_IconInformation);
end;
finally
aSheet.Application.Quit;
ea1.Disconnect;
end;
end;
end;