My Blog is my notepad, I want to mark my work anytime, also i can share my harvest with everyone....

Teracy 's space--->

I love BS Develop ,So i am busying as a bee.Do more,Know more,you will get more.....

博客园 首页 新随笔 联系 订阅 管理

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Common;
using System.Data.SqlClient;
using System.IO;


public partial class ExportWE : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(\"server=.;uid=sa;pwd=;database=pubs\");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }
    }
    public override void VerifyRenderingInServerForm(Control control)
    {

    }
    void bind()
    {
        SqlDataAdapter sda = new SqlDataAdapter(\"select * from publishers\", con);

        DataSet ds = new DataSet();

        sda.Fill(ds,\"publishers\");

        GridView1.DataSource = ds;
       
        GridView1.DataBind();
    }
    private void ExportWordOrExcel(string ExportType, GridView gv)
    {
        string filetype = \"\";
        if (ExportType == \"application/vnd.ms-word\")
            filetype = \".doc\";
        if (ExportType == \"application/vnd.ms-excel\")
            filetype = \".xls\";
        Response.Clear();
        //Response.Charset = \"utf-8\";
        Response.Charset = \"GB2312\";
        Response.AppendHeader(\"Content-Disposition\", \"attachment;filename=Pig,Please name\" + filetype);

        Response.ContentType = ExportType;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        gv.AllowPaging = false;
        bind();
        gv.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
        gv.AllowPaging = true;
        bind();


    }
///下面是导出到Word里面,要是做导出到Excel和这个一样的
    protected void ExportWord_Click(object sender, EventArgs e)
    {
        ExportWordOrExcel(\"application/vnd.ms-word\", this.GridView1);
    }
}

posted on 2007-05-03 09:30  Teracy  阅读(668)  评论(0编辑  收藏  举报
One Two Three 向“前”走............
frontpage tracking
Sony Style Coupons