曾经用过对付excel的一段程序
//将数据存储到excel文件中
procedure TForm1.excelsave(dir:string);
begin
if Length(dir)<>3 then
dir:=dir+'/';
e.Connect;
e.Visible[0]:=False;
wenjian:=FormatDateTime('yyyy-mm-dd',Now);
shijian:=FormatDateTime('hh:mm:ss',Now);
if FileExists(dir+wenjian+'.xls')=True then
begin
//文件存在时的处理
DeleteFile( ExtractFilePath(Application.ExeName)+ 'temp.xls');
e.Workbooks.Open(dir+wenjian+'.xls',Null,Null,Null,Null,Null,Null,
Null,Null,Null,Null,Null,Null,0);
eb.ConnectTo(e.Workbooks[1]);
es.ConnectTo(eb.Worksheets[1] as _worksheet);
//有文件时处理
dispose1;
e.ActiveWorkbook.SaveAs(ExtractFilePath(Application.ExeName)+ 'temp.xls',
null,null,null,0,0,0,0,0,0,0,0);
DeleteFile(dir+wenjian+'.xls');
e.Workbooks.Open(ExtractFilePath(Application.ExeName)+ 'temp.xls',Null,
Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,0);
// e.Worksheets.Application.Columns.ColumnWidth:=16;
e.ActiveWorkbook.SaveAs(dir+wenjian+'.xls',Null,Null,Null,0,0,0,0,
0,0,0,0);
end
else
begin
//文件不存在时候的处理
e.Workbooks.Open(ExtractFilePath(Application.ExeName)+ 'temp.xls',Null,
Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,Null,0);
eb.ConnectTo(e.Workbooks[1]);
es.ConnectTo(eb.Worksheets[1] as _worksheet);
//无文件时处理
dispose2;
// e.Worksheets.Application.Columns.ColumnWidth:=16;
e.ActiveWorkbook.SaveAs(dir+wenjian+'.xls',Null,Null,Null,0,0,0,0,
0,0,0,0);
end;
e.Workbooks.Close(1);
e.Disconnect;
end;