procedure TFrm_Xzdmwh.SaveToExcelFile(FileName: String);
var
eclApp, WorkBook: Variant;
xlsFileName: string;
StrXian, StrXiang, StrCun, StrSQL: String;
SheetLine, i, j: Integer;
begin
xlsFileName := FileName;
try
eclApp := CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;
try
workBook := eclApp.workBooks.Add;
with tab_xian do
begin
StrXian := FieldByName('qhdm').AsString;
eclApp.Cells(1, 2) := FieldByName('xzqm').AsString;
end;
SheetLine := 1;
with ADOQueryIO do
begin
Close;
SQL.Clear;
StrSQL := Format('Select xzqxdm, xzqxm from xzqx where qhdm = ''%s'' ',[StrXian]);
SQL.Add(StrSQL);
Open;
First;
for i := 0 to RecordCount - 1 do
begin
SheetLine := SheetLine + 1;
StrXiang := FieldByName('xzqxdm').AsString;
eclApp.Cells(SheetLine, 3) := FieldByName('xzqxm').AsString;
with ADOQueryIO1 do
begin
Close;
SQL.Clear;
StrSQL := Format('Select qhdm, xzqxdm, qsdm, qsqm from qsqy where xzqxdm = ''%s'' ',[StrXiang]);
SQL.Add(StrSQL);
Open;
First;
for j := 0 to RecordCount - 1 do
begin
SheetLine := SheetLine + 1;
eclApp.Cells(SheetLine, 2) := Char(255) + FieldByName('qhdm').AsString;
eclApp.Cells(SheetLine, 3) := Char(255) + FieldByName('xzqxdm').AsString;
StrCun := FieldByName('qsdm').AsString;
eclApp.Cells(SheetLine, 4) := Char(255) + StrCun;
eclApp.Cells(SheetLine, 5) := FieldByName('qsqm').AsString;;
if not eof then
Next;
end;
end;
if not eof then
Next;
end;
end;
WorkBook.saveas(xlsFileName);
WorkBook.close;
WorkBook:=eclApp.workBooks.Open(xlsFileName);
if MessageDlg(xlsFileName+'文件已被修改,是否保存?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
WorkBook.save
else
workBook.Saved := True; //放弃修改
WorkBook.Close;
eclApp.Quit;
eclApp:=Unassigned;
except
ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。');
WorkBook.close;
eclApp.Quit;
eclApp:=Unassigned;
end;
end;
var
eclApp, WorkBook: Variant;
xlsFileName: string;
StrXian, StrXiang, StrCun, StrSQL: String;
SheetLine, i, j: Integer;
begin
xlsFileName := FileName;
try
eclApp := CreateOleObject('Excel.Application');
WorkBook:=CreateOleobject('Excel.Sheet');
except
ShowMessage('您的机器里未安装Microsoft Excel。');
Exit;
end;
try
workBook := eclApp.workBooks.Add;
with tab_xian do
begin
StrXian := FieldByName('qhdm').AsString;
eclApp.Cells(1, 2) := FieldByName('xzqm').AsString;
end;
SheetLine := 1;
with ADOQueryIO do
begin
Close;
SQL.Clear;
StrSQL := Format('Select xzqxdm, xzqxm from xzqx where qhdm = ''%s'' ',[StrXian]);
SQL.Add(StrSQL);
Open;
First;
for i := 0 to RecordCount - 1 do
begin
SheetLine := SheetLine + 1;
StrXiang := FieldByName('xzqxdm').AsString;
eclApp.Cells(SheetLine, 3) := FieldByName('xzqxm').AsString;
with ADOQueryIO1 do
begin
Close;
SQL.Clear;
StrSQL := Format('Select qhdm, xzqxdm, qsdm, qsqm from qsqy where xzqxdm = ''%s'' ',[StrXiang]);
SQL.Add(StrSQL);
Open;
First;
for j := 0 to RecordCount - 1 do
begin
SheetLine := SheetLine + 1;
eclApp.Cells(SheetLine, 2) := Char(255) + FieldByName('qhdm').AsString;
eclApp.Cells(SheetLine, 3) := Char(255) + FieldByName('xzqxdm').AsString;
StrCun := FieldByName('qsdm').AsString;
eclApp.Cells(SheetLine, 4) := Char(255) + StrCun;
eclApp.Cells(SheetLine, 5) := FieldByName('qsqm').AsString;;
if not eof then
Next;
end;
end;
if not eof then
Next;
end;
end;
WorkBook.saveas(xlsFileName);
WorkBook.close;
WorkBook:=eclApp.workBooks.Open(xlsFileName);
if MessageDlg(xlsFileName+'文件已被修改,是否保存?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
WorkBook.save
else
workBook.Saved := True; //放弃修改
WorkBook.Close;
eclApp.Quit;
eclApp:=Unassigned;
except
ShowMessage('不能正确操作Excel文件。可能是该文件已被其他程序打开,或系统错误。');
WorkBook.close;
eclApp.Quit;
eclApp:=Unassigned;
end;
end;