C#自定义导出Excel
//导出Excel
protected void ibtExcel_Click(object sender, ImageClickEventArgs e)
{
decimal s1 = 0;
decimal s2 = 0;
decimal s3 = 0;
decimal s4 = 0;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gv = new GridView();
gv = GridView1;
DataSet ds = DbHelperSQL.Query(Session["dt"].ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
decimal d1 = decimal.Parse(dr[1].ToString());
decimal d2 = decimal.Parse(dr[2].ToString());
decimal d3 = decimal.Parse(dr[3].ToString());
decimal d4 = decimal.Parse(dr[4].ToString());
s1 += d1;
s2 += d2;
s3 += d3;
s4 += d4;
gv.DataKeyNames = new string[] { "dw" };
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
gv.DataBind();
gv.RenderControl(htmlWrite);
StringBuilder sb = new StringBuilder();
sb.Append("<div><table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" AllowCustomPaging=\"True\" border=\"1\" id=\"tbd\" style=\"background-color:White;border-color:#ECE9D8;border-width:1px;border-style:None;width:100%;border-collapse:collapse\"><tr><td colspan=\"6\" style=\"font-size:15px;font-weight:bolder; text-align:center\">合同信息查看</td></tr><tr><td>审定日期</td><td>" + tbFromDate.Value +"到"+ tbToDate.Value + "</td><td>合同类型</td><td>" + ddlType.SelectedValue + "</td><td>卖方性质</td><td>" + ddlPG_Attr.SelectedValue + "</td></tr><tr><td>知识产权类型</td><td>" + ddlPact_PR.SelectedValue + "</td><td>买方国别</td><td>" + ddlCountry.SelectedValue + "</td><td>买方地区</td><td>" + tbBs_Name.Value + "</td></tr><tr><td>买方性质</td><td>" + ddlBuyer_Attr.SelectedValue + "</td><td>技术领域</td><td>" + ddlPact_TD.SelectedValue + "</td><td></td><td></td></tr></table>")
sb.Append(htmlWrite.InnerWriter.ToString().Replace("<div>", "").Replace("</table>", "").Replace("</div>", ""));
sb.Append("<tr><td align=\"center\" colspan=\"2\">合计</td><td align=\"center\">" + s1.ToString() + "</td><td align=\"center\">" + s2.ToString() + "</td><td align=\"center\">" + s3.ToString() + "</td><td align=\"center\">" + s4.ToString() + "</td></tr></table></tr></div>");
Response.Write(sb.ToString());
Response.End();
gv.DataBind();
}
protected void ibtExcel_Click(object sender, ImageClickEventArgs e)
{
decimal s1 = 0;
decimal s2 = 0;
decimal s3 = 0;
decimal s4 = 0;
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView gv = new GridView();
gv = GridView1;
DataSet ds = DbHelperSQL.Query(Session["dt"].ToString());
foreach (DataRow dr in ds.Tables[0].Rows)
{
decimal d1 = decimal.Parse(dr[1].ToString());
decimal d2 = decimal.Parse(dr[2].ToString());
decimal d3 = decimal.Parse(dr[3].ToString());
decimal d4 = decimal.Parse(dr[4].ToString());
s1 += d1;
s2 += d2;
s3 += d3;
s4 += d4;
gv.DataKeyNames = new string[] { "dw" };
gv.DataSource = ds.Tables[0].DefaultView;
gv.DataBind();
}
gv.DataBind();
gv.RenderControl(htmlWrite);
StringBuilder sb = new StringBuilder();
sb.Append("<div><table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" AllowCustomPaging=\"True\" border=\"1\" id=\"tbd\" style=\"background-color:White;border-color:#ECE9D8;border-width:1px;border-style:None;width:100%;border-collapse:collapse\"><tr><td colspan=\"6\" style=\"font-size:15px;font-weight:bolder; text-align:center\">合同信息查看</td></tr><tr><td>审定日期</td><td>" + tbFromDate.Value +"到"+ tbToDate.Value + "</td><td>合同类型</td><td>" + ddlType.SelectedValue + "</td><td>卖方性质</td><td>" + ddlPG_Attr.SelectedValue + "</td></tr><tr><td>知识产权类型</td><td>" + ddlPact_PR.SelectedValue + "</td><td>买方国别</td><td>" + ddlCountry.SelectedValue + "</td><td>买方地区</td><td>" + tbBs_Name.Value + "</td></tr><tr><td>买方性质</td><td>" + ddlBuyer_Attr.SelectedValue + "</td><td>技术领域</td><td>" + ddlPact_TD.SelectedValue + "</td><td></td><td></td></tr></table>")
sb.Append(htmlWrite.InnerWriter.ToString().Replace("<div>", "").Replace("</table>", "").Replace("</div>", ""));
sb.Append("<tr><td align=\"center\" colspan=\"2\">合计</td><td align=\"center\">" + s1.ToString() + "</td><td align=\"center\">" + s2.ToString() + "</td><td align=\"center\">" + s3.ToString() + "</td><td align=\"center\">" + s4.ToString() + "</td></tr></table></tr></div>");
Response.Write(sb.ToString());
Response.End();
gv.DataBind();
}
效果图:
![](https://images.cnblogs.com/cnblogs_com/lhws/QQ截图20120312230053.png)