Y
yaminboy
Unregistered / Unconfirmed
GUEST, unregistred user!
在我下面的程序中存二个尚不清楚的问题:
1、程序中第二次循环的目的,是要把另一数据表中每一个人都包括几年中数字转入Excel中,
可是程序运行后Excel中没有数字,不知是什么原因?
2、此程序如果遇到保存的文件重名或强行中断后,再打开生成的excel表后容易出错,造成excel无法正确打开,重新启动excel恢复正常。
以上二点问题请富翁们帮帮忙,谢谢!
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj,Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Database1: TDatabase;
Query1: TQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
Query2: TQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
eclApp,WorkBook:Variant;
xlsFileName,stxm,stxb,stbm,strybm:string;
i:integer;
begin
xlsFileName:='e:/access_excel/grzh.xls';
try
//创建OLE对象:Excel Application 与 WorkBook
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
WorkBook:=eclApp.workBooks.Open(xlsFileName);
except
ShowMessage('您的机器里未安装Microsoft Excel.');
Exit;
end;
i:=14;
query1.Active:=true;
query1.First;
Query2.Active:=true;
query2.First;
EclApp.Visible := False;
while not query1.Eof do begin
stbm:=query1.Fields[0].AsString;
stxm:=query1.Fields[1].AsString;
stxb:=query1.Fields[2].AsString;
WorkBook.WorkSheets[1].Cells[6,6].Value := stxm;
if stxb='0' then
WorkBook.WorkSheets[1].Cells[6,9].Value := '男'
else
WorkBook.WorkSheets[1].Cells[6,9].Value := '女';
strybm:=query2.Fields[3].AsString;
while stbm=strybm do begin
WorkBook.WorkSheets[1].Cells[3,i].Value := query2.Fields[1].AsInteger;
WorkBook.WorkSheets[1].Cells[4,i].Value := query2.Fields[2].AsInteger;
i:=i+1;
strybm:=query2.Fields[3].AsString;
query2.next;
end;
WorkBook.saveas('e:/access_excel/'+ stxm + '.xls');
query1.next;
end;
WorkBook.close;
EclApp.Quit;
EclApp:=Unassigned; //释放VARIANT变量
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
end.
1、程序中第二次循环的目的,是要把另一数据表中每一个人都包括几年中数字转入Excel中,
可是程序运行后Excel中没有数字,不知是什么原因?
2、此程序如果遇到保存的文件重名或强行中断后,再打开生成的excel表后容易出错,造成excel无法正确打开,重新启动excel恢复正常。
以上二点问题请富翁们帮帮忙,谢谢!
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj,Grids, DBGrids, DB, DBTables;
type
TForm1 = class(TForm)
Database1: TDatabase;
Query1: TQuery;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
Button1: TButton;
Button2: TButton;
Query2: TQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
eclApp,WorkBook:Variant;
xlsFileName,stxm,stxb,stbm,strybm:string;
i:integer;
begin
xlsFileName:='e:/access_excel/grzh.xls';
try
//创建OLE对象:Excel Application 与 WorkBook
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
WorkBook:=eclApp.workBooks.Open(xlsFileName);
except
ShowMessage('您的机器里未安装Microsoft Excel.');
Exit;
end;
i:=14;
query1.Active:=true;
query1.First;
Query2.Active:=true;
query2.First;
EclApp.Visible := False;
while not query1.Eof do begin
stbm:=query1.Fields[0].AsString;
stxm:=query1.Fields[1].AsString;
stxb:=query1.Fields[2].AsString;
WorkBook.WorkSheets[1].Cells[6,6].Value := stxm;
if stxb='0' then
WorkBook.WorkSheets[1].Cells[6,9].Value := '男'
else
WorkBook.WorkSheets[1].Cells[6,9].Value := '女';
strybm:=query2.Fields[3].AsString;
while stbm=strybm do begin
WorkBook.WorkSheets[1].Cells[3,i].Value := query2.Fields[1].AsInteger;
WorkBook.WorkSheets[1].Cells[4,i].Value := query2.Fields[2].AsInteger;
i:=i+1;
strybm:=query2.Fields[3].AsString;
query2.next;
end;
WorkBook.saveas('e:/access_excel/'+ stxm + '.xls');
query1.next;
end;
WorkBook.close;
EclApp.Quit;
EclApp:=Unassigned; //释放VARIANT变量
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
end.