{具體幫助可以從excel得到
下面給出一例,excelfilename代表要儲存的檔名
thedataSet代表傳入的資料集
reportTitle代表報表頭
titlefont,detailfont代表字型
reportheader代表報表頭
vprogressbar代表進度條
}
procedure TfrCom.ExcelHandle
(
ExcelFileName:string;
TheDataSet :TDataSet;
ReportTitle :string;
ReportHeader :string
);
var
vTheExcelFileName, vTitle : string;
vTheDataSet : TDataSet;
i : Integer;
begin
//檔案預設儲存在當前路徑,檔名加匯出日期
vTheExcelFileName := ExtractFilePath(Application.ExeName) + ReportTitle + ".xls";
vTheDataSet := TDataSet(TheDataSet);
//匯出Excel
With TDS2Excel.Create(TDataSet(vTheDataSet)) do
Try
Save2File(True,vTheExcelFileName,ReportTitle,vTitle);
finally
Free;
end;
procedure TDS2Excel.Save2File(WillWriteHead: Boolean; ExcelFileName,
ReportTitle, ReportHeader : WideString);
SaveDialog1 : TSaveDialog;
ExcelApp : variant;
CmdLine : string;
FReportTitle := ReportTitle; //標題賦值
FReportHeader := ReportHeader; //表頭賦值
SaveDialog1 := TSaveDialog.Create(nil); //儲存對話方塊
SaveDialog1.Filter := "Excel文件|*.xls";
SaveDialog1.InitialDir := ExcelFileName;
SaveDialog1.FileName := ExcelFileName;
if not SaveDialog1.Execute then exit;
Save2File(SaveDialog1.FileName, WillWriteHead);
if Application.MessageBox(
PChar("檔案 "+SaveDialog1.FileName+" 匯出成功!"+#13#13+" 現在需要開啟它嗎? "),
"詢問:",
MB_YesNo+MB_IconQuestion) = mrYes then Begin
try
ExcelApp := GetActiveOLEObject("Excel.Application");
except
ExcelApp := CreateOLEObject("Excel.Application");
Application.MessageBox(" 未安裝 MircoSoft Excel!","提示",MB_OK+MB_ICONINFORMATION);
exit;
CmdLine:=ExcelApp.path+"\Excel ""+SaveDialog1.FileName+""";
WinExec(pchar(CmdLine),SW_MAXIMIZE);
//WinExec(pchar(String(ExcelFileName+".xls")),SW_MAXIMIZE);
//ShellExecute(0,Nil,PChar(String(ExcelFileName+".xls")),"","",SW_SHOW)
Finally
SaveDialog1.Free;
我從程式碼中節選的一些,你自己看看,我也不知道少沒少
{具體幫助可以從excel得到
下面給出一例,excelfilename代表要儲存的檔名
thedataSet代表傳入的資料集
reportTitle代表報表頭
titlefont,detailfont代表字型
reportheader代表報表頭
vprogressbar代表進度條
}
procedure TfrCom.ExcelHandle
(
ExcelFileName:string;
TheDataSet :TDataSet;
ReportTitle :string;
ReportHeader :string
);
var
vTheExcelFileName, vTitle : string;
vTheDataSet : TDataSet;
i : Integer;
begin
//檔案預設儲存在當前路徑,檔名加匯出日期
vTheExcelFileName := ExtractFilePath(Application.ExeName) + ReportTitle + ".xls";
vTheDataSet := TDataSet(TheDataSet);
//匯出Excel
With TDS2Excel.Create(TDataSet(vTheDataSet)) do
Try
Save2File(True,vTheExcelFileName,ReportTitle,vTitle);
finally
Free;
end;
end;
procedure TDS2Excel.Save2File(WillWriteHead: Boolean; ExcelFileName,
ReportTitle, ReportHeader : WideString);
var
SaveDialog1 : TSaveDialog;
ExcelApp : variant;
CmdLine : string;
begin
FReportTitle := ReportTitle; //標題賦值
FReportHeader := ReportHeader; //表頭賦值
SaveDialog1 := TSaveDialog.Create(nil); //儲存對話方塊
Try
SaveDialog1.Filter := "Excel文件|*.xls";
SaveDialog1.InitialDir := ExcelFileName;
SaveDialog1.FileName := ExcelFileName;
if not SaveDialog1.Execute then exit;
Save2File(SaveDialog1.FileName, WillWriteHead);
if Application.MessageBox(
PChar("檔案 "+SaveDialog1.FileName+" 匯出成功!"+#13#13+" 現在需要開啟它嗎? "),
"詢問:",
MB_YesNo+MB_IconQuestion) = mrYes then Begin
try
try
ExcelApp := GetActiveOLEObject("Excel.Application");
except
ExcelApp := CreateOLEObject("Excel.Application");
end;
except
Application.MessageBox(" 未安裝 MircoSoft Excel!","提示",MB_OK+MB_ICONINFORMATION);
exit;
end;
CmdLine:=ExcelApp.path+"\Excel ""+SaveDialog1.FileName+""";
WinExec(pchar(CmdLine),SW_MAXIMIZE);
//WinExec(pchar(String(ExcelFileName+".xls")),SW_MAXIMIZE);
//ShellExecute(0,Nil,PChar(String(ExcelFileName+".xls")),"","",SW_SHOW)
end;
Finally
SaveDialog1.Free;
end;
end;
我從程式碼中節選的一些,你自己看看,我也不知道少沒少