1 1//首先DataGrid必须先要加载数据
2 /// <summary>
3 /// dataGrid 输出到Excel数据并对格式化处理
4 /// 孙二永 20070611
5 /// </summary>
6 /// <param name="sender"></param>
7 /// <param name="e"></param>
8 private void Button1_Click(object sender, System.EventArgs e)
9 {
10 Response.Clear();
11
12 Response.Buffer=true;
13
14 Response.Charset="gb2312";
15
16 Response.AppendHeader("Content-Disposition","filename=FileName.xls"); //添加http头
17
18 Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");//设置输出的流为简体中文
19
20 Response.ContentType="application/msexcel";//设置输出文件类型为excel文件
21
22 this.EnableViewState=false;
23
24 System.Globalization.CultureInfo myCultrue=new System.Globalization.CultureInfo("zh-CN",true);//区域性标识符,语言-国家/地区,zh-CN 代表的是中文 - 中国
25
26 System.IO.StringWriter stringWrite=new System.IO.StringWriter(myCultrue);
27
28 System.Web.UI.HtmlTextWriter html=new HtmlTextWriter(stringWrite);
29
30 this.dataGrid.RenderControl(html);
31
32 Response.Write(stringWrite.ToString());
33
34 Response.End();
35
36 }
37
38
2 /// <summary>
3 /// dataGrid 输出到Excel数据并对格式化处理
4 /// 孙二永 20070611
5 /// </summary>
6 /// <param name="sender"></param>
7 /// <param name="e"></param>
8 private void Button1_Click(object sender, System.EventArgs e)
9 {
10 Response.Clear();
11
12 Response.Buffer=true;
13
14 Response.Charset="gb2312";
15
16 Response.AppendHeader("Content-Disposition","filename=FileName.xls"); //添加http头
17
18 Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312");//设置输出的流为简体中文
19
20 Response.ContentType="application/msexcel";//设置输出文件类型为excel文件
21
22 this.EnableViewState=false;
23
24 System.Globalization.CultureInfo myCultrue=new System.Globalization.CultureInfo("zh-CN",true);//区域性标识符,语言-国家/地区,zh-CN 代表的是中文 - 中国
25
26 System.IO.StringWriter stringWrite=new System.IO.StringWriter(myCultrue);
27
28 System.Web.UI.HtmlTextWriter html=new HtmlTextWriter(stringWrite);
29
30 this.dataGrid.RenderControl(html);
31
32 Response.Write(stringWrite.ToString());
33
34 Response.End();
35
36 }
37
38