我的頁(yè)面大概是這樣的:兩個(gè)gridview(一個(gè)用來存放計(jì)算的原始數(shù)據(jù),一個(gè)用來存放結(jié)果數(shù)據(jù),然后把結(jié)果數(shù)據(jù)導(dǎo)出到excel中)用戶添加原始數(shù)據(jù)有兩種方式:一種是在線添加原始數(shù)據(jù),還有一種是通過把txt文件中的數(shù)據(jù)導(dǎo)入到gridview中去,其中第二種方法,我在界面添加了FileUpload 供用戶上傳要導(dǎo)入數(shù)據(jù)的文件,現(xiàn)在遇到的問是:之前沒有加第二種方法的時(shí)候 結(jié)果數(shù)據(jù)導(dǎo)出到excel 完全正常,但是后來加了一個(gè)FileUpload? 以實(shí)現(xiàn)用戶把文件數(shù)據(jù)導(dǎo)入到gridview,但是加了一個(gè)FileUpload? 控件后 結(jié)果數(shù)據(jù)導(dǎo)出到excel 就不正常了,打開導(dǎo)出的excel 提示什么文件丟失,并且導(dǎo)出內(nèi)容還有界面上的控件 ,和之前只導(dǎo)出結(jié)果數(shù)據(jù)完全不同,然后我把FileUpload 屏蔽后,導(dǎo)出的excel又完全正常,現(xiàn)在很是費(fèi)解 請(qǐng)大家?guī)兔?謝謝
#region excel //這段把gridview中的數(shù)據(jù)導(dǎo)出excel的代碼應(yīng)該沒有問題
/// <summary>
/// 導(dǎo)出為Excel
/// </summary>
/// <param name="ctl">控件ID</param>
/// <param name="FileName">文件名</param>
public static void ToExcel(System.Web.UI.Control ctl, string FileName)
{
HttpContext.Current.Response.Charset = "UTF8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-7");
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel"; //image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
#endregion
4 回答

慕斯709654
TA貢獻(xiàn)1840條經(jīng)驗(yàn) 獲得超5個(gè)贊
ctl是什么,這個(gè)控件里面是不是包含了FileUpload控件,如果包含的話,導(dǎo)出時(shí)就會(huì)將該控件也渲染到你的excel中去,嘗試將
FileUpload放到ctl之外,或者導(dǎo)出之前替換ctl中的服務(wù)器控件
public class ExcelHelper { public static void PrepareControlForExport(System.Web.UI.Control control) { for (int i = 0; i < control.Controls.Count; i++) { System.Web.UI.Control current = control.Controls[i]; if (current is LinkButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText)); } else if (current is HyperLink) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text)); } else if (current is CheckBox) { control.Controls.Remove(current); control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } if (current.HasControls()) { PrepareControlForExport(current); } } } }
調(diào)用
ExcelHelper.PrepareControlForExport(GridView1);
就可以替換其中的服務(wù)器控件,方法可能需要你再擴(kuò)充下
另外,頁(yè)面中需要有
public override void VerifyRenderingInServerForm(Control control)
{
}
- 4 回答
- 0 關(guān)注
- 464 瀏覽
添加回答
舉報(bào)
0/150
提交
取消