dbgrid中数据导出到excel,ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,

  • 主题发起人 主题发起人 blackczh
  • 开始时间 开始时间
B

blackczh

Unregistered / Unconfirmed
GUEST, unregistred user!
dbgrid中数据导出到excel,ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i]有问题 ( 积分: 50 )<br />procedure TForm1.Button1Click(Sender: TObject);
var Temp_Worksheet: _WorkSheet;
i:integer;
begin
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
Temp_Worksheet:=ExcelWorkbook1.
WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
for i:=0 to dbgrid1.FieldCount-1 do
begin
ExcelWOrkSheet1.Cells.Item[1,i+1]:=dbgrid1.Fields.FieldName;
end;
dbgrid1.DataSource.DataSet.First;
while not dbgrid1.DataSource.DataSet.Eof do
begin
for i:=0 to dbgrid1.DataSource.DataSet.FieldCount -1 do
begin
ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i]:=dbgrid1.DataSource.DataSet.Fields.Value;
dbgrid1.DataSource.DataSet.Next;
end;
end;
end;
运行到ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i]:=dbgrid1.DataSource.DataSet.Fields.Value;这句就报错:ole error 800A03EC.请问是什么意思
 
procedure TForm1.Button1Click(Sender: TObject);
var Temp_Worksheet: _WorkSheet;
i:integer;
begin
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=True;
ExcelApplication1.Caption := '应用程序调用 Microsoft Excel';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
Temp_Worksheet:=ExcelWorkbook1.
WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
ExcelWorkSheet1.ConnectTo(Temp_WorkSheet);
for i:=0 to dbgrid1.FieldCount-1 do
begin
ExcelWOrkSheet1.Cells.Item[1,i+1]:=dbgrid1.Fields.FieldName;
end;
dbgrid1.DataSource.DataSet.First;
while not dbgrid1.DataSource.DataSet.Eof do
begin
for i:=0 to dbgrid1.DataSource.DataSet.FieldCount -1 do
begin
ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i]:=dbgrid1.DataSource.DataSet.Fields.Value;
dbgrid1.DataSource.DataSet.Next;
end;
end;
end;
运行到ExcelWorksheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i]:=dbgrid1.DataSource.DataSet.Fields.Value;这句就报错:ole error 800A03EC.请问是什么意思
 
问题出在 ExcelWorkSheet1.Cells.Item[dbgrid1.DataSource.DataSet.RecNo+1,i] 的赋值语句上。ExcelWorkSheet1.Cells.Item中的行被设置为随 DataSet.RecNo+1动态改变的时候,越界出错!
 
那应该怎么改呢
 
后退
顶部