我的打开EXCEL文件代码是这样的,可是EXCELAPP.QUIT是关不了EXCEL.EXE的
procedure TForm1.EX1Click(Sender: TObject);
var filename,path:string;
Excelapp:variant;
begin
Excelapp:=CreateOleObject('Excel.Application');
path:=ExtractFilePath(application.ExeName);
fileName:='';
opendialog1.FileName:='';
opendialog1.Execute;
if opendialog1.FileName='' then Exit;
filename:=opendialog1.FileName;Excelapp.workBooks.Open(filename);
adoConnection1.Execute('delete from table1');
adoConnection1.Execute('INSERT INTO table1(姓名,考勤号码,日期, 对应时段,上班时间,下班时间,签到时间,签退时间)' + 'SELECT 姓名,考勤号码,日期,对应时段, 上班时间,下班时间,签到时间,签退时间' +
' FROM [excel 8.0;database=' + fileName + '].['+GetName(FileName)+'$]');//Excelapp.Workbooks.Close(0);
Excelapp.quit;
Excelapp := Unassigned;{
adoconnection1.Connected:=false;ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+path+'\attdb.mdb;'+'Persist Security Info=False';
ADOConnection1.Execute('select * into table1 FROM [excel 8.0;database=c:\ji.xls].ji');
adoconnection1.Connected:=true;
}
with adoQuery1 do
begin
close();
sql.clear;
sql.Text:='select 姓名,考勤号码, 对应时段,上班时间,下班时间,签到时间,签退时间,每天工作时间 from table1';
open;
CountR.Caption:=intToStr(adoQuery1.RecordCount);
end;
procedure TForm1.EX1Click(Sender: TObject);
var filename,path:string;
Excelapp:variant;
begin
Excelapp:=CreateOleObject('Excel.Application');
path:=ExtractFilePath(application.ExeName);
fileName:='';
opendialog1.FileName:='';
opendialog1.Execute;
if opendialog1.FileName='' then Exit;
filename:=opendialog1.FileName;Excelapp.workBooks.Open(filename);
adoConnection1.Execute('delete from table1');
adoConnection1.Execute('INSERT INTO table1(姓名,考勤号码,日期, 对应时段,上班时间,下班时间,签到时间,签退时间)' + 'SELECT 姓名,考勤号码,日期,对应时段, 上班时间,下班时间,签到时间,签退时间' +
' FROM [excel 8.0;database=' + fileName + '].['+GetName(FileName)+'$]');//Excelapp.Workbooks.Close(0);
Excelapp.quit;
Excelapp := Unassigned;{
adoconnection1.Connected:=false;ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+path+'\attdb.mdb;'+'Persist Security Info=False';
ADOConnection1.Execute('select * into table1 FROM [excel 8.0;database=c:\ji.xls].ji');
adoconnection1.Connected:=true;
}
with adoQuery1 do
begin
close();
sql.clear;
sql.Text:='select 姓名,考勤号码, 对应时段,上班时间,下班时间,签到时间,签退时间,每天工作时间 from table1';
open;
CountR.Caption:=intToStr(adoQuery1.RecordCount);
end;
不行还是要到主程序结束时才会关的
下面是我用来关闭Word代码,改成Excel即可。const //Global Constants
WORDNAME = 'Microsoft Word';
...
hwnd: THandle;
...
//找到Word窗体,发送WM_CLOSE消息使其关闭
hwnd := FindWindow(nil, WORDNAME);
SendMessage(hwnd, WM_CLOSE, 0, 0);
我在uses下面写
const EXCELName='Microsoft Excel'
在Form 的 PUBLIC里写
hwnd:THandle;
在程序要结束的地方写:
hwnd := FindWindow(nil, WORDNAME);
SendMessage(hwnd, WM_CLOSE, 0, 0);
发觉没有什么改变.
WORDNAME = 'Microsoft Word';...function TfrmMain.ExportToDoc(const whichTemplate: Integer): TErrno;
var
V: Variant; //代表Word的变体类型变量
Template, NewTemplate, DocumentType, Visible: OleVariant;
itemIndex: OleVariant;
fileName: Olevariant;
NoPrompt, OriginalFormat: OleVariant;
RouteDocument, SaveChanges: OleVariant;
//paraCount: Integer;
hwnd: THandle;
What, Name: OleVariant;
strBook: String;
begin
Result := NO_ERROR;
//导出处理签到Word
fileName := strEXEPath + 'doc\信访处理签' + Trim(lbSN.Caption) + '.doc';
//MessageDlg(fileName, mtError, [mbOK], 0); //for test //如果该记录的对应Word文档已经存在则提示是否覆盖
if FileExists(fileName) = true then
begin
Beep;
if MessageDlg('信访处理签管理系统' + #13 + #13
+ '当前信访处理签已经存在,是否覆盖?'
, mtConfirmation , mbOKCancel, 0) = mrCancel then
Abort;
end;
try //测试当前是否运行了Word 2000
V := GetActiveOleObject('Word.Application');
except
V := CreateOleObject('Word.Basic'); //未运行则运行之
end;
try
WordApp.Connect; //连接到Word 2000
except
Beep;
MessageDlg('不能生成文档,请确认是否安装了Word 2000!', mtError, [mbOK], 0);
Abort;
end;
WordApp.Visible := False; //不显示Word 2000
//给调用Add函数使用的实参赋值
if whichTemplate = 1 then
//使用安装路径下的“信访处理签1.dot”模板
Template := strEXEPath + 'templates\信访处理签1.dot'
else
Template := strEXEPath + 'templates\信访处理签2.dot';
NewTemplate := False;
DocumentType := wdNewBlankDocument;
Visible := true;
//调用Add函数
WordApp.Documents.Add(Template, NewTemplate, DocumentType, Visible);
//连接到新建的文档
itemIndex := 1;
WordDoc.ConnectTo(WordApp.Documents.Item(itemIndex));
//文档另存为
WordDoc.SaveAs(fileName);
//paraCount := WordDoc.Paragraphs.Count; //求得模板中的段落总数
//MessageDlg(IntToStr(paraCount), mtInformation, [mbOK], 0); //for test
//向Word文档中写入内容 ... //保存文档
NoPrompt := false;
OriginalFormat := wdOriginalDocumentFormat;
WordApp.Documents.Save(NoPrompt, OriginalFormat); //关闭文档
SaveChanges := wdSaveChanges;
OriginalFormat := wdOriginalDocumentFormat;
RouteDocument := false;
WordApp.Documents.Close(SaveChanges, OriginalFormat, RouteDocument);
//断开和Word 2000的连接
WordApp.Disconnect; //找到Word窗体,发送WM_CLOSE消息使其关闭
hwnd := FindWindow(nil, WORDNAME);
SendMessage(hwnd, WM_CLOSE, 0, 0);
end;