??? 浠?澶╁???寸??涓?绡????ㄧ????绔?锛?瀵逛?GridView涓??版????瀵煎?虹??澶?????棰??????舵??浠???ridView琛ㄦ?間腑?劇ず??涓?浠?浠???浠??版??搴?涓?璇誨?虹???鹼?杩???瑕???涓?涓?浜?浜轟負澶?????妯℃?垮??锛?姣?濡????ゆ??????涓??????╂???hypelinker绛?锛????ㄥ?煎?哄??锛???浠?寰?寰?涓???瑕?杩?浜??т歡?劇ず????涓?甯???浠ユ?т歡??褰㈠??ユ?劇ず锛?????浠ユ?版????褰㈠??ュ?绀恒??杩?灏遍??瑕??ㄥ?煎?烘?朵?涓?浜?澶???锛?????浜?涓?浜?浜???缃?涓???淇℃??锛??寸??濡?涓?锛? ? 涓???瀵煎??utton
?? protected void export_Click(object sender, EventArgs e)
??? {
??????? if (this.MachineList.Rows.Count == 0)
??????? {
??????????? Response.Write("<script>alert('娌℃???ユ?懼?版?版??,??娉?瀵煎?猴?')</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?熾??
濡??????村ソ??澶????規?锛?涔?璇峰??浣?????锛?
?
?
?