62,046
社区成员
发帖
与我相关
我的任务
分享
string fileName = HttpUtility.UrlEncode("Excel文件名为中文哦.xls");
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
private void DataSetToExcel(string FileName,DataSet ds)
{
string style = "<style>td{mso-number-format:\"\\@\";}</style>";//防止导出excel时将以0开头的全数字数据的0去掉
//GridView gridview = new GridView();
//gridview.DataSource = dst;
//gridview.DataBind();
DateTime now = System.DateTime.Now;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.ContentType = "application/vnd.ms-xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
DataGrid dg = new DataGrid();
dg.DataSource = ds;
dg.DataBind();
dg.RenderControl(htmlWrite);
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(now.ToString() + ".xls"));
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
}