2013-09-23 40 views
0

不工作我使用的GridView的導出到Excel。在Gridview出現之前,用戶會做一些過濾。我想我的導出按鈕似乎沒有工作,因爲我沒有把我的GridView在頁面加載....出口的GridView的Excel在ASP.NET

繼承人我出口的代碼:

protected void Button2_Click(object sender, EventArgs e) 
    { 
     Response.ClearContent(); 
     Response.AppendHeader("content-disposition", "attachment; filename=logbook.xls"); 
     Response.ContentType = "application/excel"; 

     StringWriter stringWriter = new StringWriter(); 
     HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); 

     GridView1.RenderControl(htmlTextWriter); 
     Response.Write(stringWriter.ToString()); 
     Response.End(); 
    } 

我的GridView代碼:

 protected void btnSearch_Click(object sender, EventArgs e) 
    { 
     BindGridviewData(ddlSystemList.SelectedValue);      
    } 


    private void BindGridviewData(string s) 
    { 

     string ConnectionStringB = ConfigurationManager.ConnectionStrings["connection"].ConnectionString; 
     using (SqlConnection connectionB = new SqlConnection(ConnectionStringB)) 
     { 
      connectionB.Open(); 
      SqlCommand cmd = new SqlCommand(); 

      if (s == "Webloan") 
      { 
       cmd.Connection = connectionB; 
       cmd.CommandText = "Select a.ldatetime as DATE, a.username as USERNAME, a.fullname as FULLNAME, a.description as DESCRIPTION, b.action_name as ACTION from WEBLOAN_SERVER.webloan.dbo.logbook a join WEBLOAN_SERVER.webloan.dbo.action_def b on a.action_no = b.action_no where DATEDIFF(day,ldatetime,@date_exec1) <= 0 and DATEDIFF(day,ldatetime,@date_exec2) >= 0"; 
       cmd.CommandType = CommandType.Text; 
      } 

      if (s == "Saveplus") 
      { 
       cmd.Connection = connectionB; 
       cmd.CommandText = "Select top 10 from WEBLOAN_SERVER.saveplus.dbo.logbook where DATEDIFF(day,ldatetime,@date_exec) = 0"; 
       cmd.CommandType = CommandType.Text; 
      } 

      cmd.Parameters.AddWithValue("@date_exec1", txtDate.Text); 
      cmd.Parameters.AddWithValue("@date_exec2", txtDate2.Text); 

      SqlDataAdapter da = new SqlDataAdapter(cmd); 

      DataSet ds = new DataSet(); 



      da.Fill(ds); 
      GridView1.DataSource = ds;   
      GridView1.DataBind(); 
      connectionB.Close(); 
     } 

    } 

     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) 
    { 
     GridView1.PageIndex = e.NewPageIndex; //assign new page number 
     BindGridviewData(ddlSystemList.SelectedValue); 
     //GridView1.DataBind(); // bind data to gridview 
    } 

我的網頁加載代碼:

 protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 

      txtDate.Text = DateTime.Now.ToString("MM/d/yyyy"); 
      txtDate2.Text = DateTime.Now.ToString("MM/d/yyyy");  

     }  

    } 

我還加了我重寫代碼:

public override void VerifyRenderingInServerForm(Control control) 
    { 
    } 

林在ASP新......我迫切需要幫助就這一個。謝謝

PS:IM使用更新面板和我的生成到Excel按鈕是我更新面板

+0

你做了什麼錯誤..? – SANDEEP

+0

@SANDEEP我沒有得到它只是刷新頁面任何錯誤和它沒有下載任何excel文件panel.have您使用更新面板?如果u使用,然後使用按鈕觸發其生成EXCEL – Bongsky

+1

檢查它不是因爲更新的 – SANDEEP

回答

1

裏面化妝根據您的數據進行必要的更改。

try 
      { 
       DataSet ds = ViewState["Data"] as DataSet; 
       DataView sortedView = (ds.Tables[0]).DefaultView; 
       //DataTable dt = ViewState["Data"].ToString(); 

       //Create a dummy GridView 
       GridView GridView1 = new GridView(); 
       GridView1.AllowPaging = false; 
       GridView1.DataSource = sortedView; 
       GridView1.DataBind(); 
       Response.Clear(); 
       Response.Buffer = true; 
       Response.AddHeader("content-disposition", "attachment;filename=AIDH.xls"); 
       Response.Charset = ""; 
       Response.ContentType = "application/vnd.ms-excel"; 
       StringWriter sw = new StringWriter(); 
       HtmlTextWriter hw = new HtmlTextWriter(sw); 

       for (int i = 0; i < GridView1.Rows.Count; i++) 
       { 
        //Apply text style to each Row 
        GridView1.Rows[i].Attributes.Add("class", "textmode"); 
       } 
       GridView1.Caption = "AIDH Master"; 
       GridView1.RenderControl(hw); 
       //style to format numbers to string 
       //string style = @"<style> .textmode { mso-number-format:\@; } </style>"; 
       //Response.Write(style); 
       Response.Output.Write(sw.ToString()); 
       Response.Flush(); 
       Response.End(); 
      } 
      catch (Exception ex) 
      { 
       Master.ErrorMessage(ex.ToString()); 
      } 
+0

仍然不工作:/ – Bongsky

+0

在你的按鈕單擊事件,您正在使用將網格導出爲ex​​cel。 – Ankur

0

試試下面的代碼生成Excel,在我的情況下,它的工作

if (gv.Rows.Count > 0) 
      { 


       StringWriter tw = new StringWriter(); 
       HtmlTextWriter hw = new HtmlTextWriter(tw); 

       //Get the HTML for the control. 
       gv.RenderControl(hw); 
       //Write the HTML back to the browser. 
       //Response.ContentType = application/vnd.ms-excel; 
       Response.ContentType = "application/vnd.ms-excel"; 
       Response.AppendHeader("Content-Disposition", "attachment; filename=" + strFileName); 
       EnableViewState = false; 
       Response.Write(tw.ToString()); 
       Response.End(); 
      }