不好意思,问题已经解决了,多谢bluedog的启发,100分给你了
我采用的方法是:
1.保存wwDbGrid内容到临时文件:
function CreateTmpFile(var TmpFileName:String):boolean;
var ptmpPathName,ptmpFileName
char;
begin
result := false;
try
GetMem(ptmpPathName,256);
GetMem(ptmpFileName,256);
GetTempPath(40,ptmpPathName);
GetTempFileName(ptmpPathName,pchar('cashcheck '),0,ptmpFileName);
TmpFileName := StrPas(ptmpFileName);
result := true;
finally
dispose(ptmpPathName);
dispose(ptmpFileName);
end;
end;
2.简单封装了一个excel对象:
TExcelObj=class
private
_Application:Variant;
WorkBooks:Variant;
public
constructor Create(FileName:String);
procedure AppShow;
procedure OpenFile(FileName:String);
procedure Preview;
procedure Print;
end;
implementation
constructor TExcelObj.Create(FileName:String);
begin
try
_Application := CreateOleObject( 'Excel.Application' );
WorkBooks := _Application.WorkBooks.Open(FileName);
except on e:exception do
EMsgDlg( '调用Excel失败.'+#13+
'错误信息:'+e.Message+#13+
'处理建议:请确认Ms Excel在本机上已经正确安装.');
end;
end;
procedure TExcelObj.OpenFile(FileName: String);
begin
try
WorkBooks:=_Application.WorkBooks.Open(FileName);
except on e:exception do
EMsgDlg( '调用Excel失败.'+#13+
'错误信息:文件'+FileName+'不存在,'+e.Message+#13+
'处理建议:.');
end;
end;
procedure TExcelObj.Preview;
begin
if not VarIsNull(WorkBooks) then
try
_Application.ActiveSheet.PrintPreview;
except on e:exception do
EMsgDlg( '调用Excel失败.'+#13+
'错误信息:打印文件出错,'+e.Message+#13+
'处理建议:检查打印机是否正确安装');
end;
end;
procedure TExcelObj.Print;
begin
if not VarIsNull(WorkBooks) then
try
_Application.ActiveSheet.PrintOut;
except on e:exception do
EMsgDlg( '调用Excel失败.'+#13+
'错误信息:打印文件出错,'+e.Message+#13+
'处理建议:.检查打印机是否正确安装');
end;
end;
3.调用预览功能
var excelObj:TExcelObj;
begin
try
excelObj:= TExcelObj.Create(FileName);
excelObj.appshow;
excelObj.Preview;
finally
excelObj.Free;
end;