/// <summary> /// Export the dataGridView data to Excel /// </summary> /// <param name="dataGridView">DataGridView to export</param> public static void ExportToExcel(DataGridView dataGridView) { SIGEM.Client.CancelWindow cancelWindow = new SIGEM.Client.CancelWindow(); cancelWindow.Show(); cancelWindow.MaximunProgressBarValue = dataGridView.RowCount; // Create the Excel object. object excel = CreateExcelObject(); try { #region Insert columns names // Insert the columns names at the first Excel row int columnIndex = 0; foreach (DataGridViewColumn col in dataGridView.Columns) { columnIndex++; SetExcelCell(1, columnIndex, col.HeaderText, excel); // If the process is cancelled. if (cancelWindow.Cancel) { SetVisibleExcel(true, excel); cancelWindow.Close(); return; } } #endregion Insert columns names #region Insert values // Insert on each Excel cell the argument value of each row. int rowIndex = 0; foreach (DataGridViewRow row in dataGridView.Rows) { rowIndex++; columnIndex = 0; foreach (DataGridViewColumn col in dataGridView.Columns) { columnIndex++; if (col is DataGridViewComboBoxColumn) { SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].FormattedValue, excel); } else { if (row.Cells[col.Name].Value.GetType().Name == "TimeSpan") { SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].Value.ToString(), excel); } else { SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].Value, excel); } } // If the process is cancelled. if (cancelWindow.Cancel) { SetVisibleExcel(true, excel); cancelWindow.Close(); return; } } cancelWindow.Value = rowIndex; } #endregion Insert values } catch (Exception e) { SetVisibleExcel(true, excel); cancelWindow.Close(); Presentation.ScenarioManager.LaunchErrorScenario(e); } // Close the cancel window. cancelWindow.Close(); // Set the Excel document visible. SetVisibleExcel(true, excel); }
/// <summary> /// Export the dataGridView data to Excel /// </summary> /// <param name="dataGridView">DataGridView to export</param> public static void ExportToExcel(DataGridView dataGridView) { SIGEM.Client.CancelWindow cancelWindow = new SIGEM.Client.CancelWindow(); cancelWindow.Show(); cancelWindow.MaximunProgressBarValue = dataGridView.RowCount; // Create the Excel object. object excel = CreateExcelObject(); try { #region Insert columns names // Insert the columns names at the first Excel row int columnIndex = 0; foreach (DataGridViewColumn col in dataGridView.Columns) { columnIndex++; SetExcelCell(1, columnIndex, col.HeaderText, excel); // If the process is cancelled. if (cancelWindow.Cancel) { SetVisibleExcel(true, excel); cancelWindow.Close(); return; } } #endregion Insert columns names #region Insert values // Insert on each Excel cell the argument value of each row. int rowIndex = 0; foreach (DataGridViewRow row in dataGridView.Rows) { rowIndex++; columnIndex = 0; foreach (DataGridViewColumn col in dataGridView.Columns) { columnIndex++; if (col is DataGridViewComboBoxColumn) { SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].FormattedValue, excel); } else { if (row.Cells[col.Name].Value.GetType().Name == "TimeSpan") SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].Value.ToString(), excel); else SetExcelCell(rowIndex + 1, columnIndex, row.Cells[col.Name].Value, excel); } // If the process is cancelled. if (cancelWindow.Cancel) { SetVisibleExcel(true, excel); cancelWindow.Close(); return; } } cancelWindow.Value = rowIndex; } #endregion Insert values } catch (Exception e) { SetVisibleExcel(true, excel); cancelWindow.Close(); Presentation.ScenarioManager.LaunchErrorScenario(e); } // Close the cancel window. cancelWindow.Close(); // Set the Excel document visible. SetVisibleExcel(true, excel); }