2012-11-26 93 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(); 
} 

回答

相关问题