Delphi開關Excel檔後,Office Excel的程式就不能用了? |
答題得分者是:cmf
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
我知道為什麼我把Excel檔讀進Query後,
在用Query寫回Excel檔,
但是直接用Excel 開檔則無法開啟.......
只會有Excel的主程式,
中間的book和sheet畫面仍然是桌面的畫面~~~也就是只有Excel的框框 以下是我的程式:
讀取Excel的資料..
程式是cmf大大教的,來自
http://delphi.ktop.com.tw/topic.php?TOPIC_ID=24548 //=== Program === procedure TForm1.Button1Click(Sender: TObject);
var
ExcelWorksheet:array[0..10] of TExcelWorksheet;
i : integer;
temp : string;
begin
Excelapplication1.Connect();
ExcelApplication1.Visible[0] := false;
ExcelApplication1.Caption := 'New Excel';
ExcelWorkbook1.ConnectTo(
ExcelApplication1.Workbooks.Open ('c:\excel\book1.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelApplication1.ScreenUpdating[0] := True; //wsSheet := ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet; for i:=0 to ExcelWorkbook1.WorkSheets.Count-1 do
begin
ExcelWorksheet[i]:=TExcelWorksheet.Create(self);
ExcelWorksheet[i].ConnectTo(ExcelWorkbook1.Worksheets[i 1] as _Worksheet);
end; ExcelWorksheet[0].select;
i := 1; //因為寫到Query的程式太長了,所以改用show表示取得Excel上面的值
showmessage(ExcelWorksheet[0].Range['A1','A1'].Value);
//做離開Excel的動作,我想問題就出在這
//可能是沒有中斷的乾淨,倒置Excel無法開啟
//請問這裡的程式有問題嗎?
for i:=0 to ExcelWorkbook1.WorkSheets.Count-1 do
begin
ExcelWorksheet[i].Disconnect();
end;
ExcelWorkbook1.Disconnect();
ExcelApplication1.Disconnect();
ExcelApplication1.Quit;
end; 以下是我寫回同一個Excel檔的程式
procedure TForm1.Button2Click(Sender: TObject);
var
wsSheet: _WorkSheet;
i : integer;
begin
Excelapplication1.Connect();
ExcelApplication1.Visible[0] := true;
ExcelApplication1.Caption := 'New Excel'; ExcelWorkbook1.ConnectTo(
ExcelApplication1.Workbooks.Open ('c:\excel\book1.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelApplication1.ScreenUpdating[0] := True; wsSheet := ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
ExcelWorkSheet1.ConnectTo(wsSheet);
ExcelWorksheet1.Name := 'new sheet' inttostr(ExcelWorkbook1.WorkSheets.Count); //以下是寫回Excel的值
//一樣用一行取代
ExcelWorksheet1.Range['A1','A1'].Value := 'Wa si VAKA';
end; //=== Program End === 請問哪裡出問題,有時開Excel可以,有時又不行,
最直接的方法就是重開機後,馬上執行,Excel就開不起來了。 ----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
|
william
版主 發表:66 回覆:2535 積分:3048 註冊:2002-07-11 發送簡訊給我 |
Don't know whether it can work for your case, just want to share my experience...
If you create Excel application in your program, better use your program to quit excel and destroy the according objects. However, when you close Excel (e.g. click on the x on the Excel window) after it is created by your program, running/creating Excel again will result in the case you described -- only the window title of Excel is visible while its content is kinnda of 'transparent'. In this case, you may try to kill Excel using task manager and quit your program; Excel should be usable then...
|
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
我發現我的程式如果仍然打開的話,
Excel就不能用了。 我在工作管理員的處理程序有看到Excel.Exe
把他刪除後就可以了~~~
不過不太可能叫User這樣使用.... 我在試試有沒有辦法把Excel的程序放釋放掉.... william兄,我在試試你說的內容。
(我認識英文,但英文說不認識我~~~唉~~~) ----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
l630521
資深會員 發表:15 回覆:349 積分:372 註冊:2003-02-17 發送簡訊給我 |
|
cmf
尊榮會員 發表:84 回覆:918 積分:1032 註冊:2002-06-26 發送簡訊給我 |
"不過不太可能叫User這樣使用...." 這是當然爾 請修改 離開Excel的動作 ExcelApplication1.AutoQuit:=TRUE; ExcelApplication1.Quit; for i:=0 to ExcelWorkbook1.WorkSheets.Count-1 do
begin
try
ExcelWorksheet[i].Disconnect();
except end; end;
try
ExcelWorkbook1.Disconnect();
except
end;
try
ExcelApplication1.Disconnect();
except end;
------
︿︿ |
skurama
中階會員 發表:88 回覆:127 積分:73 註冊:2002-07-22 發送簡訊給我 |
現在載入資料到Query已經沒有問題了,
我去程式改成如下,
是用動態產生的ExcelApplication1和workbook1來做
最後在Free掉,大概如下: procedure TForm1.Button1Click(Sender: TObject);
var
ExcelApplication1 : TExcelApplication;
ExcelWorkbook1 : TExcelWorkbook;
ExcelWorksheet:array[0..10] of TExcelWorksheet;
i : integer;
temp : string;
begin
ExcelApplication1 := TExcelApplication.create(self);
ExcelWorkbook1 := TExcelWorkbook.create(self); Excelapplication1.Connect();
ExcelApplication1.Visible[0] := false;
ExcelApplication1.Caption := 'New Excel';
ExcelWorkbook1.ConnectTo(
ExcelApplication1.Workbooks.Open ('c:\excel\book1.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelApplication1.ScreenUpdating[0] := True; //wsSheet := ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet; for i:=0 to ExcelWorkbook1.WorkSheets.Count-1 do
begin
ExcelWorksheet[i]:=TExcelWorksheet.Create(self);
ExcelWorksheet[i].ConnectTo(ExcelWorkbook1.Worksheets[i 1] as _Worksheet);
end; ExcelWorksheet[0].select; showmessage(ExcelWorksheet[0].Range['A1','A1'].Value); for i:=0 to ExcelWorkbook1.WorkSheets.Count-1 do
begin
ExcelWorksheet[i].Disconnect();
end;
ExcelWorkbook1.Disconnect();
ExcelWorkbook1.free;
ExcelApplication1.Disconnect();
ExcelApplication1.Quit;
ExcelApplication1.free;
end; 寫到Excel檔則改成 procedure TForm1.Button2Click(Sender: TObject);
var
wsSheet: _WorkSheet;
i : integer;
begin
Excelapplication1.Connect();
ExcelApplication1.Visible[0] := true;
ExcelApplication1.Caption := 'New Excel'; ExcelWorkbook1.ConnectTo(
ExcelApplication1.Workbooks.Open ('c:\excel\book1.xls',
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,
EmptyParam,EmptyParam,EmptyParam,EmptyParam,0));
ExcelApplication1.ScreenUpdating[0] := True; wsSheet := ExcelWorkbook1.WorkSheets.Add(EmptyParam,EmptyParam,EmptyParam,EmptyParam,0) as _WorkSheet;
ExcelWorkSheet1.ConnectTo(wsSheet);
ExcelWorksheet1.Name := 'new sheet' inttostr(ExcelWorkbook1.WorkSheets.Count); //以下是寫回Excel的值
//一樣用一行取代
ExcelWorksheet1.Range['A1','A1'].Value := 'Wa si VAKA'; ExcelWorksheet1.free;
ExcelWorkbook1.free;
ExcelApplication1.free;
end; 已經可以了~~~~
謝謝各位大大
cmf兄,再次感謝你~~~thx ----------------
初出芧房程設師,
左鍵右鼠寫程式,
日扣夜寫眼框溼,
望能早成系分師。
----------------
------
---------------- 初出芧房程設師, 左鍵右鼠寫程式, 日扣夜寫眼框溼, 望能早成系分師。 ---------------- |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |