当前位置:编程学习 > C#/ASP.NET >>

gridview导出到Excel如何加上Excel的表格格式

以下是我的代码,Gridview的ID是GVManage,导出后并没有Excel表格的样式,而且还有页面其它控件也被导出来了
请知道的朋友帮帮忙解决一下
/// <summary>
    /// 此为必须加的
    /// </summary>
    /// <param name="control"></param>
    public override void VerifyRenderingInServerForm(Control control)
    { }

    protected void btnExport_Click(object sender, EventArgs e)
    {
        string fileName = "export";
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.GVManage.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();

    }
--------------------编程问答-------------------- 帮顶,沉了 --------------------编程问答-------------------- 这种流写出然后控制样式没用过,可以尝试一下javascript中的导出,可以通过vba语法控制样式

如:
 ExWSh.Application.Selection.Interior.ColorIndex=34;//设置填充背景色 浅蓝色
 ExWSh.Application.Selection.Borders.ColorIndex=1;//设置边框颜色 黑色
 ExWSh.Application.Selection.Font.Size=11;//设置字体大小 11 --------------------编程问答-------------------- 这种  System.Web.UI.HtmlTextWriter往外写,并附带颜色 等样式的,有方案的同僚 也推一下 大家学习学习 --------------------编程问答--------------------
GridView 中的数据导出  Excel :

protected void Page_Load(object sender, EventArgs e)
{
 if (!Page.IsPostBack)
 {
  BindData();
 }
}
private void BindData()
{
 string query = "SELECT * FROM customers";
 SqlConnection myConnection = new SqlConnection(ConnectionString);
 SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
 DataSet ds = new DataSet();
 ad.Fill(ds, "customers");
 GridView1.DataSource = ds;
 GridView1.DataBind();
}


//这句话必须,否则出现异常:类型“GridView”的控件“XXXX”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
 // Confirms that an HtmlForm control is rendered for
}

protected void Button1_Click(object sender, EventArgs e)
{
 Response.Clear();
 Response.AddHeader("content-disposition","attachment;filename=FileName.xls");
 Response.Charset = "gb2312";
 Response.ContentType = "application/vnd.xls";
 System.IO.StringWriter stringWrite = new System.IO.StringWriter();
 System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);

 GridView1.AllowPaging = false;
 BindData();
 GridView1.RenderControl(htmlWrite);

 Response.Write(stringWrite.ToString());
 Response.End();
 GridView1.AllowPaging = true;
 BindData();
}
protected void paging(object sender,GridViewPageEventArgs e)
{
 GridView1.PageIndex = e.NewPageIndex;
 BindData();
--------------------编程问答--------------------
public void OutPutExcel()
        {
            //定义文档类型、字符编码
            Response.Clear();
          Response.Buffer= true;
          Response.Charset="GB2312";
            //下面这行很重要, attachment 参数表示作为附件下载,您可以改成 online在线打开
          //filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
          Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
          Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
          //Response.ContentType指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
          Response.ContentType = "application/ms-excel";
          this.EnableViewState = false;
            // 定义一个输入流
          System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

            this.RenderControl(oHtmlTextWriter);
          //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
          Response.Write(oStringWriter.ToString());
          Response.End();
        }
--------------------编程问答--------------------
protected void Button1_Click(object sender, EventArgs e)
    {
       ExcelOut(this.GridView1);

    }
    public void ExcelOut(GridView gv)
    {
        if (gv.Rows.Count > 0)
        {
            Response.Clear();
            Response.ClearContent();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("_yyyyMMdd_HHmmss") + ".xls");
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            gv.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            Response.Write("没有数据");
        }
    }


    public override void VerifyRenderingInServerForm(Control control)
    {
        //base.VerifyRenderingInServerForm(control);
    }
//此处是样式
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex < 3) //索引从0开始。
            {
                e.Row.BackColor = System.Drawing.Color.Red; //Color.FromName("#FF0000");
            }
        }
    }
--------------------编程问答-------------------- 忘说了,前台用数据源绑定 --------------------编程问答-------------------- 看看这个  NPOI   http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html --------------------编程问答-------------------- 我是这样做的:
导出到写定格式的EXCEL模板上中,再打开(下载),
不过合并单元格不知如何处理
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,