How to export to Excel along with the headings?

1

With the following code I can export without problems a datagridview in specific but without the headers. What do I need to achieve this?

    public void ExportarDataGridViewExcel(DataGridView dataCWS)
    {
        try
        {
            SaveFileDialog fichero = new SaveFileDialog();
            fichero.Filter = "Excel (*.xls)|*.xls";
            fichero.FileName = "ArchivoExportado";
            if (fichero.ShowDialog() == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel.Application aplicacion;
                Microsoft.Office.Interop.Excel.Workbook libros_trabajo;
                Microsoft.Office.Interop.Excel.Worksheet hoja_trabajo;

                aplicacion = new Microsoft.Office.Interop.Excel.Application();
                libros_trabajo = aplicacion.Workbooks.Add();
                hoja_trabajo =
                    (Microsoft.Office.Interop.Excel.Worksheet)libros_trabajo.Worksheets.get_Item(1);

                for (int i = 0; i < dataCWS.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dataCWS.Columns.Count; j++)
                    {
                        if ((dataCWS.Rows[i].Cells[j].Value == null) == false)
                        {
                            hoja_trabajo.Cells[i + 1, j + 1] = dataCWS.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                }
                libros_trabajo.SaveAs(fichero.FileName,
                    Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal);
                libros_trabajo.Close(true);
                aplicacion.Quit();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Cannot import because: " + ex.ToString());
        }
            }
        }
    }
    
asked by Eduardo Parra 03.01.2017 в 16:51
source

3 answers

3

The headers information is in the column collection of DataGridView , so you can do something just after loading the Worksheet :

hoja_trabajo = (Microsoft.Office.Interop.Excel.Worksheet)libros_trabajo.Worksheets.get_Item(1);

for (int i = 0; i < dataCWS.ColumnCount; i++)
{
    hoja_trabajo.Cells[1, i + 1] = dataCWS.Columns[i].HeaderText;
}

Now keep in mind that the contents of the cells will start from line 2 and not from 1.

    
answered by 03.01.2017 / 17:22
source
0

You could change your cycle in the following way:

int columnaIndice = 1;

            for (int i = 0; i < dataCWS.Rows.Count - 1; i++)
            {
                for (int j = 0; j < dataCWS.Columns.Count; j++)
                {
                    if(columnaIndice = 1)
                    {
                        var titulo = dataCWS.Columns[j].HeaderText;

                        // Lo asignas
                    }

                    if ((dataCWS.Rows[i].Cells[j].Value == null) == false)
                    {
                        hoja_trabajo.Cells[i + 1, j + 1] = dataCWS.Rows[i].Cells[j].Value.ToString();
                    }

                    columnaIndice++;
                }

                columnaIndice = 1;
            }
    
answered by 03.01.2017 в 17:33
0

The error is in your variables if you run it step by step you will only see that you write about the headers if you put

 for (int i = 1; i <= this.dataGridView1.Columns.Count; i++)
                    {
                        hoja_trabajo.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText;

                    }

what you have to do to correct it is this:

  int valorFila = 0;
                    for (int i = 1; i <= this.dataGridView1.Columns.Count; i++)
                    {
                        hoja_trabajo.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText;

                    }
                    valorFila = valorFila + 1;
                    for (int i = 0; i < dataCWS.Rows.Count - 1; i++)
                    {
                        for (int j = 0; j < dataCWS.Columns.Count; j++)
                        {
                            if ((dataCWS.Rows[i].Cells[j].Value == null) == false)
                            {

                                    hoja_trabajo.Cells[valorFila+1, j + 1] = dataCWS.Rows[i].Cells[j].Value.ToString();


                            }
                        }
                        valorFila++;
                    }

and ready shows you all the values with header

    
answered by 26.04.2017 в 01:47