Asp.NetGridview Excelのエクスポート
18651 ワード
フロントページにGridViewを置くなんて言わないで、注意したいのは
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="ReferPriceIndex.aspx.cs" EnableEventValidation="false"
Inherits="ZTE.Fol.Fund.UI.Web.InsideTrade.ReferPrice.ReferPriceIndex" %>
ラベルにEnableEventValidationプロパティを追加
同時にバックグラウンドに
/// <summary>
/// : VerifyRenderingInServerForm , Excel
/// “…… runat=server ”
/// :
/// asp.net2.0 , ,
/// RenderControl render ,
/// 。
/// </summary>
/// <param name="control"> </param>
public override void VerifyRenderingInServerForm(Control control)
{
}// end VerifyRenderingInServerForm
どちらも追加して、excelをエクスポートするときにページ全体をエクスポートすることを防止できます.
BeadRollListExportExcel.aspx , :
<asp:GridView ID="GridView1" BorderColor="Black" runat="server" AutoGenerateColumns="False"
Font-Size="12px" Width="530px" AllowSorting="True"
OnRowCreated="GridView1_RowCreated">
<Columns>
<asp:TemplateField HeaderText=" " ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<%#(((GridViewRow)Container).DataItemIndex + 1) %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="PrimarySchoolName" HeaderText=" " />
<asp:BoundField DataField="TypeName" HeaderText=" " />
<asp:BoundField DataField="Name" HeaderText=" " />
<asp:BoundField DataField="SexCode" HeaderText=" " />
<asp:BoundField DataField="" HeaderText=" " />
</Columns>
<HeaderStyle BackColor="Azure" Font-Size="12px" HorizontalAlign="Center" />
<RowStyle HorizontalAlign="Center" />
<PagerStyle HorizontalAlign="Center" />
</asp:GridView>
バックグラウンドコード:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetList();
Export("application/ms-excel", string.Format("{0}****.xls", DateTime.Now.ToString("yyyy-MM-dd")));
}
}
public void GetList()
{
string studentType = Request.QueryString["studentType"].ToString();
string schoolCode = Request.QueryString["schoolCode"].ToString();
string district = Request.QueryString["district"].ToString();
string strWhere = " 1=1";
if (!string.IsNullOrEmpty(studentType))
{
strWhere += " and TypeCode='" + studentType + "'";
}
if (!string.IsNullOrEmpty(district))
{
strWhere += " and DistrictCode='" + district + "'";
}
if (!string.IsNullOrEmpty(schoolCode))
{
strWhere += " and PrimarySchoolCode ='" + schoolCode + "'";
}
DataTable dt = new BLL.ObjMethod().GetList("View_ExportExcelStudent", strWhere);
GridView1.DataSource = dt;
GridView1.DataBind();
}
/// <summary>
/// Excel
/// </summary>
/// <param name="FileType"></param>
/// <param name="FileName"></param>
private void Export(string FileType, string FileName)
{
for (int i = 0; i < GridView1.Rows.Count; i++)
{
GridView1.Rows[i].Cells[4].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
}
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlDecode(FileName, Encoding.UTF8).ToString());//HttpUtility.UrlDecode
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
/// <summary>
/// ,
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//
if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);//
TableHeaderCell cell = new TableHeaderCell();
cell.Text = "*****";
cell.ColumnSpan = 23;
rowHeader.Cells.Add(cell);
((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);
}
}