如何在一個MS Excel文件中通過單擊1按鈕將2個網格視圖(GridView1 & GridView2)導出到2張獨立的工作表中?目前,我只能將一個gridview導出到一個Excel工作表,其名稱與工作表名稱相同。但是我想將2個gridview分成2個單獨的表格,我希望表單名稱可以由我自己定義/設置。由於如何在一個MS Excel文件中將2個gridview導出爲2張單獨的工作表?
public void ExportGridToExcel()
{
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
string FileName ="Export"+DateTime.Now+".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);
GridView1.GridLines = GridLines.Both;
GridView1.HeaderStyle.Font.Bold = true;
GridView1.RenderControl(htmltextwrtter);
Response.Write(strwritter.ToString());
Response.End();
}
我建議你使用EP Plus(http://epplus.codeplex.com/)等庫來做到這一點。 – navigator