Sunday 22 December 2013

Export To Excel in SharePoint 2010

SharePoint postback to work after clicking on export to excel button
<asp:ImageButton ID="imagbtnExportToExcel" runat="server" 
                                ImageUrl="~/_layouts/images/CrewRelegationTracker/Excel.PNG" Width="35px" 
                                onclick="imagbtnExportToExcel_Click"  />



  protected void Page_Load(object sender, EventArgs e)
        {
            imagbtnExportToExcel.OnClientClick = "_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;";
}
  OR

You can use below script code if above code not working for postback to work after clicking on export to excel in sharepoint 2010

<script type="text/javascript" language="javascript">
    //sharepoint postback to work after clicking on export to excel button
    if (typeof (_spBodyOnLoadFunctionNames) != 'undefined' &&
                                           _spBodyOnLoadFunctionNames != null) {
        _spBodyOnLoadFunctionNames.push("supressSubmitWraper");
    }
    function supressSubmitWraper() {
        _spSuppressFormOnSubmitWrapper = true;
    }
</script>



  protected void imagbtnExportToExcel_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                DataTable dtExportData = new DataTable();
                var btnIDFlag = ((ImageButton)sender).ID;
                if (btnIDFlag == "imagbtnExportToExcel")
                    dtExportData = (DataTable)ViewState["PageIndexData"];
                else
                    dtExportData = (DataTable)ViewState["FinalCrewEntitlmentDetails"];
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = true;
                HttpContext.Current.Response.ContentType = "application/ms-excel";
                HttpContext.Current.Response.Write(@"<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"">");

                if (btnIDFlag == "imagbtnExportToExcel")

                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=CrewRelegationTracker.xls");
                else
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=CrewRelegationTracker.xls");

                HttpContext.Current.Response.Charset = "utf-8";
                HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
                HttpContext.Current.Response.Write("<font style='font-size:10.0pt; font-family:Calibri;'>");
                HttpContext.Current.Response.Write("<BR><BR><BR>");
                HttpContext.Current.Response.Write("<Table border='1' bgColor='#ffffff' " +
                  "borderColor='#000000' cellSpacing='0' cellPadding='0' " +
                  "style='font-size:10.0pt; font-family:Calibri; background:white;'>");

                int columnscount = dtExportData.Columns.Count;
                HttpContext.Current.Response.Write("<TR>");
                for (int j = 0; j < columnscount; j++)
                {
                    HttpContext.Current.Response.Write("<Td style='background-color:#99CCCC'>");
                    HttpContext.Current.Response.Write("<B>" + dtExportData.Columns[j].ColumnName + "</B>");
                    HttpContext.Current.Response.Write("</Td>");
                }
                HttpContext.Current.Response.Write("</TR>");
                foreach (DataRow row in dtExportData.Rows)
                {
                    HttpContext.Current.Response.Write("<TR>");
                    for (int i = 0; i < dtExportData.Columns.Count; i++)
                    {
                        HttpContext.Current.Response.Write("<Td>");
                        HttpContext.Current.Response.Write(row[i].ToString());
                        HttpContext.Current.Response.Write("</Td>");
                    }
                    HttpContext.Current.Response.Write("</TR>");
                }
                HttpContext.Current.Response.Write("</Table>");
                HttpContext.Current.Response.Write("</font>");
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
            catch (Exception ex)
            {
                objLogger.LogExceptions(ex, "btnSearch_Click");
                //Page.ClientScript.RegisterStartupScript(this.GetType(), "refresh", "ErrorMessage();", true);
            }
        }

No comments:

Post a Comment