Wednesday, October 8, 2014

Export tables, div, Grids and any to Excel and PDF in asp.net c#

Export tables, div, Grids and any to Excel and PDF in asp.net c#

àWhile exporting  these designs with out images it will working fine

à If it having images means you need to give the absolute path for the image URL then only it will work

Check this blog

 Excel Export
protected void imgexcel_Click(object sender, ImageClickEventArgs e)
        {

            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=Suppliers.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            tblrpt.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();

            //Response.Clear();
            //Response.Buffer = true;
            //Response.AddHeader("content-disposition",
            //  "attachment;filename=GridViewExport.doc");
            //Response.Charset = "";
            //Response.ContentType = "application/vnd.ms-word ";
            //StringWriter sw = new StringWriter();
            //HtmlTextWriter hw = new HtmlTextWriter(sw);
            //imgReportHeading.Width = 160;
            //imgReportHeading.Height = 42;
            //tblrpt.RenderControl(hw);
            //Response.Output.Write(sw.ToString());
            //Response.Flush();
            //Response.End();
        }

Pdf Export
protected void imgpdf_Click(object sender, ImageClickEventArgs e)
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition",
                "attachment;filename=GridViewExport.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            tblrpt.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
            HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
            PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
            pdfDoc.Open();
            htmlparser.Parse(sr);
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }


Read More »

How to save the Image to folder from data base in asp.net

How to save the Image to folder from data base in asp.net

1)  By using this method getting the image bytes and save to the folder

public void SaveImageToFolder()
        {
            try
            {
                DataTable dt = new DataTable();
                byte[] byts = null;
                string strcon = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
                SqlConnection connection = new SqlConnection(strcon);
                connection.Open();
                SqlCommand command = new SqlCommand("select COMPANY_LOGO from COMPANYIMAGES where COMPANY_SYSID='" + GPRSTrackSession.CompanyCode + "'", connection);
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(dt);
                if (dt.Rows.Count > 0)
                {
                    Byte[] bytes = (Byte[])dt.Rows[0]["COMPANY_LOGO"];

                    var pathName = "~/Images/rptimgs.jpg";
                    var fileName = Server.MapPath(pathName);

                    FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite);
                    fileStream.Write(bytes, 0, bytes.Length);
                    fileStream.Close();
                }
            }
            catch (Exception ex)
            {

            }

        }

2) And assign that path to the image url in the application
string imgPath = Request.Url.AbsoluteUri.Split('/')[0].ToString() + "//" + Request.Url.AbsoluteUri.Split('/')[2] + "/images/rptimgs.jpg";
                            imgReportHeading.ImageUrl = imgPath;




Read More »