天天看点

GridView中的数据导出到Excel方法(包含处理模板列)

??? 浠?澶╁???寸??涓?绡????ㄧ????绔?锛?瀵逛?GridView涓??版????瀵煎?虹??澶?????棰??????舵??浠???ridView琛ㄦ?间腑?剧ず??涓?浠?浠???浠??版??搴?涓?璇诲?虹???硷?杩???瑕???涓?涓?浜?浜轰负澶?????妯℃?垮??锛?姣?濡????ゆ??????涓??????╂???hypelinker绛?锛????ㄥ?煎?哄??锛???浠?寰?寰?涓???瑕?杩?浜??т欢?剧ず????涓?甯???浠ユ?т欢??褰㈠??ユ?剧ず锛?????浠ユ?版????褰㈠??ュ?绀恒??杩?灏遍??瑕??ㄥ?煎?烘?朵?涓?浜?澶???锛?????浜?涓?浜?浜???缃?涓???淇℃??锛??寸??濡?涓?锛? ? 涓???瀵煎??utton

?? protected void export_Click(object sender, EventArgs e)

??? {

??????? if (this.MachineList.Rows.Count == 0)

??????? {

??????????? Response.Write("<script>alert(&apos;娌℃???ユ?惧?版?版??,??娉?瀵煎?猴?&apos;)</script>");

??????? }

??? ????else

??????? {

??????????? this.MachineList.AllowPaging = false; // 灏?????椤电??GridView涓????版???ㄩ?ㄥ?煎?哄??xcel

??????????? Bind();

??????????? Export("application/ms-excel", "璁惧?淇℃??.xls");

??????????? // ?㈡?? export("application/ms-word", "璁惧?淇℃??.doc"); ?d?瀵煎?虹??灏辨??Word?煎???浜?.

? ??????????this.MachineList.AllowPaging = true;

??????????? Bind();

??????? }???????

??? }

?

浜???瀵煎?轰富?芥??

?public void Export(string FileType, string FileName)

??? {

??????? string style = @"<style>.text{mso-number-format:@}</script>";//瀵煎?ュ??xcel??淇?瀛?琛ㄩ???板????涓????㈠???ㄧ?? 0 .

?????

??PrepareGridViewForExport(MachineList);

//灏?妯$?????剧ず?烘??

??????? Response.Clear();

??????? Response.Charset = "GB2312";

??????? Response.ContentEncoding = Encoding.UTF7;

??????? Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());

??????? Response.ContentType = FileType;

??????? this.EnableViewState = false;

??????? this.MachineList.AllowPaging = false;

??????? System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);

??????? StringWriter sw = new StringWriter();

??????? HtmlTextWriter htw = new HtmlTextWriter(sw);

??????? this.MachineList.RenderControl(htw);

??????? Response.Write(style);

??????? Response.Write(sw.ToString());

??????? //Response.Write(dt.ToString());

??????? Response.End();

??? }

?

??? /// <summary>

??? /// ????VerifyRenderingInServerForm(Control control)?规?,浠ョ‘淇??ㄧ?搴?杩?琛?????瀹???GridView?т欢?绘??浣?浜?"<form runat="server"></form>"??璁板??.

??? /// 璇ユ?规??ㄦ?ョ‘璁ゅ?ㄨ?琛??朵负??瀹???ASP.NET???″?ㄦ?т欢????lt;form runat="server">??璁?

??? ///???? 蹇?椤讳?浜?<form runat="server">涓????т欢??浠ュ?ㄥ???颁???璋??ㄦ?ゆ?规?,浠ヤ究?ㄦ?т欢琚?缃?浜???璁板??舵?剧ず??璇?淇℃??;???,????????渚?璧?浜?娉ㄥ????????????

??? /// ?т欢搴?璇ュ??ontrol.Render()?规???????涓?璋??ㄦ?ゆ?规?.

??? ///???? 濡?????????浣跨?ㄥ?㈡?风???????????″?ㄦ?т欢娌℃????????lt;form runat="server"> ??璁颁腑,瀹?浠?灏???娉?姝e父宸ヤ?,杩?????浠ラ??杩?????VerifyRenderingInServerForm(

??? /// Control control)?规?浣跨?ㄧ?搴?姝e父杩?琛?.

??? /// </summary>

??? /// <param name="control"></param>

??? public override void VerifyRenderingInServerForm(Control control)

??? {

??????? // Confirms that an HtmlForm control is rendered for

??????? //the specified ASP.NET server control at run time.

}

?

涓???濡???GridView瀛??ㄦā?垮??锛??朵腑????瀛??т欢锛?渚?濡?CheckBox绛?锛?瀵煎??XCEL??灏变??虹?拌?ュ?哄????涓?瑙?寰?????浠ヨ?瀵规ā?垮??????澶???锛?杞?杞斤?

public? void PrepareGridViewForExport(Control gv)//妯″????规????绱? flashcong

??? {

?

??????? LinkButton lb = new LinkButton();

??????? Literal l = new Literal();

?? ?????string name = String.Empty;

?

??????? for (int i = 0; i < gv.Controls.Count; i++)

??????? {

???????????

??????????? if (gv.Controls[i].GetType() == typeof(LinkButton))

??????????? {

?

??????????????? l.Text = (gv.Controls[i] as LinkButton).Text;

????? ??????????gv.Controls.Remove(gv.Controls[i]);

??????????????? gv.Controls.AddAt(i, l);

?

??????????? }

?

??????????? else if (gv.Controls[i].GetType() == typeof(DropDownList))

??????????? {

?

??????????????? l.Text = (gv.Controls[i] as DropDownList).SelectedItem.Text;

??????????????? gv.Controls.Remove(gv.Controls[i]);

??????????????? gv.Controls.AddAt(i, l);

?

??????????? }

?

??????????? else if (gv.Controls[i].GetType() == typeof(CheckBox))

??????????? {

?

??????????????? l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";

??????????????? gv.Controls.Remove(gv.Controls[i]);

??????????????? gv.Controls.AddAt(i, l);

?

??????????? }

?

??????????? else if (gv.Controls[i].GetType() == typeof(ImageButton))

??????????? {

??????????????? l.Text = "?剧??";

???? ???????????gv.Controls.Remove(gv.Controls[i]);

??????????????? gv.Controls.AddAt(i, l);

??????????? }

?

??????????? if (gv.Controls[i].HasControls())

??????????? {

??????????????? PrepareGridViewForExport(gv.Controls[i]);

??????????? }

??????? }

}

?

??? 娉??瀵逛?妯℃?垮????澶???杩???涓??规?????碉?????ridView浣跨?ㄦ?讹??ㄤ?<a></a>?ㄥ?㈡?风????寮?瀵硅??妗????规????ㄥ?煎?烘?讹?杩?涓???涓???妯℃?垮???т欢锛?浣?瀵煎?哄??Excel涓?浼??剧ず??涓???绾跨???炬?ワ???浠ユ?冲?绘??瀹???

?充?寰?澶??规?锛?涔?娌℃?????????ユ????濂界?ㄤ?涓?寰?浠ョ???规?锛?澶???涓?????????锛??ㄦ?ユ?剧ず?炬?ョ??????锛?瀵煎?哄?????ㄥ?????????规?瑙e?炽??

濡??????村ソ??澶????规?锛?涔?璇峰??浣?????锛?

?

?

?