1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
protected void btnExcel_click(object sender, EventArgs e)
{
if (Session["datatable"] != null)
dt = (DataTable)Session["datatable"];
dt.DefaultView.Sort = "niveau1 ASC,niveau2 ASC, niveau3 ASC, niveau4 ASC";
dt = dt.DefaultView.ToTable("dt");
try
{
string strFileName = String.Empty;
string strFilePath = String.Empty;
strFilePath = Server.MapPath("~/Excel/") + "Structure.xls";
if (File.Exists(strFilePath))
{
File.Delete(strFilePath);
}
System.IO.StringWriter oStringWriter = new StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
StreamWriter objStreamWriter;
//string strStyle =@" .text { mso-number-format:\@; }";
objStreamWriter = File.AppendText(strFilePath);
GridViewStructure.DataSource = dt;
GridViewStructure.DataBind();
GridViewStructure.RenderControl(oHtmlTextWriter);
objStreamWriter.WriteLine(oStringWriter.ToString());
objStreamWriter.Close();
string strScript = "window.open('./Excel/Structure.xls');";
if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "clientScript"))
ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", strScript, true);
}
catch (Exception)
{
//Handle Exception
}
GridViewStructure.Visible = false;
} |