1
public void OutPutExcel()
2
{
3
GridView gv = new GridView();
4
DataTable dt = BuCustomers.GetAll().Tables[0];
5
this.GridView1.DataSource = dt;
6
this.GridView1.DataBind();
7
//定义文档类型、字符编码
8
Response.Clear();
9
Response.Buffer = true;
10
Response.Charset = "GB2312";
11
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
12
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
13
Response.AppendHeader("Content-Disposition", "attachment;filename=FileFlow.xls");
14
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
15
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
16
Response.ContentType = "application/ms-excel";
17
this.EnableViewState = false;
18
// 定义一个输入流
19
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
20
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
21
22
gv.RenderControl(oHtmlTextWriter);
23
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
24
Response.Write(oStringWriter.ToString());
25
Response.End();
26
}
27
public override void VerifyRenderingInServerForm(Control control)
28
{
29
}
30
private void exporttable()
31
{
32
DataTable dt = BuCustomers.GetAll().Tables[0];
33
34
//定义文档类型、字符编码
35
Response.Clear();
36
Response.Buffer = true;
37
Response.Charset = "GB2312";
38
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
39
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
40
Response.AppendHeader("Content-Disposition", "attachment;filename=FileFlow.xls");
41
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
42
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
43
Response.ContentType = "application/ms-excel";
44
this.EnableViewState = false;
45
46
47
StringWriter stringWriter = new StringWriter();
48
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
49
DataGrid excel = new DataGrid();
50
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
51
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
52
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
53
AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
54
headerStyle.BackColor = System.Drawing.Color.LightGray;
55
headerStyle.Font.Bold = true;
56
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
57
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;
58
59
excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
60
excel.HeaderStyle.MergeWith(headerStyle);
61
excel.ItemStyle.MergeWith(itemStyle);
62
excel.GridLines = GridLines.Both;
63
excel.HeaderStyle.Font.Bold = true;
64
excel.DataSource = dt.DefaultView; //输出DataTable的内容
65
excel.DataBind();
66
excel.RenderControl(htmlWriter);
67
68
//string filestr = "d://data//" + filePath; //filePath是文件的路径
69
//int pos = filestr.LastIndexOf("//");
70
//string file = filestr.Substring(0, pos);
71
//if (!Directory.Exists(file))
72
//{
73
// Directory.CreateDirectory(file);
74
//}
75
//System.IO.StreamWriter sw = new StreamWriter(filestr);
76
//sw.Write(stringWriter.ToString());
77
//sw.Close();
78
79
Response.Write(stringWriter.ToString());
80
Response.End();
81
}
public void OutPutExcel()2
{3
GridView gv = new GridView();4
DataTable dt = BuCustomers.GetAll().Tables[0];5
this.GridView1.DataSource = dt;6
this.GridView1.DataBind();7
//定义文档类型、字符编码8
Response.Clear();9
Response.Buffer = true;10
Response.Charset = "GB2312";11
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开12
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm13
Response.AppendHeader("Content-Disposition", "attachment;filename=FileFlow.xls");14
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");15
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档16
Response.ContentType = "application/ms-excel";17
this.EnableViewState = false;18
// 定义一个输入流19
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();20
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);21

22
gv.RenderControl(oHtmlTextWriter);23
//this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件24
Response.Write(oStringWriter.ToString());25
Response.End();26
}27
public override void VerifyRenderingInServerForm(Control control)28
{29
}30
private void exporttable()31
{32
DataTable dt = BuCustomers.GetAll().Tables[0];33

34
//定义文档类型、字符编码35
Response.Clear();36
Response.Buffer = true;37
Response.Charset = "GB2312";38
//下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开39
//filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm40
Response.AppendHeader("Content-Disposition", "attachment;filename=FileFlow.xls");41
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");42
//Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档43
Response.ContentType = "application/ms-excel";44
this.EnableViewState = false;45

46

47
StringWriter stringWriter = new StringWriter();48
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);49
DataGrid excel = new DataGrid();50
System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();51
System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();52
System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();53
AlternatingStyle.BackColor = System.Drawing.Color.LightGray;54
headerStyle.BackColor = System.Drawing.Color.LightGray;55
headerStyle.Font.Bold = true;56
headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;57
itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;58

59
excel.AlternatingItemStyle.MergeWith(AlternatingStyle);60
excel.HeaderStyle.MergeWith(headerStyle);61
excel.ItemStyle.MergeWith(itemStyle);62
excel.GridLines = GridLines.Both;63
excel.HeaderStyle.Font.Bold = true;64
excel.DataSource = dt.DefaultView; //输出DataTable的内容65
excel.DataBind();66
excel.RenderControl(htmlWriter);67

68
//string filestr = "d://data//" + filePath; //filePath是文件的路径69
//int pos = filestr.LastIndexOf("//");70
//string file = filestr.Substring(0, pos);71
//if (!Directory.Exists(file))72
//{73
// Directory.CreateDirectory(file);74
//}75
//System.IO.StreamWriter sw = new StreamWriter(filestr);76
//sw.Write(stringWriter.ToString());77
//sw.Close();78

79
Response.Write(stringWriter.ToString());80
Response.End();81
}

浙公网安备 33010602011771号