C# 将多个网格视图导出到多个excel选项卡(图纸)
我有6个GridView在我的网站,我需要导出到excel,但每一个在一个不同的工作表 这个链接使用了一些非常类似的东西,但他使用的是数据集,而我不是。我是C语言的新手,所以我无法更改它以适应我的解决方案 我的代码将所有GridView导出到同一工作表。我创建了一个循环来运行我的GridView,现在我需要输入一个代码来为每个excel工作表生成每个循环C# 将多个网格视图导出到多个excel选项卡(图纸),c#,asp.net,excel,gridview,C#,Asp.net,Excel,Gridview,我有6个GridView在我的网站,我需要导出到excel,但每一个在一个不同的工作表 这个链接使用了一些非常类似的东西,但他使用的是数据集,而我不是。我是C语言的新手,所以我无法更改它以适应我的解决方案 我的代码将所有GridView导出到同一工作表。我创建了一个循环来运行我的GridView,现在我需要输入一个代码来为每个excel工作表生成每个循环 protected void btExcel_Click(object sender, EventArgs e) { Response
protected void btExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=FARPOP_Mensal_" + txDt.Text + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("Windows-1252");
Response.Charset = "ISO-8859-1";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView[] gvExcel = new GridView[] { gvAnual, gvPorUF, gvPorFarmacia, gvPorMunicipio, gvPorPV, gvPorCNPJ };
for (int i = 0; i < gvExcel.Length; i++)
{
GridView gv = gvExcel[i];
//
// Need to redirect to each sheet here?
//
gvExcel[i].HeaderRow.BackColor = Color.White;
gvExcel[i].UseAccessibleHeader = false;
gvExcel[i].AllowPaging = false;
for (int x = 0; x < gvExcel[i].Columns.Count; x++)
{
gvExcel[i].Columns[x].Visible = true;
}
gvExcel[i].DataBind();
foreach (TableCell cell in gvExcel[i].HeaderRow.Cells)
{
cell.BackColor = Color.CadetBlue;
cell.Enabled = false;
}
foreach (GridViewRow row in gvExcel[i].Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
cell.Controls.Clear();
if (row.RowIndex % 2 == 0)
{
cell.BackColor = Color.White;
}
else
{
cell.BackColor = Color.LightBlue;
}
cell.CssClass = "textmode";
}
}
gvExcel[i].RenderControl(hw);
}
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
我根据评论找到了一个很好的解决方案
因此,如果您需要使用.NET使用多个GridView来复制工作表,则需要下载并保存
引用dll后,请使用以下代码:
protected void btExcel_Click(object sender, EventArgs e)
{
XLWorkbook wb = new XLWorkbook();
GridView[] gvExcel = new GridView[] { GridView1, GridView2, GridView3, GridView4, GridView5, GridView6};
string[] name = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
gvExcel[i].AllowPaging = false;
gvExcel[i].DataBind();
DataTable dt = new DataTable(name[i].ToString());
for (int z = 0; z < gvExcel[i].Columns.Count; z++)
{
dt.Columns.Add(gvExcel[i].Columns[z].HeaderText);
}
foreach (GridViewRow row in gvExcel[i].Rows)
{
dt.Rows.Add();
for (int c = 0; c < row.Cells.Count; c++)
{
dt.Rows[dt.Rows.Count - 1][c] = row.Cells[c].Text;
}
}
wb.Worksheets.Add(dt);
gvExcel[i].AllowPaging = true;
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Workbook_Name.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
我花了好几天的时间寻找。。。但它工作得很好。
开心点我根据评论找到了一个好的解决方案
因此,如果您需要使用.NET使用多个GridView来复制工作表,则需要下载并保存
引用dll后,请使用以下代码:
protected void btExcel_Click(object sender, EventArgs e)
{
XLWorkbook wb = new XLWorkbook();
GridView[] gvExcel = new GridView[] { GridView1, GridView2, GridView3, GridView4, GridView5, GridView6};
string[] name = new string[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
for (int i = 0; i < gvExcel.Length; i++)
{
if (gvExcel[i].Visible)
{
gvExcel[i].AllowPaging = false;
gvExcel[i].DataBind();
DataTable dt = new DataTable(name[i].ToString());
for (int z = 0; z < gvExcel[i].Columns.Count; z++)
{
dt.Columns.Add(gvExcel[i].Columns[z].HeaderText);
}
foreach (GridViewRow row in gvExcel[i].Rows)
{
dt.Rows.Add();
for (int c = 0; c < row.Cells.Count; c++)
{
dt.Rows[dt.Rows.Count - 1][c] = row.Cells[c].Text;
}
}
wb.Worksheets.Add(dt);
gvExcel[i].AllowPaging = true;
}
}
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;filename=Workbook_Name.xlsx");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
wb.SaveAs(MyMemoryStream);
MyMemoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.End();
}
}
我花了好几天的时间寻找。。。但它工作得很好。
很高兴在你进一步深入这个兔子洞之前,我想向你展示一下微软不在服务器上使用Office Automation的建议:你最好找一个第三方工具来执行Excel导出,或者,您可以将Excel导出作为计划作业运行,稍后根据需要发送给用户。@K1982:发布的代码不使用互操作。我已编辑了您的标题。请看,如果共识是否定的,他们就不应该。@John Saunders:maniak1982是对的。我在以前的代码中使用了互操作。解决方案如下。在您发布的代码中,您在哪里使用了互操作?我只看到了一个GridView。在你进一步深入这个兔子洞之前,我想向你展示一下Microsoft关于不在服务器上使用Office Automation的建议:你最好找一个第三方工具来执行Excel导出,或者,您可以将Excel导出作为计划作业运行,稍后根据需要发送给用户。@K1982:发布的代码不使用互操作。我已编辑了您的标题。请看,如果共识是否定的,他们就不应该。@John Saunders:maniak1982是对的。我在以前的代码中使用了互操作。解决方案如下。在您发布的代码中,您在哪里使用了互操作?我只看到了一个开阔的视野。