2012-11-26 57 views
0

我想導出DataTable或DataSet的數據,格式如Word,Excel和PDF格式中的頁眉頁腳顏色,字體尺寸,行顏色。可能嗎?使用格式化將數據導出到Excel,Word和PDF

如果是的話那怎麼樣?請幫助我。 我的代碼如下。

public void ExportToExcel(DataTable dt) 
{ 
     con.Open(); 
     string sql = "select *from test"; 
     cmd = new SqlCommand(sql, con); 
     dt = new DataTable(); 
     da = new SqlDataAdapter(cmd); 
     da.Fill(dt); 
     GridView1.DataSource = dt; 
     GridView1.DataBind(); 
     cmd.Dispose(); 

     string filename = "DownloadTest.xls"; 
     System.IO.StringWriter tw = new System.IO.StringWriter(); 
     System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); 
     DataGrid dgGrid = new DataGrid(); 
     dgGrid.DataSource = dt; 
     dgGrid.DataBind(); 
     DataSet ds = new DataSet(); 
     ds = dt.Clone; 

     dgGrid.RenderControl(hw); 

     Response.ContentType = "application/vnd.ms-excel"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); 
     this.EnableViewState = false; 
     Response.Write(tw.ToString()); 
     Response.End(); 


     con.Close(); 
} 

回答