Entwickler-Ecke

WinForms - DataGridview in Excel schreiben


Ivy - Di 24.01.12 12:17
Titel: DataGridview in Excel schreiben
Hallo zusammen,
ich versuche gerade daten aus meiner DGV in excel zu speichern. ich habe dafür dieses codesnippet verwendet:


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:
 Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
            // creating new WorkBook within Excel application
            Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
            // creating new Excelsheet in workbook
            Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
            // see the excel sheet behind the program
            //Funny
            app.Visible = true;
            // get the reference of first sheet. By default its name is Sheet1.
            // store its reference to worksheet

            //Fixed:(Microsoft.Office.Interop.Excel.Worksheet)
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Tabelle1"];
            worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;
            // changing the name of active sheet
            worksheet.Name = "Exported from Ketoan";
            // storing header part in Excel
            for (int i = 1; i < DatenTabelle.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = DatenTabelle.Columns[i - 1].HeaderText;
            }
            // storing Each row and column value to excel sheet
            for (int i = 0; i < DatenTabelle.Rows.Count - 1; i++)
            {
                for (int j = 0; j < DatenTabelle.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = DatenTabelle.Rows[i].Cells[j].Value.ToString();
                }
            }

            // save the application
            string fileName = String.Empty;
            //SaveFileDialog saveFileDialog1 = new SaveFileDialog();

            fileName = "D:\\abs.xls";
            //Fixed-old code :11 para->add 1:Type.Missing
            workbook.SaveAs(fileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);



            // Exit from the application
            //app.Quit();
            app.Quit();
            workbook = null;
            app = null;


nun kommt bei dieser zeile ein fehler:

C#-Quelltext
1:
worksheet.Cells[i + 2, j + 1] = DatenTabelle.Rows[i].Cells[j].Value.ToString();                    


"Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt."

woran könnte das liegen??

LG
IVY


Th69 - Di 24.01.12 13:38

Hallo Ivy,

es liegt daran, daß einer der Variablen (bzw. Eigenschaften) null ist.
Der Debugger sagt dir das aber besser als jemand aus der Ferne hier im Forum...


Ivy - Di 24.01.12 16:45

EDIT erledigt