C# 如何将两个GridView导出到一个MS Excel文件中的两个单独的图纸中?
如何通过单击1按钮将两个gridviews GridView1和GridView2导出到一个MS Excel文件中的两个单独的工作表中?目前,我只能将1个gridview导出到文件名与工作表名称相同的Excel工作表。但我想将两个GridView分成两个单独的图纸,我希望图纸名称由我自己定义/设置。谢谢C# 如何将两个GridView导出到一个MS Excel文件中的两个单独的图纸中?,c#,asp.net,excel,gridview,C#,Asp.net,Excel,Gridview,如何通过单击1按钮将两个gridviews GridView1和GridView2导出到一个MS Excel文件中的两个单独的工作表中?目前,我只能将1个gridview导出到文件名与工作表名称相同的Excel工作表。但我想将两个GridView分成两个单独的图纸,我希望图纸名称由我自己定义/设置。谢谢 public void ExportGridToExcel() { Response.Clear(); Response.Buffer = true; Res
public void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName ="Export"+DateTime.Now+".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
//编辑我的答案,因为只有一个导出按钮: 看看: 首先添加以下名称空间:
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
因此,基本上,您应该拥有或继续使用2个DataTable来绑定到2个GridView:dt1和dt2
创建一个数据集并向其中添加2个数据表
DataSet dataset = new DataSet();
dataset.Tables.Add(dt1);
dataset.Tables.Add(dt2);
然后
您需要修改代码,以便在excel文件中创建工作表,手动将数据从网格复制到excel文件,并将此代码创建为一个函数,该函数采用网格、sheetname和sheetid,如1,2等作为参数,然后在button click事件中调用它两次,一次用于grid1,一次用于grid2,分别使用不同的SheetName和sheet ID 1和2。该函数的代码如下所示 在button click事件中编写这两行代码,然后通过将excel应用程序和工作簿作为参数传递给每个网格调用函数两次
// 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 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].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\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();
}
我建议您使用EP Plus之类的库来执行此操作。您好,只有一个导出按钮。我想将多个gridview导出到一个excel,因此我相信您的代码将通过两次调用导出到两个单独的excel。请告诉我谢谢你。我编辑了我的答案。希望这能解决问题。我已经添加了Microsoft.Offfice.Interop.Excel作为引用,但其中一个DataGridView中仍然存在错误,即下划线缺少类型或命名空间缺少引用错误。请告知,请提供获取错误的确切行以及错误消息Public void ExportToExcelMicrosoft.Office.Interop.Excel.\u应用程序应用程序,Microsoft.Office.Interop.Excel.\u工作簿,DataGridView gridview,字符串SheetName,int sheetid
currentSheet.Name = "your sheet name"
// 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 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].Value.ToString();
}
}
// save the application
workbook.SaveAs("c:\\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();
}