using System.IO;
DataSet dsnew = new DataSet();
string _sqlStr = "select * from test";
using (DBManage _db = new DBManage())
{
dsnew = _db.GetDataSet(_sqlStr);
}
if (dsnew.Tables[0].Rows.Count > 0)
{
if (!GridView2.Visible) { GridView2.Visible = true; }
GridView2.DataSource = dsnew.Tables[0].DefaultView;
GridView2.DataBind();
GridView2.AllowPaging = false;
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
GridView2.RenderControl(htmlWriter);
GridView2.Visible = false;
string filename = "test.xls";
string filestr = Server.MapPath("test") + "//" + filename;
int pos = filestr.LastIndexOf("//");
string file = filestr.Substring(0, pos);
if (!Directory.Exists(file))
{
Directory.CreateDirectory(file);
}
StreamWriter sw = new StreamWriter(filestr);
sw.Write(stringWriter.ToString());
sw.Close();
GridView2.DataSource = null;
GridView2.DataBind();
}