Never give up - LEO

人 只有在合适的地方 才能体现出最大的价值
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

Asp.net 输出 Excel

Posted on 2007-10-17 11:35  lizhiwen  阅读(524)  评论(0编辑  收藏  举报
 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    }