发错了
var
MsExcel: oleVariant;
ExcelSheet1: Variant;
i: integer;
row: integer; //Excel行
bkm: tbookmark;
isexport: boolean;
begin
if not DataGrid.DataSource.DataSet.Active then
showmessage('没有数据可存')
else
begin
try
MsExcel := CreateOleObject('Excel.Application');
except
MessageBox(Handle, '初始化Excel错误。',
'error', MB_OK + MB_ICONERROR);
exit;
end;
Screen.Cursor := crHourGlass;
MsExcel.Visible := False;
MsExcel.WorkBooks.Add;
ExcelSheet1 := MsExcel.WorkBooks[1].WorkSheets[1];
isexport := true;
bkm := DataGrid.DataSource.DataSet.getbookmark;
datagrid.DataSource.DataSet.DisableControls;
for i := 0 to datagrid.FieldCount - 1 do //.dbquery.Fields.Count - 1
ExcelSheet1.Cells.Item[1, i + 1] := datagrid.Columns.FieldName; //.dbquery.Fields.FieldName; //标题
DataGrid.DataSource.DataSet.first;
row := 2;
while not DataGrid.DataSource.DataSet.Eof do
begin
for i := 0 to Datagrid.FieldCount - 1 do //.dbquery.Fields.Count - 1 do
begin
if not isexport then
begin
MsExcel.DiaplayAlert := false;
MsExcel.quit;
exit;
end;
application.ProcessMessages;
ExcelSheet1.Cells.Item[row, i + 1] := DataGrid.Columns.Items.Field.AsString; //frmDAtamodule.dbquery.Fields.AsString;
end;
row := row + 1;
DataGrid.DataSource.DataSet.Next;
end;
try
DataGrid.DataSource.DataSet.GotoBookmark(bkm);
finally
DataGrid.DataSource.DataSet.freebookmark(bkm);
end;
MsExcel.QUIT;
datagrid.DataSource.DataSet.EnableControls;
Screen.Cursor := crDefault;
end;
end;