一個將datagrid控件內容輸出到excel文件的demo.html
生成EXCEL文件#region 生成EXCEL文件
public void setToExcel()
{
// Response.Clear();
// Response.Buffer= true;
// Response.Charset="utf-8";
if (eformsn.Text=="")
{
DataGrid1.Visible=false;
Label1.Text="要匯入EXCEL,請先輸入申請單號!";
Label1.Visible=true;
}
else
{
dl.DataSource=DB("query", "select * from msi_eol_list_line where eformsn="+eformsn.Text+" order by eformsn").Tables[0].DefaultView;
dl.DataBind();app
//下面這行很重要,attachment 參數表示做為附件下載,您能夠改爲online在線打開
//filename= http://www.xzqdds.com ).xls 指定輸出文件的名稱,注意其擴展名和指定文件類型相符,能夠為:.doc .xls .txt .htm this
Response.AppendHeader("Content-Disposition","attachment;filename=EOL_"+eformsn.Text+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); spa
//Response.ContentType指定文件類型 能夠為application/ms-excel,application/ms-word ,application/ms-txt,application/ms-html或其餘瀏覽器可直接支持文檔
Response.ContentType = "application/ms-excel";
dl.EnableViewState = false; excel
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);orm
//將目 淘寶熱賣標數據綁定到輸入流輸出
//this 表示輸出本頁,你也能夠綁定datagrid,或其餘支持obj.RenderControl()屬性的控件
// http://www.89900.com
dl.RenderControl(oHtmlTextWriter);
//this.DataBind.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
#endregionhtm