using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HaiTianTechEDWebReference;
using System.Data;
public partial class ExportToExcel : System.Web.UI.Page
{
    DataHelpService db = new DataHelpService();
  
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataSet ds = db.GetCustomerQuestion(1);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GetExcel();
    }
    //导出到Excel
    private void GetExcel()
    {
        string title = "2011海天技术大会现场问答";
        title = System.Web.HttpUtility.UrlEncode(title.ToString());
        ////获取数据集  
        DataSet ds = db.GetCustomerQuestion(1);
        DataView dv = ds.Tables[0].DefaultView;
        DataTable dt = dv.Table;
        System.IO.StringWriter sw = new System.IO.StringWriter(); //创建对象
        sw.WriteLine("2011海天技术大会现场问答");  //输入标题 
        sw.WriteLine("手机号\t问题");//输入字段
        //输入表格内容
        foreach (DataRow dr in dt.Rows)
        {
            sw.WriteLine(dr["QusMobile"].ToString().Trim() + "\t" + dr["Qustion"].ToString().Trim());
        }
        sw.Close(); //关闭数据流
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
    }
}