Autor Beitrag
WSS-130
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: Mi 13.02.13 09:51 
Hi,

ich brauch mal wieder die Hilfe von einem der schon mehr Erfahrung gesammelt hab als ich.
Ich schreibe in meinem Programm Daten in eine Excel Datei und benutze dazu com Objekte.

Mein Export der Daten läuft im BackgroundWorker, wird also so gestartet:
Background_DoWork(...)
ausblenden C#-Quelltext
1:
2:
3:
4:
private void Background_DoWork(...)
{
    exportData.exportXLS(worker, e);
}


Export:
ausblenden volle Höhe C#-Quelltext
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:
public void exportXLS(System.ComponentModel.BackgroundWorker worker, System.ComponentModel.DoWorkEventArgs e)
{
  Excel.Application excapp = null;
  Excel.Workbooks books = null;
  Excel.Workbook workbook = null;
  Excel.Sheets sheets = null;
  Excel.Worksheet sheet = null;
  Excel.Range firstRow = null;
  
  excapp = new Excel.Application();
  books = excapp.Workbooks;
  workbook = books.Add(Excel.XlWBATemplate.xlWBATWorksheet);
  sheets = workbook.Worksheets;
  sheet = sheets[1];

  object n = null;
  string colLast = null;

  try
  {
    //Variablen
    colLast = "U";
    int rowCurrent = 1;
    n = System.Reflection.Missing.Value;

    //erstmal Üerschriften
    sheet.Cells[rowCurrent, 1] = "Überschrift";
    //... hier ist noch mehr drin
    sheet.Cells[rowCurrent, 21] = "Überschrift";

    //nächste zeile, beginne mit befüllen
    rowCurrent++;

    foreach (signFlow flow in container)
    {
      if (worker.CancellationPending)
      {
        e.Cancel = true;
        File.Delete(path);
        break;
      }
      else
      {
        int colCurrent = 1;

        sheet.Cells[rowCurrent, colCurrent] = flow.callIntSign.getName;
        colCurrent++;
        //... hier ist noch mehr drin
        sheet.Cells[rowCurrent, colCurrent] = flow.callMetaData.getcomment;
        colCurrent++;
      }
    }

    // Excel formatieren
    // Schriftart
    sheet.get_Range("A1", colLast + rowCurrent).Font.Name = "Arial Narrow";
    // Schriftgröße
    sheet.get_Range("A1", colLast + rowCurrent).Font.Size = 8;
    // Fettschrift
    sheet.get_Range("A1", colLast + "1").Font.Bold = true;
    // Orientierung aufwärts
    sheet.get_Range("M1""U1").Orientation = Excel.XlOrientation.xlUpward;
    // Ausrichtung linksbündig
    sheet.get_Range("A1", colLast + "1").HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft;
    // Spaltenbreite
    sheet.get_Range("A1""A1").ColumnWidth = 20;
    //... hier ist noch mehr drin
    sheet.get_Range("U1""U1").ColumnWidth = 20;
    // Zeilenumbruch
    sheet.get_Range("A1", colLast + rowCurrent).Cells.WrapText = true;
    // Autofilter
    sheet.get_Range("A1", colLast + rowCurrent).AutoFilter(1, Type.Missing, Excel.XlAutoFilterOperator.xlAnd, Type.Missing, true);    // rowCurrent - 1, wenn %Ende% entfällt
    // Fenster fixieren
    firstRow = sheet.get_Range("B2""B2");
    firstRow.Activate();
    firstRow.Select();
    firstRow.Application.ActiveWindow.FreezePanes = true;

    //Datei speichern und workbook schließen
    if (e.Cancel != true)
    {
      workbook.SaveAs(path, n, n,
        n, n, n, Excel.XlSaveAsAccessMode.xlNoChange,
        n, n, n, n, n);
      workbook.Close(false, n, n);
      excapp.Workbooks.Close();
      excapp.Application.Quit();
      excapp.Quit();
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show("Fehler beim speichern des Excel-Exports " + ex.Message);
  }
  finally
  {
    //COM Objekte freigeben um EXCEL.exe zu schließen

    while (Marshal.FinalReleaseComObject(firstRow) > 0) ;
    while (Marshal.FinalReleaseComObject(sheet) > 0) ;
    while (Marshal.FinalReleaseComObject(sheets) > 0) ;
    while (Marshal.FinalReleaseComObject(workbook) > 0) ;
    while (Marshal.FinalReleaseComObject(books) > 0) ;
    while (Marshal.FinalReleaseComObject(excapp) > 0) ;

    //Variablen auf Null setzen
    firstRow = null;
    sheet = null;
    sheets = null;
    workbook = null;
    books = null;
    excapp = null;
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
  }
}


RunWorkerCompleted:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
private void RunWorkerCompleted(...)
{
    if (e.Error != null)
        MessageBox.Show("Error: " + e.Error.Message);
    else if (e.Cancelled)
        MessageBox.Show("Export canceled.");
    else
    {
        button_startcancel.Text = "Start";
        MessageBox.Show("Einlesen beendet in: " + watch.ElapsedMilliseconds + "ms");
        //Backgroundworker beenden
    }
    background_Export.Dispose();
}

Der schon oft besprochene Punkt ist bei eben auch, dass sich der Excel Prozess nicht nach Ende der "RunWorkerCompleted" schließt. Es sind also wohl noch com-Objekte offen die noch releast werden müssen....nur wo???

Viele Grüße und danke schonmal an die Helfer!