繁体   English   中英

调用并将两个Gridview导出到两个单独的工作表中

[英]Call and export two Gridviews into two separate worksheets

我想将gridview1gridview2导出到两个单独的工作表中,这些工作表可以在我的代码中命名为一个Excel文件中的grid view 1grid view 2 我在导出到Excel时遇到问题,不确定如何在按钮中调用导出并传递参数以导出两个Gridview:

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

我将代码放在下面的什么地方?

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

您将必须在其中创建这些对象

公共无效的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)   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM