【问题标题】:Export only Gridview Data to Excel format in Asp.net C#在 Asp.net C# 中仅将 Gridview 数据导出为 Excel 格式
【发布时间】:2016-05-09 10:05:33
【问题描述】:

在我的页面中,我有两个文本框控件,其中我从日历扩展器中选择日期,在导出到 excel 按钮中我要将 Gridview 数据导出到 excel 表。 当我对 excel 表进行门控时,它会显示我从中导出 excel 表的文本框和按钮。

我已经在导出按钮中编写了导出代码。 喜欢:-

    protected void Export_to_Excel_Click(object sender, EventArgs e)
    {
        try
        {

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Grd_MidData.AllowPaging = false;
            bindgriddata();
            //Change the Header Row back to white color
            Grd_MidData.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++)
            {
                Grd_MidData.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            Grd_MidData.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.Flush();
        }
        catch (Exception ee)
        {

        }
    }

当我对 excel 表进行门控时,它会显示 gridview 数据以及两个文本框和我进行过滤的按钮。

所以任何人都建议我,如何只显示 gridview 数据而不显示 Excel 工作表上的文本框和按钮。

【问题讨论】:

  • 几乎所有的答案都在这里写一个(HtmlTextWriter)字符串或有互操作代码。两者都不要使用。这将导致您稍后在 DateTime 和 Decimal 格式方面出现问题。 Excel 也会发出警告,因为您生成的不是“真正的”Excel 文件,而是扩展名为 .xls 的 HTML 页面。开始使用专门的库来创建 Excel 文件,例如 EPPlusExample herehere.

标签: c# asp.net excel gridview


【解决方案1】:

您必须将数据绑定到您的 Excel 代码, 使用下面的代码

    bindgriddata();    
    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
    Response.ContentType = "application/ms-excel";
    StringWriter sw = new StringWriter();
    HtmlTextWriter ht = new HtmlTextWriter(sw);
    Grd_MidData.RenderControl(ht);
    Response.Write(sw.ToString());
    Response.End();

它对我有用。

【讨论】:

  • 谢谢#Rajesh。我用过这段代码。我所做的只有一项更改是在 Response.End();我使用了 Response.Flush();。但问题仍然存在。当我下载 excel 表时,它会显示 Gridview 数据以及文本框和按钮。就像我们打印屏幕一样,它显示了整页数据......那么我怎样才能从工作表中删除那 2 个文本框和按钮。
【解决方案2】:

这里 gridview 数据与 From date: To Date: 文本框和按钮一起显示。那么如何从工作表中删除这些字段。

【讨论】:

  • 您好,这是因为您在代码中使用了 RenderControl,通过此属性数据呈现在 Excel 工作表中。
【解决方案3】:

您可以使用 close xml 生成 excel,它将提供更多功能来创建 excel 和格式。

protected void Export_to_Excel_Click(object sender, EventArgs e)
{
    bindgriddata();
    Grd_MidData.DataSource = objDS;  // Dataset
    Grd_MidData.DataBind();

    using (XLWorkbook wb = new XLWorkbook())
    {
        try
        {
            //creating worksheet
            var ws = wb.Worksheets.Add("Report");

            //adding columms header
            int columnscount = objDS.Tables[0].Columns.Count;
            char a = 'A';
            for (int j = 1; j <= columnscount; j++)
            {
                string str = a + "1";
                ws.Cell(str).Value = objDS.Tables[0].Columns[j - 1].ColumnName.ToString();
                ws.Cell(str).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                a++;
            }
            ws.Columns().AdjustToContents();

            //formatting columns header 
            var rngheaders = ws.Range("A1:J1");
            rngheaders.FirstRow().Style
                .Font.SetBold()
                .Font.SetFontSize(12)
                .Font.SetFontColor(XLColor.Black)
                .Fill.SetBackgroundColor(XLColor.DeepSkyBlue)
                .Alignment.SetHorizontal(XLAlignmentHorizontalValues.Center)
                .Border.OutsideBorder = XLBorderStyleValues.Thin;


            ////adding data to excel
            int k = 2;
            foreach (DataRow row in objDS.Tables[0].Rows)
            {
                char b = 'A';
                string str = b + "" + k;
                for (int i = 0; i < objDS.Tables[0].Columns.Count; i++)
                {
                    ws.Cell(str).Value = row[i].ToString();
                    ws.Cell(str).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
                    ws.Cell(str).Style.Alignment.SetHorizontal(XLAlignmentHorizontalValues.Left);
                    b++;
                    str = b + "" + k;
                }
                k++;
            }

            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "";
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
            Response.AddHeader("content-disposition", "attachment;filename=Customer.xlsx");
        }
        catch { }

        using (MemoryStream MyMemoryStream = new MemoryStream())
        {
            wb.SaveAs(MyMemoryStream);
            MyMemoryStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
        }
    }

}

【讨论】:

    【解决方案4】:

    我在导出 GridView 时遇到了同样的问题。我添加了一个 try/catch 并将消息导出到 Excel,然后意识到它正在生成错误。我通过以下答案解决了这个问题:GridView must be placed inside a form tag with runat="server" even after the GridView is within a form tag

    【讨论】:

    • 你能把这个答案的要点融入这个场景吗?如果此问题重复,您可以标记该问题并将其标记为重复问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2013-08-05
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多