將datagrid控件內容輸出到excel文件

来源:互联网 发布:win7部分软件乱码 编辑:程序博客网 时间:2024/06/14 15:48
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
一個將datagrid控件容輸出到excel文件的demo.

生成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();

//下面這行很重要,attachment 參數表示作為附件下載,您可以改成online在線打開
//filename=FileFlow.xls 指定輸出文件的名稱,注意其擴展名和指定文件類型相符,可以為:.doc .xls .txt .htm  

Response.AppendHeader("Content-Disposition","attachment;filename=EOL_" eformsn.Text ".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8"); 

//Response.ContentType指定文件類型 可以為application/ms-excel,application/ms-word ,application/ms-txt,application/ms-html或其他瀏覽器可直接支持文檔 
Response.ContentType = "application/ms-excel";
dl.EnableViewState = false; 

System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

//將目標數據綁定到輸入流輸出  
//this 表示輸出本頁,你也可以綁定datagrid,或其他支持obj.RenderControl()屬性的控件  
dl.RenderControl(oHtmlTextWriter);
//this.DataBind.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
}
#endregion

http://floatping.cnblogs.com/archive/2006/07/03/441769.html

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>