將頁面顯示的訂單表導出Excel文件。
步驟
- 定義導出Excel文件的方法:
private void Export(string FileType, string FileName)
{
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename="
+ HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
tb.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control){}
其中tb是Table類型的對象,需要先填好要打印的表的具體內容。
- 對該方法進行調用:
Export("application/ms-excel", "訂單報表.xls");