Excel richtig beenden?

teppi

Erfahrenes Mitglied
Hallo!

Ich hab nun schon alle vorgeschlagenen Methoden durchprobiert, die ich so im Netz finden konnte. Aber ich kriege es einfach nicht hin, zur Laufzeit meines Programms den Excelprozess korrekt zu beenden. Ich erstelle eine Excelobjekt, ein Workbookobjekt und ein Worksheetobjekt. Ich setze alles null und benutze auch diese Marshal.ReleaseComObject() .. Aber es hilfts nichts. Ich bin echt am Ende mit meinem Latein ... Hilfe !

Vielen Dank im voraus,
Gruß Stefan :)

PHP:
using System;
using System.Data;
using Excel;
using System.Runtime.InteropServices;

namespace WindowsApplication10
{
	/// <summary>
	/// Parserklasse um Daten aus einem vorher ausgewählten Exceldokument zu extrahieren
	/// </summary>
	public class ExcelWriter
	{
		private Excel.Workbook wb = null;
		private Excel.Worksheet ws = null;
		private System.Data.DataTable daten = null;
		private string fileName = "";
		private Excel.Application app = null;

		public ExcelWriter(string inFileName, System.Data.DataTable inDaten)
		{
			//
			// TODO: Add constructor logic here
			//
			this.fileName = inFileName;

            this.daten = inDaten;
			
			app = new Excel.ApplicationClass();
			
			this.wb = app.Workbooks.Add(Type.Missing);

			this.wb.Application.DisplayAlerts = false;

			this.createXSL();
		}
        
		private void createXSL(){
			
			// neues Datenblatt mit Ordnernamen anlegen
			ws = (Excel.Worksheet)wb.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
											
			// Neuen Kopf erstellen	
			ws.Cells[1,1] = "Ordnerstruktur";
			ws.Cells[1,2] = "Req-ID";
			ws.Cells[1,3] = "Akzeptanztest";
			ws.Cells[1,4] = "ARD";
			ws.Cells[1,5] = "ARD Version";
			ws.Cells[1,6] = "Links-FDB";
			ws.Cells[1,7] = "Feature Complete";
			ws.Cells[1,8] = "PQ-EU";
			ws.Cells[1,9] = "TO-EU";
			ws.Cells[1,10] = "PQ-NAR";
			ws.Cells[1,11] = "TO-NAR";
			ws.Cells[1,12] = "PQ-JPN";
			ws.Cells[1,13] = "TO-JPN";
			ws.Cells[1,14] = "PQ-CHN";
			ws.Cells[1,15] = "TO-CHN";
			ws.Cells[1,16] = "Description";
			ws.Cells[1,17] = "Test Name";
			ws.Cells[1,18] = "Designer";
			ws.Cells[1,19] = "Creation Date";
			ws.Cells[1,20] = "Status";
			ws.Cells[1,21] = "Stepname";
			ws.Cells[1,22] = "Description";

			//Alle Zeilen der DataTable durchlaufen und Exceldatei füllen

			int index = 2;

			foreach (DataRow row in daten.Rows)
			{
				ws.Cells[index,2]= row[1];
				ws.Cells[index,3] = row[2];
				ws.Cells[index,4] = row[3];
				ws.Cells[index,5] = row[4];
				ws.Cells[index,6] = row[5];
				ws.Cells[index,7] = row[6];
				ws.Cells[index,8] = row[7];
				ws.Cells[index,9] = row[8];
				ws.Cells[index,10] = row[9];
				ws.Cells[index,11] = row[10];
				ws.Cells[index,12] = row[11];
				ws.Cells[index,13] = row[12];
				ws.Cells[index,14] = row[13];
				ws.Cells[index,15] = row[14];
				ws.Cells[index,16] = row[15];
				ws.Cells[index,17] = row[16];
				ws.Cells[index,18] = row[17];
				ws.Cells[index,19] = row[18];
				ws.Cells[index,20] = row[19];
				ws.Cells[index,21] = row[20];
				ws.Cells[index,22] = row[21];

				index++;
			}

			wb.SaveAs("c:\\bla.xls", 
				Excel.XlFileFormat.xlExcel7, 
				Type.Missing, Type.Missing, Type.Missing, Type.Missing, 
				Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, 
				Type.Missing, Type.Missing, Type.Missing);
	

			wb.Close(Type.Missing,Type.Missing,Type.Missing);

			wb = null;
			ws = null;
			
			app.Quit();		
	
			System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
	
			app = null;

			System.GC.Collect();		
			System.GC.WaitForPendingFinalizers();
			System.GC.Collect();		
		}
	}
}
 
Naja .. da steht nix Neues für mich. Wie gesagt ich entferne schon die Excel Applikation mittels:

System.Runtime.InteropServices.Marshal.ReleaseComObject(app);

Aber der Prozess läuft trotzdem noch. Ich scheine etwas vergessen zu haben. Ich weiß nur nicht was.

Danke trotzdem ..
 
Eventuell das WorkSheet vor dem WorkBook vernichten. Und dann solltest den GC auch nicht unbedingt manuell aufrufen. Der weiß schon wann er soll.
 
Ok .. ich probier das morgen, wenn ich wieder auf Arbeit bin.

Vielen Dank so weit. Obwohl mir schon schwant, dass es nix hilft :) ..
 

Neue Beiträge

Zurück