如题,我以前1.1的时候用的好好的,现在导出居然导出整个页面,不是我想要的public void DGToExcel(System.Web.UI.Control ctl,string excelName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+excelName+".xls");
HttpContext.Current.Response.Charset ="GB2312";
//HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad) ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void btn_output_Click(object sender, System.EventArgs e)
{
ds = dos.GetDs(sql_hidden.Value.ToString()+" order by e_id desc","");
dg_output.DataSource = ds;
dg_output.DataBind();
withexcel.DGToExcel(dg_output,"设备列表");
}
我要的是web的,不是application,也不是winform的,请教高手
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer=true;
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+excelName+".xls");
HttpContext.Current.Response.Charset ="GB2312";
//HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType ="application/ms-excel";
ctl.Page.EnableViewState =false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad) ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
private void btn_output_Click(object sender, System.EventArgs e)
{
ds = dos.GetDs(sql_hidden.Value.ToString()+" order by e_id desc","");
dg_output.DataSource = ds;
dg_output.DataBind();
withexcel.DGToExcel(dg_output,"设备列表");
}
我要的是web的,不是application,也不是winform的,请教高手
<%@ Page Language="C#" AutoEventWireup="true" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
System.Data.DataSet ds = new System.Data.DataSet();
public void DGToExcel(System.Web.UI.Control ctl, string excelName)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + excelName + ".xls");
HttpContext.Current.Response.Charset = "GB2312";
//HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.Default;
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter tw = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
} public System.Data.DataTable CreateDataTable()
{
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser");
System.Data.DataRow dr;
dataTable1.Columns.Add(new System.Data.DataColumn("Id", typeof(System.Int32)));
dataTable1.Columns.Add(new System.Data.DataColumn("Title", typeof(System.String)));
for (int i = 0; i < 8; i++)
{
dr = dataTable1.NewRow();
dr[0] = i;
dr[1] = Guid.NewGuid().ToString();
dataTable1.Rows.Add(dr);
}
return dataTable1;
}
protected void btn_output_Click(object sender, EventArgs e)
{ dg_output.DataSource = ds;
dg_output.DataBind();
DGToExcel(dg_output, "设备列表");
}
public override void VerifyRenderingInServerForm(Control control)
{
} protected void Page_Load(object sender, EventArgs e)
{
ds.Tables.Add(CreateDataTable());
if (!Page.IsPostBack)
{
dg_output.DataSource = ds;
dg_output.DataBind();
} }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="dg_output" runat="server">
</asp:GridView>
<asp:Button ID="btn_output" runat="server" Text="Button" OnClick="btn_output_Click" />
</form>
</body>
</html>