S
SUB
Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是如下代码:
var
Excel,Sheet:Variant;
I,J,X,Y:Integer;
begin
Try
Excel:=UnAssigned;
Excel:=CreateOleObject('Excel.Application');
Excel.Visible:=False;
Excel.WorkBooks.Add;
Except
Excel:=UnAssigned;
DispInfo('本机未安装EXCEL,本功能必须在安装有EXCEL的电脑上才能运行!',1);
Abort;
End;
if VarIsEmpty(Excel) then
begin
DispInfo('建立Excel对象不成功,请重试!',1);
Excel:=UnAssigned;
Abort;
end;
X:=DbGridEh.FrozenCols;
DbGridEh.FrozenCols:=0;//设置锁定列为0
J:=0;
For I:=0 To DbGridEh.Columns.Count-1 Do
Begin
If DbGridEh.Columns.Visible Then
J:=J+1;
End; //取得显示列总列数
I:=Round(J/2);
sheet:=Excel.worksheets[1];
Sheet.Name:=TForm(DbGridEh.Parent.Parent).caption;
Sheet.Cells[1,I]:=TForm(DbGridEh.Parent.Parent).caption;
sheet.cells[2,1]:='日期:'+FormatDateTime('yyyy"年"mm"月"dd"日"',Now);
Sheet.Cells[2,J-1]:='制表:'+UserCode; //以上为表头输出
J:=1;
For I:=0 To DbGridEh.Columns.Count-1 Do
If DbGridEh.Columns.Visible Then
begin
Sheet.Cells[3,J]:=DbGridEh.Columns.Title.Caption; //设置列标题
J:=J+1;
end;
If Not DbGridEh.DataSource.DataSet.Active Then
Begin
Excel.Quit;
Excel:=UnAssigned;
Abort;
End;
Try
With DbGridEh.DataSource.DataSet Do
Begin
First;
I:=4; //行记数
While Not Eof Do
Begin
Y:=1;
For J:=0 To DbgridEh.Columns.Count-1 Do //列记数
begin
If DbGridEh.Columns[J].Visible Then
begin
If DbgridEh.Columns[J].PickList.Count>0 Then
Sheet.Cells[I,Y]:=DbGridEh.Columns[J].PickList.Strings[DbGridEh.Columns[J].KeyList.IndexOf(FieldByName(DbGridEh.Columns[J].FieldName).AsString)]
Else
If FieldByName(DbGridEh.Columns[J].FieldName).DataType=FtDateTime Then
Sheet.Cells[I,Y]:=FormatDateTime('yyyy.mm.dd',FieldByName(DbGridEh.Columns[J].FieldName).AsDateTime)
Else
Sheet.Cells[I,Y]:=FieldByName(DbGridEh.Columns[J].FieldName).AsString;
Y:=Y+1;
end;
end;
I:=I+1;
Next;
End;
End;
Finally
DbGridEh.FrozenCols:=X;
Excel.Visible:=True;
Excel.Quit;//控制Excel退出
Excel:=UnAssigned;
End;
在程序执行到( Excel.Quit;//控制Excel退出)时,Excel窗口便提示Excel
"的文本已经改变,是否保存?"如果不让这个提示出现,并且不关闭Excel对象
应该怎样办呢?请各位大虾指点?
var
Excel,Sheet:Variant;
I,J,X,Y:Integer;
begin
Try
Excel:=UnAssigned;
Excel:=CreateOleObject('Excel.Application');
Excel.Visible:=False;
Excel.WorkBooks.Add;
Except
Excel:=UnAssigned;
DispInfo('本机未安装EXCEL,本功能必须在安装有EXCEL的电脑上才能运行!',1);
Abort;
End;
if VarIsEmpty(Excel) then
begin
DispInfo('建立Excel对象不成功,请重试!',1);
Excel:=UnAssigned;
Abort;
end;
X:=DbGridEh.FrozenCols;
DbGridEh.FrozenCols:=0;//设置锁定列为0
J:=0;
For I:=0 To DbGridEh.Columns.Count-1 Do
Begin
If DbGridEh.Columns.Visible Then
J:=J+1;
End; //取得显示列总列数
I:=Round(J/2);
sheet:=Excel.worksheets[1];
Sheet.Name:=TForm(DbGridEh.Parent.Parent).caption;
Sheet.Cells[1,I]:=TForm(DbGridEh.Parent.Parent).caption;
sheet.cells[2,1]:='日期:'+FormatDateTime('yyyy"年"mm"月"dd"日"',Now);
Sheet.Cells[2,J-1]:='制表:'+UserCode; //以上为表头输出
J:=1;
For I:=0 To DbGridEh.Columns.Count-1 Do
If DbGridEh.Columns.Visible Then
begin
Sheet.Cells[3,J]:=DbGridEh.Columns.Title.Caption; //设置列标题
J:=J+1;
end;
If Not DbGridEh.DataSource.DataSet.Active Then
Begin
Excel.Quit;
Excel:=UnAssigned;
Abort;
End;
Try
With DbGridEh.DataSource.DataSet Do
Begin
First;
I:=4; //行记数
While Not Eof Do
Begin
Y:=1;
For J:=0 To DbgridEh.Columns.Count-1 Do //列记数
begin
If DbGridEh.Columns[J].Visible Then
begin
If DbgridEh.Columns[J].PickList.Count>0 Then
Sheet.Cells[I,Y]:=DbGridEh.Columns[J].PickList.Strings[DbGridEh.Columns[J].KeyList.IndexOf(FieldByName(DbGridEh.Columns[J].FieldName).AsString)]
Else
If FieldByName(DbGridEh.Columns[J].FieldName).DataType=FtDateTime Then
Sheet.Cells[I,Y]:=FormatDateTime('yyyy.mm.dd',FieldByName(DbGridEh.Columns[J].FieldName).AsDateTime)
Else
Sheet.Cells[I,Y]:=FieldByName(DbGridEh.Columns[J].FieldName).AsString;
Y:=Y+1;
end;
end;
I:=I+1;
Next;
End;
End;
Finally
DbGridEh.FrozenCols:=X;
Excel.Visible:=True;
Excel.Quit;//控制Excel退出
Excel:=UnAssigned;
End;
在程序执行到( Excel.Quit;//控制Excel退出)时,Excel窗口便提示Excel
"的文本已经改变,是否保存?"如果不让这个提示出现,并且不关闭Excel对象
应该怎样办呢?请各位大虾指点?