Tuesday, July 1, 2014

Datatable Export to excel

aspx page

<asp:Button ID="btnExportToExcel" runat="server" OnClientClick="javascript:setFormSubmitToFalse()"
                                Text="Export to Excel" CssClass="ms-ButtonHeightWidth" OnClick="btnExportToExcel_Click" />

function setFormSubmitToFalse() {
            setTimeout(function () { _spFormOnSubmitCalled = false; }, 3000);
            return true;
        }


cs page

protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                if (gvCouncilReport.HeaderRow != null)
                {
                    for (int i = 0; i < gvCouncilReport.HeaderRow.Cells.Count; i++)
                    {
                        if (string.IsNullOrEmpty(gvCouncilReport.HeaderRow.Cells[i].Text))
                        {
                            LinkButton Link = (LinkButton)gvCouncilReport.HeaderRow.Cells[i].Controls[0];

                            dt.Columns.Add(Link.Text);
                        }
                        else
                        {
                            dt.Columns.Add(gvCouncilReport.HeaderRow.Cells[i].Text);
                        }
                    }
                }
               
                DataTable dtSortTable = (DataTable)ViewState["DataSource"];

                if (dtSortTable != null)
                {
                    if (dtSortTable.Rows.Count > 0)
                    {
                        foreach (DataRow dr in dtSortTable.Rows)
                        {
                            DataRow dataRow;
                            dataRow = dt.NewRow();
                            int i=0;
                            foreach (DataColumn dc in dtSortTable.Columns)
                            {
                                dataRow[i] = Convert.ToString(dr[dc]).Replace("<br/>", "\n").Replace("\n", "\r\n");
                                //dataRow[i] = Convert.ToString(dr[dc]).Replace("<br/>", Environment.NewLine);
                               
                                i++;
                            }
                            dt.Rows.Add(dataRow);
                        }
                       
                    }
                }
               

                Guid gd = Guid.NewGuid();
                string filePath = @"D:\FolderName\" + gd.ToString() + ".xlsx";
                new CreateExcel().ExportToExcel(dt, filePath);

                // var FileByte = File.ReadAllBytes(filePath);
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition",
                                    "attachment; filename=" + gd.ToString() + ".xlsx");
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                Response.WriteFile(filePath, false);
                Response.End();
            }
            catch (Exception ex)
            {
                GRMCommon.LogEntry(ex.Message + "  " + ex.StackTrace);
            }

        }

No comments:

Post a Comment

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...