1

我正在尝试将 gridview 内容导出到 Excel 中,如下所示,创建了 Excel 但没有内容。但我可以查看网页中的内容。

using System;
using System.IO;
using System.Reflection;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Web.UI;
using System.Web;

namespace CustomReports.Reports
{
public partial class Reports : System.Web.UI.Page
{
    DataTable ResultTable = new DataTable();        
    GridView ResultGrid = new GridView();

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void VerifyRenderingInServerForm(Control control) 
    {
     /* Verifies that the control is rendered */ 
}


    protected void Button1_Click(object sender, EventArgs e)
    {

        DataColumn col1 = new DataColumn("S.No");
        DataColumn col2 = new DataColumn("Workflow Name");
        DataColumn col3 = new DataColumn("Subject Name");
        DataColumn col4 = new DataColumn("Subject ID");

        col1.DataType = System.Type.GetType("System.Int32");
        col2.DataType = System.Type.GetType("System.String");
        col3.DataType = System.Type.GetType("System.String");
        col4.DataType = System.Type.GetType("System.String");

        ResultTable.Columns.Add(col1);
        ResultTable.Columns.Add(col2);
        ResultTable.Columns.Add(col3);
        ResultTable.Columns.Add(col4);

        CoreServiceSession client = new CoreServiceSession();
        SessionAwareCoreServiceClient csClient = client.GetClient();
        var readoption = new ReadOptions();

        ProcessesFilterData filter = new ProcessesFilterData()
        {
            BaseColumns = ListBaseColumns.IdAndTitle,
            ProcessType = ProcessType.Historical

        };

        int i = 1;
        foreach (IdentifiableObjectData data in csClient.GetSystemWideList(filter))
        {
            var processHistory = data as ProcessHistoryData;
            if (processHistory != null)
            {
                DataRow row = ResultTable.NewRow();
                row[col1] = i;
                row[col2] = processHistory.Title;
                foreach (var subjectdetails in processHistory.Subjects)
                {
                    row[col3] = subjectdetails.Title.ToString();
                    row[col4] = subjectdetails.IdRef.ToString();
                }
                ResultTable.Rows.Add(row);
            }
            i++;
        }

        //GridView ResultGrid = new GridView();
        ResultGrid.DataSource = ResultTable;
        ResultGrid.DataBind();
        Panel1.Controls.Add(ResultGrid);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ExporttoExcel(ResultGrid, "Reports");
    }


    protected void ExporttoExcel(GridView objGridView, string FileName)
    {
        string attachment = "attachment; filename=" + FileName + ".xls";
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", attachment);
        HttpContext.Current.Response.ContentType = "application/ms-excel";
        StringWriter objStringWriter = new StringWriter();
        HtmlTextWriter objHtmlTextWriter = new HtmlTextWriter(objStringWriter);
        objGridView.RenderControl(objHtmlTextWriter);
        HttpContext.Current.Response.Write(objStringWriter.ToString());
        HttpContext.Current.Response.End();


    }






}

}

Excel没有从gridview导入内容,它是空白的

ASPX 代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Reports.aspx.cs" Inherits="CustomReports.Reports.Reports" EnableEventValidation="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">    
<title>Custom Report Page</title>
</head>
<body>
 <form id="form1" runat="server">
     <div>
            <table id="table1" runat="server">
                <tr>
                    <td>
                        <asp:Label ID="CustomReports1" runat="server" Text="Custom Reports" 
                        Font-Bold="True"></asp:Label>
                    </td>
                </tr>

                <tr>
                    <td>
                        <asp:Label ID="SelectReportType2" runat="server" Text="Select Report Type"></asp:Label>
                    </td>

                    <td>
                        <asp:DropDownList ID="DDReportTypes" runat="server" Height="25px" Width="193px" onselectedindexchanged="DDReportTypes_SelectedIndexChanged" AutoPostBack = "true" ></asp:DropDownList>
                    </td>
                </tr>
            </table>


            <table id="dynamictable" runat="server">
            </table>  


            <table id="buttons">
                <tr>
                    <td> 
                        <asp:Button ID="Button1" runat="server" Text="Search" onclick="Button1_Click" />
                    </td>

                    <td> 
                        <asp:Button ID="Button2" runat="server" Text="Export" onclick="Button2_Click" />
                    </td>

                    <td> 
                        <asp:Button ID="Button3" runat="server" Text="GetValues" onclick="Button3_Click" />
                    </td>           
                </tr>
            </table>



            <asp:TextBox ID="TextBox0" runat="server" Height="28px" Width="355px"></asp:TextBox> 

            <asp:Panel ID="Panel1" runat="server">             
                  <asp:GridView ID="ResultGrid" runat="server">
                  </asp:GridView>           
            </asp:Panel>

    </div> 

</form>

</body>
</html>
4

3 回答 3

2

你不需要表格。只需渲染网格:

ResultGrid.RenderControl(htw);

但是,您需要添加此代码,因为通常网格必须在表单中

public override void VerifyRenderingInServerForm(Control control) 
{       
}
于 2012-08-13T08:19:01.470 回答
0

尝试使用此代码导出...

    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=fooreport.xls");
    Response.Charset = "";
    Response.ContentType = "application/ms-excel";
    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
    lstEarningReport.RenderControl(htmlWrite);
    Response.Write("<table border='1' >  ");
    Response.Write(stringWrite.ToString());
    Response.Write("</table>");
    Response.End();
于 2012-08-13T08:19:11.000 回答
0

这是一个老问题,但是当我遇到同样的问题时遇到了。EnableViewState在我的情况下,在 gridview 上设置为 false。将其设置回 true 可以解决问题。我还在导出它之前对gridview进行了数据绑定,也将其删除。

于 2016-02-04T12:54:12.467 回答