Exporting Data From Multiple DataGridViews on a Form to Multiple Excel Sheets in a Single Excel File

1

I have a Database Application in which it has multiple dataGridViews on the Form. I am able to successfully export a single grid view to an Excel sheet but I would I would like to export the grid to different Excel sheets in a single Excel file.

I tried the following which didn't work.

public void ExportGridViewToExcel(DataGridView dataGridViewCommission, DataGridView dataGridViewPaymentsReceived, string commissionsheet, string paymentsheet)
{
    // creating Excel Application
    Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();


    // creating new WorkBook within Excel application
    Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);


    // creating new Excelsheet in workbook
    Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
    Microsoft.Office.Interop.Excel._Worksheet worksheet2 = null;

    // see the excel sheet behind the program
    //  app.Visible = true;

    // get the reference of first sheet. By default its name is Sheet1.
    // store its reference to worksheet
    worksheet1 = workbook.Sheets[1];

    // changing the name of active sheet
    worksheet1.Name = commissionsheet;

    string filelocation;
    SaveFileDialog SaveFile = new SaveFileDialog();
    if (SaveFile.ShowDialog() == DialogResult.OK)
    {
        filelocation = SaveFile.FileName;

        // storing header part in Excel
        for (int i = 1; i < dataGridViewCommission.Columns.Count + 1; i++)
        {
            worksheet1.Cells[1, i + 1] = dataGridViewCommission.Columns[i - 1].HeaderCell.Value;

        }

        for (int i = 1; i < dataGridViewCommission.Rows.Count + 1; i++)
        {
            worksheet1.Cells[i + 1, 1] = dataGridViewCommission.Rows[i - 1].HeaderCell.Value;
            //worksheet2.Cells[i + 1, 1] = dataGridViewPaymentsReceived.Rows[i - 1].HeaderCell.Value;
        }

        for (int i = 0; i < dataGridViewCommission.Rows.Count; i++)
        {
            for (int j = 0; j < dataGridViewCommission.Columns.Count; j++)
            {
                worksheet1.Cells[i + 2, j + 2] = dataGridViewCommission.Rows[i].Cells[j].Value.ToString();


            }
        }

        worksheet2 = workbook.Sheets["Sheet2"];
        worksheet2.Name = paymentsheet;

        for (int i = 1; i < dataGridViewPaymentsReceived.Columns.Count + 1; i++)
        {
            worksheet2.Cells[1, i + 1] = dataGridViewPaymentsReceived.Columns[i - 1].HeaderCell.Value;
        }


        // storing Each row and column value to excel sheet


        for (int i = 0; i < dataGridViewPaymentsReceived.Rows.Count; i++)
        {
            for (int j = 0; j < dataGridViewPaymentsReceived.Columns.Count; j++)
            {

                worksheet2.Cells[i + 2, j + 2] = dataGridViewPaymentsReceived.Rows[i].Cells[j].Value.ToString();

            }
        }

        // save the application
        workbook.SaveAs(filelocation, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
        workbook.Close(true, Type.Missing, Type.Missing);
        app.Quit();

        Process.Start(filelocation + ".xls");
    }
}

I encountered a COMexception, Invalid Index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX))

Can anyone suggest me a way to get away with this?

c#
winforms
excel
excel-interop
asked on Stack Overflow Aug 14, 2013 by DoIt • edited Aug 14, 2013 by StanK

1 Answer

1

Maybe you need to add more sheets before you can access Sheet at index 2

int count = workbook.Worksheets.Count;
Excel.Worksheet addedSheet = workbook.Worksheets.Add(Type.Missing,
workbook.Worksheets[count], Type.Missing, Type.Missing);
answered on Stack Overflow Aug 14, 2013 by Mauricio Gracia Gutierrez • edited Aug 14, 2013 by Mauricio Gracia Gutierrez

User contributions licensed under CC BY-SA 3.0