如何在asp.net中将多个Gridview导出到excel中 [英] how to export multiple Gridview into excel in asp.net
本文介绍了如何在asp.net中将多个Gridview导出到excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题将两个Gridview数据导出到Excel工作表
解决方案..?
i have a problem to export the two Gridview data in to Excel sheet
Solution..?
推荐答案
检查这个
导出多个GridViews-to-Excel-SpreadSheet-in-ASP.Net.aspx [ ^ ]
在这里,我得到了解决方案..
public override void VerifyRenderingInServerForm(Control control)
{
/ *验证控件是否呈现* /
}
protected void btnexport_Click(object sender,ImageClickEventArgs e)
{
try
{
Response.Clear();
Response.Bu ffer = true;
Response.AddHeader(content-disposition,
attachment; filename = GridViewExport.xls);
Response.Charset =;
Response.ContentType =application / vnd.ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
if (GridView2.Rows.Count!= 0)
{
PrepareForExport(GridView2);
}
if (GridView4.Rows.Count!= 0)
{
PrepareForExport(GridView4);
}
表tb =新表();
TableRow tr1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Controls.Add(GridView2);
tr1 .Cells.Add(cell1);
TableCell cell3 = new TableCell();
cell3。 Controls.Add(GridView4);
TableCell cell2 = new TableCell();
cell2.Text = ;
//// if(rbPreference.SelectedValue ==2)
// // {
// tr1.Cells.Add(cell2);
// tr1.Cells.Add (cell3);
// tb.Rows.Add(tr1);
/// /}
// //其他
// // {
TableRow tr2 = new TableRow();
tr2.Cells.Add(cell2);
T. ableRow tr3 = new TableRow();
tr3.Cells.Add(cell3);
tb .Rows.Add(tr1);
tb.Rows.Add(tr2);
tb .Rows.Add(tr3);
//}
tb.RenderControl(hw);
//将数字格式化为字符串的样式
string style = @。textmode {mso-number-format:\ @; } ;;
Response.Write(样式);
Response.Output.Write(sw。 ToString());
Response.Flush();
}
catch(exception ex)
{
Response.Write(ex.ToString());
}
终于
{
Response.End();
}
}
protected void PrepareForExport(GridView Gridview)
{
// Gridview.AllowPaging = Convert.ToBoolean( rbPaging.SelectedItem.Value);
GridView2.AllowPaging = false;
GridView4.AllowPaging = false;
btnview_Click1(this,null);
// Gridview.DataBin d();
//将标题行改回白色
Gridview.HeaderRow.Style.Add(background-color,#FFFFFF);
//将样式应用于单个单元格
for(int k = 0; k< Gridview.HeaderRow.Cells.Count; k ++)
{
Gridview.HeaderRow.Cells [k] .Style.Add(background-color,green) ;
}
for(int i = 0; i< Gridview.Rows.Count; i ++)
{
GridViewRow row = Gridview.Rows [我];
//将颜色改回白色
row.BackColor = System.Drawing.Color.White;
//为每一行应用文字样式
row.Attributes.Add(class,textmode);
< br $>
//将样式应用于交替行的单个单元格
if (i%2!= 0)
{
for(int j = 0; j< Gridview.Rows [i]。 Cells.Count; j ++)
{
row.Cells [j] .Style.Add(background-color,#C2D69B);
}
}
}
}
here,i got the solution ..
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
protected void btnexport_Click(object sender, ImageClickEventArgs e)
{
try
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
if (GridView2.Rows.Count!=0)
{
PrepareForExport(GridView2);
}
if (GridView4.Rows.Count!=0)
{
PrepareForExport(GridView4);
}
Table tb = new Table();
TableRow tr1 = new TableRow();
TableCell cell1 = new TableCell();
cell1.Controls.Add(GridView2);
tr1.Cells.Add(cell1);
TableCell cell3 = new TableCell();
cell3.Controls.Add(GridView4);
TableCell cell2 = new TableCell();
cell2.Text = " ";
//// if (rbPreference.SelectedValue == "2")
// // {
// tr1.Cells.Add(cell2);
// tr1.Cells.Add(cell3);
// tb.Rows.Add(tr1);
//// }
// //else
// //{
TableRow tr2 = new TableRow();
tr2.Cells.Add(cell2);
TableRow tr3 = new TableRow();
tr3.Cells.Add(cell3);
tb.Rows.Add(tr1);
tb.Rows.Add(tr2);
tb.Rows.Add(tr3);
//}
tb.RenderControl(hw);
//style to format numbers to string
string style = @" .textmode { mso-number-format:\@; } ";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
finally
{
Response.End();
}
}
protected void PrepareForExport(GridView Gridview)
{
// Gridview.AllowPaging = Convert.ToBoolean(rbPaging.SelectedItem.Value);
GridView2.AllowPaging = false;
GridView4.AllowPaging = false;
btnview_Click1(this, null);
// Gridview.DataBind();
//Change the Header Row back to white color
Gridview.HeaderRow.Style.Add("background-color", "#FFFFFF");
//Apply style to Individual Cells
for (int k = 0; k < Gridview.HeaderRow.Cells.Count; k++)
{
Gridview.HeaderRow.Cells[k].Style.Add("background-color", "green");
}
for (int i = 0; i < Gridview.Rows.Count; i++)
{
GridViewRow row = Gridview.Rows[i];
//Change Color back to white
row.BackColor = System.Drawing.Color.White;
//Apply text style to each Row
row.Attributes.Add("class", "textmode");
//Apply style to Individual Cells of Alternating Row
if (i % 2 != 0)
{
for (int j = 0; j < Gridview.Rows[i].Cells.Count; j++)
{
row.Cells[j].Style.Add("background-color", "#C2D69B");
}
}
}
}
这篇关于如何在asp.net中将多个Gridview导出到excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文