调用并导出两个gridview到两个单独的工作表中
本文关键字:两个 工作 单独 调用 gridview | 更新日期: 2023-09-27 18:17:17
我想将gridview1
和gridview2
导出到两个单独的工作表中,可以在我的代码中命名为grid view 1
和grid view 2
在一个Excel文件中。我有问题导出到Excel,我不知道如何调用我的导出按钮,并通过参数导出两个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();
}
我把下面的代码放在哪里?
// 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);
您必须在
中创建这些对象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)
}