0

I would like to export gridview1 and gridview2 into two separate worksheets that can be named in my codes as grid view 1 and grid view 2 in one Excel file. I have trouble exporting to Excel and am not sure how to call my export in the button and pass the parameters in to export two Gridviews:

public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName, int sheetid)
        {
            // creating new Excelsheet in workbook
            Microsoft.Office.Interop.Excel._Worksheet worksheet = 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
            worksheet = workbook.Sheets["Sheet" + sheetid];
            worksheet = workbook.ActiveSheet;

            // changing the name of active sheet
            worksheet.Name = SheetName;

            // storing header part in Excel
            for (int i = 1; i < gridview.Columns.Count + 1; i++)
            {
                worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText;
            }



            // storing Each row and column value to excel sheet
            for (int i = 0; i < gridview.Rows.Count - 1; i++)
            {
                for (int j = 0; j < gridview.Columns.Count; j++)
                {
                    worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString();
                }
            }


            // save the application
            workbook.SaveAs(@"C:\Users\testacc\Desktop\Test\output.xls", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

            // Exit from the application
            app.Quit();
        }

Where do I place the code below?

// 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);
pnuts
  • 58,317
  • 11
  • 87
  • 139
James Boer
  • 321
  • 4
  • 9
  • 28

1 Answers1

0

you will have to create those objects in

public void callingfunction() {

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);



ExportToExcel(app, workbook, gv1, 'sheet1', 1)

ExportToExcel(app, workbook, gv2, 'sheet2', 2)   
}
Laxmikant
  • 588
  • 4
  • 11
  • Hi there thanks for the answer. However I do receive error upon populated the data into excel. Hope you can help me out . please refer to http://stackoverflow.com/questions/33115067/error-recevied-upon-exporting-of-2-gridviews-into-excel – James Boer Oct 14 '15 at 01:14