Good evening everyone. I added a DataGridView and Button to the form. The button was supposed to export to Excel . But it gives an error: The object reference does not indicate an object instance.
//Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ DataTable DataTable dt = new DataTable(); //ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ foreach( DataGridViewColumn column in dataGridView.Columns) { dt.Columns.Add(column.HeaderText, column.ValueType); } //ΠΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ ΡΡΡΠΎΠΊΠΈ foreach(DataGridViewRow row in dataGridView.Rows) { dt.Rows.Add(); foreach(DataGridViewCell cell in row.Cells) { dt.Rows[dt.Rows.Count - 1][cell.ColumnIndex] = cell.Value.ToString();//ΠΡΠΈΠ±ΠΊΠ° Π·Π΄Π΅ΡΡ } } //ΠΠΊΡΠΏΠΎΡΡ Π² Excel string folderPath = @"C:\\Excel\\"; if(!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } using (XLWorkbook wb = new XLWorkbook()) { wb.Worksheets.Add(dt, "Customers"); wb.SaveAs(folderPath + "DataGridViewExport.xlsx"); }