Exemplo n.º 1
0
        /// <summary>
        ///   Export in Excel the results from any datagridview
        /// </summary>
        /// <param name = "dgvResults">DataGridView with the results</param>
        /// <param name = "footer">Footer written at the end of the file</param>
        public static void ExportInExcel(DataGridView dgvResults, params Object[] footer)
        {
            SaveFileDialog ofd = new SaveFileDialog {Filter = Resources.FileFilterCSV, FileName = "results.csv"};
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                CSVWriter writer = new CSVWriter(ofd.FileName);

                object[][] values = new object[dgvResults.Rows.Count + 2][];
                int rowIndex = 0;
                int colIndex = 0;
                foreach (DataGridViewColumn col in dgvResults.Columns) /*Writing Column Headers*/
                {
                    values[colIndex] = new object[dgvResults.Columns.Count];
                    values[rowIndex][colIndex] = col.HeaderText;
                    colIndex++;
                }
                rowIndex++; /*1*/

                foreach (DataGridViewRow row in dgvResults.Rows) /*Writing the values*/
                {
                    colIndex = 0;
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        values[rowIndex][colIndex] = cell.Value;
                        colIndex++;
                    }
                    rowIndex++;
                }

                for (int i = 0; i < footer.Length; i++)
                    values[rowIndex][i] = footer[i];

                /*Writing the results in the last row*/
                writer.Write(values);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        ///   Export in Excel the results from any datagridview
        /// </summary>
        /// <param name = "dgvResults">DataGridView with the results</param>
        /// <param name = "footer">Footer written at the end of the file</param>
        public static void ExportInExcel(DataGridView dgvResults, params Object[] footer)
        {
            SaveFileDialog ofd = new SaveFileDialog {Filter = Resources.FileFilterCSV, FileName = "results.csv"};
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                CSVWriter writer = new CSVWriter(ofd.FileName);

                object[][] values = new object[dgvResults.Rows.Count + 2][];
                int rowIndex = 0;
                int colIndex = 0;
                foreach (DataGridViewColumn col in dgvResults.Columns) /*Writing Column Headers*/
                {
                    values[colIndex] = new object[dgvResults.Columns.Count];
                    values[rowIndex][colIndex] = col.HeaderText;
                    colIndex++;
                }
                rowIndex++; /*1*/

                foreach (DataGridViewRow row in dgvResults.Rows) /*Writing the values*/
                {
                    colIndex = 0;
                    foreach (DataGridViewCell cell in row.Cells)
                    {
                        values[rowIndex][colIndex] = cell.Value;
                        colIndex++;
                    }
                    rowIndex++;
                }

                for (int i = 0; i < footer.Length; i++)
                    values[rowIndex][i] = footer[i];

                /*Writing the results in the last row*/
                writer.Write(values);
            }
        }