Autor Beitrag
lubu
Hält's aus hier
Beiträge: 2



BeitragVerfasst: Do 04.12.14 16:05 
Hallo

Ich habe hier den Code um ein neues Excel File zu öffnen.
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:
// Variablen deklarieren 
Microsoft.Office.Interop.Excel.Application myExcelApplication;
Microsoft.Office.Interop.Excel.Workbook myExcelWorkbook;
Microsoft.Office.Interop.Excel.Worksheet myExcelWorkSheet;
myExcelApplication = null;

try
{
    // First Contact: Excel Prozess initialisieren
    myExcelApplication = new Microsoft.Office.Interop.Excel.Application();
    myExcelApplication.Visible = false;
    myExcelApplication.ScreenUpdating = true;

    // Excel Datei anlegen: Workbook
    var myCount = myExcelApplication.Workbooks.Count;
    myExcelWorkbook = (Microsoft.Office.Interop.Excel.Workbook)(myExcelApplication.Workbooks.Add(System.Reflection.Missing.Value));
    myExcelWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)myExcelWorkbook.ActiveSheet;

    // Daten eingeben 
                    
    myExcelWorkSheet.Cells[34] = Properties.Settings.Default.name;
                        
    // Excel Datei abspeichern 
    // wenn die Datei vorher vorhanden ist, kommt in Excel eine Fehlermeldung. 
    myExcelWorkbook.Close(true"Roamingtest.xls", System.Reflection.Missing.Value);
}

catch (Exception ex)
{
    String myErrorString = ex.Message;
    MessageBox.Show(myErrorString);
}
finally
{
    // Excel beenden 
    if (myExcelApplication != null)
    {
        myExcelApplication.Quit();
    }
}

Was muss man hier verändern damit man nicht eine neue Instanz von Excel gestartet wird, sondern schon eine bestehende Datei geladen wird.

Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Moderiert von user profile iconTh69: Topic aus Basistechnologien verschoben am Do 04.12.2014 um 16:29
Talemantros
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 444
Erhaltene Danke: 2

Win7 Proff 64bit
C# (VS2013)
BeitragVerfasst: Fr 05.12.14 08:41 
Guten Morgen,
Ich hatte mir mal was rausgesucht für die Zukunft, weil ich das auch noch machen muss, aber ist bisher ungetestet

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
        private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            object misValue = System.Reflection.Missing.Value;

            xlApp = new Excel.Application();
            xlWorkBook = xlApp.Workbooks.Open("test.xls"0true5""""true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t"falsefalse0true10);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            MessageBox.Show(xlWorkSheet.get_Range("A1","A1").Value2.ToString());

            xlWorkBook.Close(true, misValue, misValue);
            xlApp.Quit();

            releaseObject(xlWorkSheet);
            releaseObject(xlWorkBook);
            releaseObject(xlApp);
        }


Vielleicht hilft es dir.

ich weiß nicht, ob ich den Link zur Seite posten darf, daher lasse ich es erstmal :-)