在delphi7中,如何将sql查询结果输出为EXCEL格式?(100分)

  • 主题发起人 主题发起人 yangyj
  • 开始时间 开始时间
Y

yangyj

Unregistered / Unconfirmed
GUEST, unregistred user!
在delphi7中,如何将sql查询结果输出为EXCEL格式?
 
用DBGridEH控件

SaveDBGridEhToExportFile(TDBGridEhExportAsXLS, DBGridEh1, 文件名, True);
 
也可以直接蹈入到EXCEL中
不过通用性不好,只能针对某个表,把数据类型写死了
 
帮顶...................
 
用DBGridEh控件
SaveDBGridEhToExportFile(TDBGridEhExpoirtClassAsXLS,DBGridEh,FileName,True)
 
procedure TForm1.Button1Click(Sender: TObject);
begin
SaveDBGridEhToExportFile(TDBGridEhExportAsXLS, DBGridEh1, 'c:/FileName.xls', True);
end;
---------出错信息,请高手指导
[Error] Unit1.pas(33): Undeclared identifier: 'SaveDBGridEhToExportFile'
[Error] Unit1.pas(33): Undeclared identifier: 'TDBGridEhExportAsXLS'
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
 
自己循环写不行啊``还可以写到多个SHEET里`
 
参考 http://www.delphibbs.com/delphibbs/dispq.asp?lid=737517
Delphi与Word之间的融合技术 Excel也有的
 
var
filename: string;
ExcelApp: Variant;
begin
try
ExcelApp := CreateOleObject('Excel.Application');
except
MessageBox(Handle, '初始化Excel错误。',
'error', MB_OK + MB_ICONERROR);
exit;
end;
pathOpendialog.Filter := 'excel file (*.xls)|*.xls';
pathopendialog.InitialDir := ExtractFiledir(Application.ExeName) + '/data';
if pathOpenDialog.Execute then
begin
filename := pathopenDialog.FileName;
ExcelApp.Visible := True;
ExcelApp.WorkBooks.add;
try
ExcelApp.WorkBooks.Open(filename);
except
ExcelApp.workbooks.close;

end;
end;
excelapp.quit;

end;
 
发错了
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;
 
使用现成的函数或控件
 
后退
顶部