在Excel中导出GridView数据时出现问题C# [英] Trouble while Exporting GridView Data in Excel C#

查看:90
本文介绍了在Excel中导出GridView数据时出现问题C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在excel中导出gridview数据时遇到麻烦。它导出整个页面而不是Gridview数据。



我的代码如下:

  Response.Clear(); 
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset =;
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmlwritter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType =application / ms-excel;
Response.AddHeader(Content-Disposition,string.Format(attachment; filename = {0},DSR+ DateTime.Now.ToString(dd-MM-yyyy)+xls ));
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmlwritter);
Response.Write(strwritter.ToString());
Response.End();


解决方案

此代码可能会帮助您

  protected void btnExportExcel_Click(object sender,EventArgs e)
{
BindData();
GridView1.Visible = true;

string FileName =Deal Report_(+ DateTime.Now.AddHours(5).ToString(yyyy-MM-dd)+).xls;

Response.Clear();
Response.Buffer = true;

Response.AddHeader(content-disposition,
attachment; filename =+ FileName);
Response.Charset =;
Response.ContentType =application / vnd.ms-excel;
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

GridView1.HeaderRow.Style.Add(color,#FFFFFF);
GridView1.HeaderRow.Style.Add(background-color,#1F437D); (int i = 0; i< GridView1.Rows.Count; i ++)



$ GridViewRow row = GridView1.Rows [i];
row.BackColor = System.Drawing.Color.White;
row.Attributes.Add(class,textmode);
if(i%2!= 0)
{
for(int j = 0; j< row.Cells.Count; j ++)
{
/ /row.Cells[j].Style.Add(\" background-color,#eff3f8);
}
}
}
GridView1.RenderControl(hw);
string style = @< style> .textmode {mso-number-format:\ @;}< / style>;
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.End();
GridView1.Visible = false;
}


Having trouble while exporting the gridview data in excel. It is exporting the whole page not Gridview data.

My Code as below :

        Response.Clear();
        Response.Buffer = true;
        Response.ClearContent();
        Response.ClearHeaders();
        Response.Charset = "";
        StringWriter strwritter = new StringWriter();
        HtmlTextWriter htmlwritter = new HtmlTextWriter(strwritter);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/ms-excel";
        Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", "DSR"+DateTime.Now.ToString("dd-MM-yyyy")+".xls"));
        GridView1.GridLines = GridLines.Both;
        GridView1.HeaderStyle.Font.Bold = true;
        GridView1.RenderControl(htmlwritter);
        Response.Write(strwritter.ToString());
        Response.End();     

解决方案

This code may be help you

        protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        BindData();
        GridView1.Visible = true;

        string FileName = "Deal Report_(" + DateTime.Now.AddHours(5).ToString("yyyy-MM-dd") + ").xls";

        Response.Clear();
        Response.Buffer = true;

        Response.AddHeader("content-disposition",
         "attachment;filename=" + FileName);
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        GridView1.HeaderRow.Style.Add("color", "#FFFFFF");
        GridView1.HeaderRow.Style.Add("background-color", "#1F437D");

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            row.BackColor = System.Drawing.Color.White;
            row.Attributes.Add("class", "textmode");
            if (i % 2 != 0)
            {
                for (int j = 0; j < row.Cells.Count; j++)
                {
                    //row.Cells[j].Style.Add("background-color", "#eff3f8");
                }
            }
        }
        GridView1.RenderControl(hw);
        string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        Response.Write(style);
        Response.Output.Write(sw.ToString());
        Response.End();
        GridView1.Visible = false;
    }

这篇关于在Excel中导出GridView数据时出现问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆