2013-06-08 5 views
0

Я использую приведенный ниже код для экспорта данных gridview. Это прекрасно работает на странице aspx. Но теперь я создаю страницу ascx и использую тот же код на этой странице ascx. Но это не работает для меня. Пожалуйста, помогите мне сделать это.Экспорт excel с пользовательского контроля

try 
      { 
       DataSet ds = new DataSet(); 
       ds = (DataSet)Session["Datasource"]; 
       GridView grid = new GridView(); 
       Response.ClearContent(); 
       Response.Buffer = true; 
       Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Record.xls")); 
       Response.ContentType = "application/ms-excel"; 
       grid.DataSource = ds; 
       grid.DataBind(); 
       grid.AllowPaging = false; 
       StringWriter sw = new StringWriter(); 
       HtmlTextWriter htw = new HtmlTextWriter(sw); 
       //Change the Header Row back to white color 
       grid.HeaderRow.Style.Add("background-color", "#FFFFFF"); 
       //Applying stlye to gridview header cells 
       for (int i = 0; i < grid.HeaderRow.Cells.Count; i++) 
       { 
        grid.HeaderRow.Cells[i].Style.Add("background-color", "#507CD1"); 
        grid.FooterRow.Cells[i].Style.Add("background-color", "#507CD1"); 
       } 
       int j = 1; 
       //This loop is used to apply stlye to cells based on particular row 
       foreach (GridViewRow gvrow in grid.Rows) 
       { 
        gvrow.BackColor = System.Drawing.Color.White; 
        if (j <= grid.Rows.Count) 
        { 
         if (j % 2 != 0) 
         { 
          for (int k = 0; k < gvrow.Cells.Count; k++) 
          { 
           gvrow.Cells[k].Style.Add("background-color", "#EFF3FB"); 
          } 
         } 
        } 
        j++; 
       } 
       grid.RenderControl(htw); 
       Response.Write(sw.ToString()); 
       Response.End(); 

      } 
      catch (Exception e1) 
      { 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "onload", "<script language='javascript'>alert('" + e1.Message + "');</script>", false); 
      } 
+0

Вы должны использовать iTextSharp чтения http://stackoverflow.com/questions/ 5086097/itextsharp-excel-output и прочитайте эту статью, если вы не хотите использовать iTextSharp http://www.codeproject.com/Questions/255522/Export-gridview-contents-to-Excel – skhurams

+0

еще один элемент управления http: // exporttoexcel .codeplex.com / – skhurams

ответ

0

VerifyRenderingInServerForm является метод Page класса, так что вам нужно, чтобы добавить это к страницу, содержащую UserControl

public override void VerifyRenderingInServerForm(Control control) 
{ 
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET 
server control at run time. */ 
} 
Смежные вопросы