一個將datagrid控件內容輸出到Excel文件的demo.
生成EXCEL文件#region 生成EXCEL文件
público vazio setToExcel()
{
// Response.Clear();
// Response.Buffer= verdadeiro;
// Response.Charset="utf-8";
if (eformsn.Text=="")
{
DataGrid1.Visible=falso;
Label1.Text="要匯入EXCEL,請先輸入申請單號!";
Label1.Visible=true;
}
outro
{
dl.DataSource=DB("consulta", "selecione * de msi_eol_list_line onde eformsn="+eformsn.Text+" ordenar por eformsn").Tables[0].DefaultView;
dl.DataBind();
//下面這行很重要,anexo 參數表示作為附件下載,您可以改成online在線打開
nome do
arquivo=FileFlow.xls Disposição","anexo;nome do arquivo=EOL_" +eformsn.Text+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
//Response.ContentType.
Response.ContentType = "aplicativo/ms-excel";
dl.EnableViewState = falso;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = novo System.Web.UI.HtmlTextWriter(oStringWriter);
//將目標數據綁定到輸入流輸出
//this 表示輸出本頁,你也可以綁定datagrid,或其他支持obj.RenderControl()屬性的控件
dl.RenderControl(oHtmlTextWriter);
//this.DataBind.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
#endregion