Created
July 15, 2013 10:57
-
-
Save RockyMyx/5999147 to your computer and use it in GitHub Desktop.
C#: easy-grid-to-excel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void OnExportClick(object sender, EventArgs e) | |
{ | |
Response.ClearContent(); | |
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls"); | |
Response.ContentType = "application/excel"; | |
StringWriter sw = new StringWriter(); | |
HtmlTextWriter htw = new HtmlTextWriter(sw); | |
gridView.RenderControl(htw); | |
//解决数字导出时可能会出现截断的情况,如001变成1,需配合RowDataBound事件使用 | |
string style = @"<style> .text { mso-number-format:\@; } </script> "; | |
Response.Write(style); | |
Response.Write(sw.ToString()); | |
Response.End(); | |
} | |
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e) | |
{ | |
if (e.Row.RowType == DataControlRowType.DataRow) | |
{ | |
e.Row.Cells[1].Attributes.Add("class", "text"); | |
} | |
} | |
//类型“GridView”的控件“ctl00_contentPlaceHolder_gridView”必须放在具有 runat=server 的窗体标记内。 | |
public override void VerifyRenderingInServerForm(Control control) | |
{ | |
} | |
//导出分页GridView | |
//修改页文件可以修正这个问题:EnableEventValidation = "false". | |
<%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment