我想以 Word、Excel 和 PDF 格式的页眉颜色、字体大小、行颜色等格式导出 DataTable 或 DataSet 的数据。是否可以?
如果是,那怎么办?请医治我。我的代码如下。
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();
}