我正在將GridView導出到Excel文件,但是當我打開 該文件時,首先出現有關 格式類型和擴展名不匹配的錯誤,以及當我打開它 整個頁面被帶入Excel文件,而不僅僅是網格視圖。將GridView導出到Excel導出整個頁面
我沒有使用更新面板。我試圖與GridView控件內按鈕和外 和相同的結果,所以現在看來,這可能是從代碼隱藏一些東西, 看起來是這樣的:
Response.Clear();
Response.Buffer = true;
string filename = "GridViewExport_" + DateTime.Now.ToString() + ".xls";
Response.AddHeader("content-disposition",
"attachment;filename=" + filename);
Response.Charset = String.Empty;
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView3.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
什麼是strHeader? – coredump
請參閱我的編輯。 – jomsk1e