给你一个我曾经作过的生成Excel文件的函数
Function CreateExcelFile(MyTable1:TDataSet;MyTable2:TDataSet):string;
//生成Excel文件函数 Created By JDF on 2001-10-24
var
FileName:string;
eclApp,WorkBook:Variant;
i,j,m,n,ColCount1,RowCount1,ColCount2,RowCount2:integer;
SaveDialogExcel:TSaveDialog; //存储对话框
Present: TDateTime;
Year, Month, Day, Hour, Min, Sec, MSec: Word;begin
Present:= Now;
DecodeDate(Present, Year, Month, Day);
DecodeTime(Present, Hour, Min, Sec, MSec); SaveDialogExcel:=TSaveDialog.Create(nil);
if not DirectoryExists('ExcelFile') then
MkDir('ExcelFile');
Try
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
Except
ShowMessage('您的机器里没有安装Microsoft Excel!');
Exit;
End; //try...Except
if not MyTable1.Active then
MyTable1.Open;
if not MyTable2.Active then
MyTable2.Open;
ColCount1 := MyTable1.FieldCount; //取得列数
RowCount1 := MyTable1.RecordCount; //取得行数
ColCount2 := MyTable2.FieldCount; //取得列数
RowCount2 := MyTable2.RecordCount; //取得行数
Try
workBook:=eclApp.workBooks.Add;
i:=1;
j:=1;
for j := 1 to ColCount1 do
eclApp.Cells(1,j):= MyTable1.Fields[j-1].DisplayName; // MyDBGrid1.Columns[j-1].Title.Caption;
j:=1;
while not MyTable1.Eof do
begin
for j:=1 to ColCount1 do
eclApp.Cells(i+1,j) := MyTable1.Fields[j-1].AsString;
i:=i+1;
MyTable1.Next;
end;
m:=RowCount1+3;
for n := 1 to ColCount2 do
eclApp.Cells(m,n):=MyTable2.Fields[n-1].DisplayName;
while not MyTable2.Eof do
begin
for n:=1 to ColCount2 do
eclApp.Cells(m+1,n) :=MyTable2.Fields[n-1].AsString;
M:=m+1;
MyTable2.Next;
end;
FileName := 'SalesList_'+inttostr(Year)+inttostr(Month)+inttostr(Day)+inttostr(Hour)+inttostr(Min)+inttostr(Sec)+inttostr(MSec);
FileName := ExtractFilePath(Application.ExeName)+'ExcelFile\' + FileName + '.xls';
SaveDialogExcel.Title:='保存为文本文件';
SaveDialogExcel.Filter:='文本文件[*.xls]|*.xls'; //过滤条件
SaveDialogExcel.DefaultExt:='*.xls'; //后缀
SaveDialogExcel.FileName:=FileName; //缺省文件名
SaveDialogExcel.InitialDir:=GetCurrentDir; //缺省目录
if SaveDialogExcel.Execute then
begin
FileName:=SaveDialogExcel.FileName; //记录下文件名
WorkBook.Saveas(FileName);
end;
WorkBook.Close;
eclApp.Quit; //退出Excel Application
Except
ShowMessage('生成Excel文件失败!');
WorkBook.Close;
eclApp.Quit;
End;
End;
Function CreateExcelFile(MyTable1:TDataSet;MyTable2:TDataSet):string;
//生成Excel文件函数 Created By JDF on 2001-10-24
var
FileName:string;
eclApp,WorkBook:Variant;
i,j,m,n,ColCount1,RowCount1,ColCount2,RowCount2:integer;
SaveDialogExcel:TSaveDialog; //存储对话框
Present: TDateTime;
Year, Month, Day, Hour, Min, Sec, MSec: Word;begin
Present:= Now;
DecodeDate(Present, Year, Month, Day);
DecodeTime(Present, Hour, Min, Sec, MSec); SaveDialogExcel:=TSaveDialog.Create(nil);
if not DirectoryExists('ExcelFile') then
MkDir('ExcelFile');
Try
eclApp:=CreateOleObject('Excel.Application');
WorkBook:=CreateOleObject('Excel.Sheet');
Except
ShowMessage('您的机器里没有安装Microsoft Excel!');
Exit;
End; //try...Except
if not MyTable1.Active then
MyTable1.Open;
if not MyTable2.Active then
MyTable2.Open;
ColCount1 := MyTable1.FieldCount; //取得列数
RowCount1 := MyTable1.RecordCount; //取得行数
ColCount2 := MyTable2.FieldCount; //取得列数
RowCount2 := MyTable2.RecordCount; //取得行数
Try
workBook:=eclApp.workBooks.Add;
i:=1;
j:=1;
for j := 1 to ColCount1 do
eclApp.Cells(1,j):= MyTable1.Fields[j-1].DisplayName; // MyDBGrid1.Columns[j-1].Title.Caption;
j:=1;
while not MyTable1.Eof do
begin
for j:=1 to ColCount1 do
eclApp.Cells(i+1,j) := MyTable1.Fields[j-1].AsString;
i:=i+1;
MyTable1.Next;
end;
m:=RowCount1+3;
for n := 1 to ColCount2 do
eclApp.Cells(m,n):=MyTable2.Fields[n-1].DisplayName;
while not MyTable2.Eof do
begin
for n:=1 to ColCount2 do
eclApp.Cells(m+1,n) :=MyTable2.Fields[n-1].AsString;
M:=m+1;
MyTable2.Next;
end;
FileName := 'SalesList_'+inttostr(Year)+inttostr(Month)+inttostr(Day)+inttostr(Hour)+inttostr(Min)+inttostr(Sec)+inttostr(MSec);
FileName := ExtractFilePath(Application.ExeName)+'ExcelFile\' + FileName + '.xls';
SaveDialogExcel.Title:='保存为文本文件';
SaveDialogExcel.Filter:='文本文件[*.xls]|*.xls'; //过滤条件
SaveDialogExcel.DefaultExt:='*.xls'; //后缀
SaveDialogExcel.FileName:=FileName; //缺省文件名
SaveDialogExcel.InitialDir:=GetCurrentDir; //缺省目录
if SaveDialogExcel.Execute then
begin
FileName:=SaveDialogExcel.FileName; //记录下文件名
WorkBook.Saveas(FileName);
end;
WorkBook.Close;
eclApp.Quit; //退出Excel Application
Except
ShowMessage('生成Excel文件失败!');
WorkBook.Close;
eclApp.Quit;
End;
End;
我的痛苦就在这里!!!!有人在Delphi下作过这样的事情吗??Help!!!
小新他们哪里去了??
但是需要首先激活对象!!!!就这么简单。其中还有一些具体的细节问题,比如是否可以用ActiveWorkBook、ActiveWorkSheet等等。
谢谢楼上的兄弟,现在给分。