tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
2
ZUGRIFFE
615
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    42_informatikerIN 42_informatikerIN ist offline Grünschnabel
    Registriert seit
    Aug 2011
    Beiträge
    2
    Hallo Zusammen

    Ich bin nun schon tagelang am googlen und habe schon viele verschiedene Lösungen für mein Problem gefunden und ausprobiert. Aber bei mir bleibt der Prozess immer noch im Task-Manager hängen.

    Umgebung: Windows XP, MS Excel 2003

    Ich erstelle eine Excel Applikation, schreibe Daten aus einer Gridview rein, anschliessend erstelle ich ein Diagramm und schliesse Excel wieder. Der Prozess EXCEL.EXE bleibt aber immer im Task-Manager.

    Den Prozess abschiessen, möchte ich nicht und als ich es ausprobiert habe hat es nicht funktioniert (Fehler bei process.Kill(); "Access is denied").

    Wenn ich nur die Applikation starte, ein Workbook und Worksheet erstelle und wieder schliesse, ist der Prozess weg. Es muss also irgendwo am Code dazwischen liegen. Ich vermute, dass irgendein Objekt noch auf den Prozess referenziert ist, finde aber einfach nicht heraus welches..

    Hier der Code meines Excel Exports (Was in der Gridview und in den Dropdownlists steht ist ja nicht relevant, oder? ):

    Code csharp:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    
    private void excelerstellen(DropDownList ddlma, DropDownList ddljahr, GridView gridview)
            {
                object n = System.Reflection.Missing.Value;
     
                Excel.Application xlapp = new Excel.Application();
                Excel.Workbooks workbooks = xlapp.Workbooks;
                Excel.Workbook xlwb = workbooks.Add(n);
                Excel.Sheets sheets = xlapp.Worksheets;
                Excel.Worksheet xlws = (Excel.Worksheet)sheets[1];            
                
                string system = "";
                string ist = "";
                string soll = "";
     
                try
                {
                    if ((bool)Session["Admin"] == true)
                    {
                        // Worksheet Name
                        xlws.Name = "Knowhow " + ddlma.SelectedItem.Text;
                        // Überschriften eingeben 
                        xlws.Cells[2, 1] = "Knowhow " + ddlma.SelectedItem.Text + " " + ddljahr.SelectedItem.Text;   //A2  
                    }
                    else
                    {
                        // Worksheet Name
                        xlws.Name = "Knowhow " + lbl_ma_name.Text;
                        // Überschriften eingeben 
                        xlws.Cells[2, 1] = "Knowhow " + lbl_ma_name.Text + " " + ddljahr.SelectedItem.Text;   //A2                }
                    xlws.Cells[4, 2] = "Level IST";   //B4 
                    xlws.Cells[4, 3] = "Level SOLL";    //C5
     
                    // Formatieren der Überschrift 
                    Excel.Range myRangeHeadline;
                    myRangeHeadline = xlws.get_Range("A2", "A2");
                    myRangeHeadline.Font.Bold = true;
     
                    // Daten eingeben 
                    int i = 5;
                    foreach (GridViewRow row in gridview.Rows)
                    {
     
                        if (gridview.ID == "gv_ändern_bewertung")
                        {
                            system = row.Cells[5].Text;
                            ist = row.Cells[6].Text;
                            soll = row.Cells[7].Text;
                        }
                        else if (gridview.ID == "gv_ansicht_bewertung")
                        {
                            system = row.Cells[4].Text;
                            ist = row.Cells[5].Text;
                            soll = row.Cells[6].Text;
                        }
                        //System
                        system = system.Replace("ö", "ö").Replace("ä", "ä").Replace("ü", "ü").Replace("&", "&");
                        xlws.Cells[i, 1] = system;
     
                        // ist
                        int ende1 = ist.IndexOf("'>") + 2;
                        ist = ist.Remove(0, ende1);
                        xlws.Cells[i, 2] = ist.Remove(1, ist.Length - 1);
     
                        //soll                    
                        int ende2 = soll.IndexOf("'>") + 2;
                        soll = soll.Remove(0, ende2);
                        xlws.Cells[i, 3] = soll.Remove(1, soll.Length - 1);
                        i++;
                    }
                    string letzte = (i - 1).ToString();   
                    xlws.get_Range("A1", "A10").EntireColumn.AutoFit();
                    // Chart erzeugen          
                    Excel.Chart myChart = (Excel.Chart)xlwb.Charts.Add(n, n, n, n);
                    myChart.ChartType = Excel.XlChartType.xlRadar;
                    myChart.HasLegend = true;
                    myChart.PlotBy = Excel.XlRowCol.xlColumns;
                  
                    // Datenquelle festlegen
                    Excel.Range range = xlws.get_Range("A4", "C" + letzte);
                    myChart.SetSourceData(range, Type.Missing);
                    myChart.HasTitle = true;                                                            
                    myChart.ChartTitle.Text = xlws.Name.ToString();               
     
                    myChart.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlBitmap, Excel.XlPictureAppearance.xlScreen);
                    myChart.Location(Excel.XlChartLocation.xlLocationAsObject, xlws.Name);
                    string pfad_app = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
                    if (pfad_app.EndsWith(@"\"))
                    {
                        pfad_app = pfad_app + @"Excel\" + Session["Username"].ToString() + ".xls";
                    }
                    else
                    {
                        pfad_app = pfad_app + @"\Excel\" + Session["Username"].ToString() + ".xls";
                    }
                    // wenn die Datei schon vorhanden ist, kommt in Excel eine Meldung ob man überschreiben will, diese wird hier ausgeschaltet.
                    xlapp.DisplayAlerts = false;
                    xlapp.AlertBeforeOverwriting = false;
                    // Excel Datei abspeichern              
                    xlwb.SaveAs(pfad_app, n, n, n, n, n, Excel.XlSaveAsAccessMode.xlNoChange, n, n, n, n, n);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(range);
                    range = null;
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myChart);
                    myChart = null;
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(myRangeHeadline);
                    myRangeHeadline = null;                           
                }
                catch (Exception ex)
                {
                    lbl_Fehlermeldung.Text = ex.Message;
                    lbl_Fehlermeldung.Visible = true;
                }
                finally
                {                
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlws.Cells);
                    foreach (Excel.Worksheet worksheet in sheets)
                    {
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(worksheet);
                    }
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(sheets);
                    sheets = null;
                    foreach (Excel.Workbook workbook in workbooks)
                    {
                        workbook.Close(false, n, n);
                        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbook);
                    }
                    xlapp.Workbooks.Close();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(workbooks);
                    workbooks = null;
                    xlapp.Application.Quit();
                    xlapp.Quit();
                    System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlapp);
                    xlapp = null;
                    GC.Collect();
                    GC.WaitForPendingFinalizers();                            
                }
            }

    Vielen Dank im Voraus!!
    Liebe Grüsse 42_informatikerIN
    Geändert von Nico Graichen (09.08.11 um 14:37 Uhr) Grund: Code-Tags eingefügt
     

  2. #2
    Avatar von Shakie
    Shakie Shakie ist offline Mitglied Diamant
    Registriert seit
    May 2004
    Ort
    Europa
    Beiträge
    2.048
    Das scheint ein bekanntes Problem zu sein. Schau mal hier.
     
    hihi = -h²

  3. #3
    42_informatikerIN 42_informatikerIN ist offline Grünschnabel
    Registriert seit
    Aug 2011
    Beiträge
    2
    Danke, aber wie du siehst habe ich das alles bereits in meinem Code berücksichtigt.
    Trotzdem bleibt anscheinend irgendwo noch ein COM Objekt offen.
    Und wie bereits beschrieben, wenn ich den Teil mit dem dem füllen und dem Diagramm rausnehme, funktionierts und der Excel Prozess wird geschlossen.

    Hoffe jemand hat eine Lösung für mein Problem!
    LG
     

Ähnliche Themen

  1. task manager
    Von MSVCplusplus im Forum C/C++
    Antworten: 5
    Letzter Beitrag: 23.12.10, 16:22
  2. Antworten: 1
    Letzter Beitrag: 26.10.07, 11:11
  3. Prozess im Task Manager beenden...
    Von themadman im Forum Visual Basic 6.0
    Antworten: 4
    Letzter Beitrag: 23.03.05, 18:03
  4. Task-Manager
    Von Moleman im Forum Microsoft Windows
    Antworten: 2
    Letzter Beitrag: 23.04.04, 23:11
  5. Hilfe! Excel bleibt als Prozess erhalten
    Von sernetix im Forum Visual Basic 6.0
    Antworten: 5
    Letzter Beitrag: 11.10.03, 17:06

Stichworte