借花献佛了:
写数据库程序时,通常希望把DBGrid列表中的数据导入excel表格,以下通过在数据模块中写一个共用过程,在任何地方需要时进行调用即可:
const
{ XlWBATemplate }
xlWBATChart = -4109;
xlWBATExcel4IntlMacroSheet = 4;
xlWBATExcel4MacroSheet = 3;
xlWBATWorksheet = -4167;
procedure Tsysdb.SaveToExcel(Db_data:TDBGrid);
var
XlAPP:Variant;
excelcount:integer;
Sheet1:Variant;
i,j:integer;
begin
if not Db_data.DataSource.DataSet.Active then exit;
if Db_data.DataSource.DataSet.RecordCount<1 then exit;
//创建excel对象
try
XlApp:=createoleobject('Excel.Application');
XLApp.Visible:=false;
excelcount:=XLApp.Workbooks.count;
XLApp.Workbooks.Add(xlWBatWorkSheet);
Sheet1 := XLApp.Workbooks[1].WorkSheets['sheet1'];
except
showmessage('你的电脑没出息有安装excel程序,无法完成此功能!');
exit;
end;
//setfocus;处理标题
for j:=0 to Db_data.FieldCount-1 do
begin
sheet1.cells[1,j+1]:=Db_data.Columns[j].Title.Caption;
end; //处理记录
Db_data.DataSource.DataSet.First;
i:=2;
while not Db_data.DataSource.DataSet.Eof do
begin
//处理一行
for j:=0 to Db_data.FieldCount-1 do
begin
if Db_data.Fields[j]<>nil then
Sheet1.cells[i,j+1]:=trim(Db_data.Fields[j].asstring)
else
Sheet1.cells[i,j+1]:='';
end;
i:=i+1;
Db_data.DataSource.DataSet.Next;
end;
XLApp.Visible:=true;
end;