将GridView的数据导入excel的同时加上标题并在标题下加时间段,请高手指点我的代码如下;
最好能给我直接加就能用嘿嘿! protected void Button3_Click(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
最好能给我直接加就能用嘿嘿! protected void Button3_Click(object sender, EventArgs e)
{
string style = @"<style> .text { mso-number-format:\@; } </script> ";
Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
Response.Clear();
Response.Buffer = true; Response.AddHeader( "content-disposition ", "attachment;filename=GiftSale.xls ");
Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312 ");
Response.ContentType = "application/ms-excel ";
this.EnableViewState = true;
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
stringWrite.WriteLine( "****** "); //Excel标题 GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString()); //Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();