Q
QianQi
Unregistered / Unconfirmed
GUEST, unregistred user!
我用的是TopGrid控件组中的TtsDBGrid
以下代码能运行,但是在另存后的电子表格文件中原来tsDBGrid中的第一条记录在电子表格中
显示为空,而其余的记录都显示正常,我怎么查也查不出原因,只能向大家请教了![]
procedure TForm1.ToExcel(Grid:TtsDBGrid;Query:TQuery);
var
I,J,Row,SelField:Integer;
begin
if SaveDialog1.Execute then
begin
try
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
except
Application.MessageBox('无法启动Excel,可能没有安装Excel!','提醒',mb_Ok);
Abort;
end;
Screen.Cursor:=crHourGlass;
SelField:=0;
for I:=1 to Grid.Cols do
begin
if Grid.Col.Visible<>False then
begin
SelField:=SelField+1;
ExcelWorkSheet1.Cells.Item[3,SelField]:=Grid.Col.FieldName;
end;
end;
row:=4;
Query.First;
while not Query.Eof do
begin
J:=0;
for I:=1 to Grid.Cols do
begin
if Grid.Col.Visible then
begin
J:=J+1;
ExcelWorkSheet1.Cells.Item[row,J]:=Grid.Field.AsString;
end;
end;
Row:=Row+1;
Query.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName+'.xls');
ExcelWorkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Application.MessageBox('数据导出完毕','电子台帐',mb_Ok);
Screen.Cursor:=crDefault;
end;
end;
以下代码能运行,但是在另存后的电子表格文件中原来tsDBGrid中的第一条记录在电子表格中
显示为空,而其余的记录都显示正常,我怎么查也查不出原因,只能向大家请教了![]
procedure TForm1.ToExcel(Grid:TtsDBGrid;Query:TQuery);
var
I,J,Row,SelField:Integer;
begin
if SaveDialog1.Execute then
begin
try
ExcelApplication1.Connect;
ExcelApplication1.Workbooks.Add(Null,0);
ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
except
Application.MessageBox('无法启动Excel,可能没有安装Excel!','提醒',mb_Ok);
Abort;
end;
Screen.Cursor:=crHourGlass;
SelField:=0;
for I:=1 to Grid.Cols do
begin
if Grid.Col.Visible<>False then
begin
SelField:=SelField+1;
ExcelWorkSheet1.Cells.Item[3,SelField]:=Grid.Col.FieldName;
end;
end;
row:=4;
Query.First;
while not Query.Eof do
begin
J:=0;
for I:=1 to Grid.Cols do
begin
if Grid.Col.Visible then
begin
J:=J+1;
ExcelWorkSheet1.Cells.Item[row,J]:=Grid.Field.AsString;
end;
end;
Row:=Row+1;
Query.Next;
end;
ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName+'.xls');
ExcelWorkBook1.Close(false);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
Application.MessageBox('数据导出完毕','电子台帐',mb_Ok);
Screen.Cursor:=crDefault;
end;
end;