Skip to main content

public void exportaraexcel(DataGridView tabla) {

    Microsoft.Office.Interop.Excel.Application excel = new 
      Microsoft.Office.Interop.Excel.Application();

    excel.Application.Workbooks.Add(true);

    int IndiceColumna = 0;

    foreach (DataGridViewColumn col in tabla.Columns)
    {

        IndiceColumna++;

        excel.Cells[1, IndiceColumna] = col.Name;

    }

    int IndeceFila = 0;

    foreach (DataGridViewRow row in tabla.Rows)
    {

        IndeceFila++;

        IndiceColumna = 0;

        foreach (DataGridViewColumn col in tabla.Columns)
        {

            IndiceColumna++;
        object cellValue = row.Cells[col.Name].Value;
        excel.Cells[IndeceFila + 1, IndiceColumna] = cellValue != null ? cellValue.ToString() : string.Empty;



    }

}

excel.Visible = true;
public void exportaraexcel(DataGridView tabla)
{
    
        Microsoft.Office.Interop.Excel.Application excel = new 
          Microsoft.Office.Interop.Excel.Application();

        excel.Application.Workbooks.Add(true);

        int IndiceColumna = 0;

        foreach (DataGridViewColumn col in tabla.Columns)
        {

            IndiceColumna++;

            excel.Cells[1, IndiceColumna] = col.Name;

        }

        int IndeceFila = 0;

        foreach (DataGridViewRow row in tabla.Rows)
        {

            IndeceFila++;

            IndiceColumna = 0;

            foreach (DataGridViewColumn col in tabla.Columns)
            {

                IndiceColumna++;
            object cellValue = row.Cells[col.Name].Value;
            excel.Cells[IndeceFila + 1, IndiceColumna] = cellValue != null ? cellValue.ToString() : string.Empty;



        }

}

    excel.Visible = true;


public void exportaraexcel(DataGridView tabla) {

    Microsoft.Office.Interop.Excel.Application excel = new 
      Microsoft.Office.Interop.Excel.Application();

    excel.Application.Workbooks.Add(true);

    int IndiceColumna = 0;

    foreach (DataGridViewColumn col in tabla.Columns)
    {

        IndiceColumna++;

        excel.Cells[1, IndiceColumna] = col.Name;

    }

    int IndeceFila = 0;

    foreach (DataGridViewRow row in tabla.Rows)
    {

        IndeceFila++;

        IndiceColumna = 0;

        foreach (DataGridViewColumn col in tabla.Columns)
        {

            IndiceColumna++;
        object cellValue = row.Cells[col.Name].Value;
        excel.Cells[IndeceFila + 1, IndiceColumna] = cellValue != null ? cellValue.ToString() : string.Empty;



    }

}

excel.Visible = true;
public void exportaraexcel(DataGridView tabla)
{
    
        Microsoft.Office.Interop.Excel.Application excel = new 
          Microsoft.Office.Interop.Excel.Application();

        excel.Application.Workbooks.Add(true);

        int IndiceColumna = 0;

        foreach (DataGridViewColumn col in tabla.Columns)
        {

            IndiceColumna++;

            excel.Cells[1, IndiceColumna] = col.Name;

        }

        int IndeceFila = 0;

        foreach (DataGridViewRow row in tabla.Rows)
        {

            IndeceFila++;

            IndiceColumna = 0;

            foreach (DataGridViewColumn col in tabla.Columns)
            {

                IndiceColumna++;
            object cellValue = row.Cells[col.Name].Value;
            excel.Cells[IndeceFila + 1, IndiceColumna] = cellValue != null ? cellValue.ToString() : string.Empty;



        }

}

    excel.Visible = true;


Origen Enlace

Logre solucionarlo asi , espero le sirva a alguien

public void exportaraexcel(DataGridView tabla) {

    Microsoft.Office.Interop.Excel.Application excel = new 
      Microsoft.Office.Interop.Excel.Application();

    excel.Application.Workbooks.Add(true);

    int IndiceColumna = 0;

    foreach (DataGridViewColumn col in tabla.Columns)
    {

        IndiceColumna++;

        excel.Cells[1, IndiceColumna] = col.Name;

    }

    int IndeceFila = 0;

    foreach (DataGridViewRow row in tabla.Rows)
    {

        IndeceFila++;

        IndiceColumna = 0;

        foreach (DataGridViewColumn col in tabla.Columns)
        {

            IndiceColumna++;
        object cellValue = row.Cells[col.Name].Value;
        excel.Cells[IndeceFila + 1, IndiceColumna] = cellValue != null ? cellValue.ToString() : string.Empty;



    }

}

excel.Visible = true;