private void dataToExelMainMenuItem_Click(object sender, EventArgs e) { try { Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.ApplicationClass(); Microsoft.Office.Interop.Excel.Workbooks wbs = excel.Workbooks; object obj = System.Reflection.Missing.Value; Microsoft.Office.Interop.Excel.Workbook wb = wbs.Add(obj); Microsoft.Office.Interop.Excel.Sheets ss = wb.Worksheets; Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)ss.get_Item(1); //object[] headers = { "First", "Second", "Third1111111111" }; //Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", "C1"); //range.Value2 = headers; //range.AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatReport1, obj, obj, obj, obj, obj, obj); //range.Font.Bold = true; //range = ws.get_Range("A2", obj); //range = range.get_Resize(3, 3); //range.Borders.Value = true; ////range.AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatSimple, obj, obj, obj, obj, obj, obj); //object[,] data = { { "1", "2", "3" }, { "4", "5", "6" }, { "7", "8", "9" } }; //range.Value2 = data; //excel.Visible = true; DataGridView grid = tabControl1.SelectedTab.Controls[0] as DataGridView; Microsoft.Office.Interop.Excel.Range range = ws.get_Range("A1", obj); range = range.get_Resize(grid.Rows.Count, 6); object[,] data = new object[grid.Rows.Count, 6]; data[0, 0] = "Дата"; data[0, 1] = "Время"; data[0, 2] = "Широта"; data[0, 3] = "Долгота"; data[0, 4] = "От предыдущей, м"; data[0, 5] = "Всего, м"; for (int i = 0; i < grid.Rows.Count - 1; i++) { for (int j = 0; j < 6; j++) { data[i + 1, j] = grid[j, i].Value; } } range.Value2 = data; range.AutoFormat(Microsoft.Office.Interop.Excel.XlRangeAutoFormat.xlRangeAutoFormatSimple, obj, obj, obj, obj, obj, obj); excel.Visible = true; } #pragma warning disable 168 catch (NullReferenceException except) { MessageBox.Show("Нет данных для отображения", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception except) { MessageBox.Show("При передаче данных в Excel произошла ошибка", "Ошибка-", MessageBoxButtons.OK, MessageBoxIcon.Error); } #pragma warning restore 168 }