我正在尝试将 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>