Tuesday, July 3, 2012

Export component art grid to excel

Following code can be used to export component art grid or asp.net grid to excel

 protected void Page_Load(object sender, EventArgs e)
        {
 ExportToExcel(DataTable, Response, "Excel Sheet-" + DateTime.Today.ToString("MM-dd-yyyy"));
 }

 public void ExportToExcel(DataTable dataTable, HttpResponse Response, string FileName)
        {
            Response.Clear();
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            Response.AppendHeader("content-disposition", "attachment; filename=" + FileName + ".xlsx");
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
            GridView gv = new GridView();
            gv.DataSource = dataTable;
            gv.DataBind();
            gv.RenderControl(hw);
            Response.Write(sw.ToString());
            Response.End();
        }

Export component art grid or normal grid to pdf

Code to export component art grid to pdf
 private void Page_Load(System.Object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Grid1.DataSource = buildGrid();
                Grid1.DataBind();
            }
        }

        public System.Data.DataSet buildGrid()
        {
            string conStr = null;
            conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
            conStr += Server.MapPath("~/app_data/demo.mdb");
            System.Data.OleDb.OleDbConnection dbCon = new System.Data.OleDb.OleDbConnection(conStr);
            dbCon.Open();

            string sql = null;
            sql = "SELECT * FROM Posts ORDER BY LastPostDate DESC";
            System.Data.OleDb.OleDbDataAdapter dbAdapter = new System.Data.OleDb.OleDbDataAdapter(sql, dbCon);
            DataSet ds = new DataSet();
            dbAdapter.Fill(ds);

            return ds;
        }

        protected void  btnExport_Click(object sender, System.EventArgs e)
        {
            ExportToExcel(buildGrid(), 0, Response, "MySheet-" + DateTime.Today.ToString("MM-dd-yyyy"));
        }

        public void ExportToExcel(DataSet dSet, int TableIndex, HttpResponse Response, string FileName)
        {
            System.IO.StringWriter sw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
            GridView gv = new GridView();
            gv.DataSource = dSet.Tables[TableIndex];
            gv.HeaderStyle.Font.Size = new FontUnit(4);
            gv.RowStyle.Font.Size = new FontUnit(4);
            gv.DataBind();
            for (int x = 0; x < gv.Columns.Count; x++)
            {
                gv.Columns[x].ItemStyle.Width = Unit.Pixel(100);
            }
            gv.RenderControl(hw);

            Document Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);

            PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf", FileMode.Create));
            HTMLWorker htmlparser = new HTMLWorker(Doc);
            Doc.Open();

            Chunk c = new Chunk("PDF EXPORT OF POSTS \n", FontFactory.GetFont("Verdana", 15));
            Paragraph p = new Paragraph();
            p.Alignment = Element.ALIGN_CENTER;
            p.Add(c);
            Chunk chunk1 = new Chunk("By ComponentArt - www.componentart.com\n\n\n", FontFactory.GetFont("Verdana", 8));
            Paragraph p1 = new Paragraph();
            p1.Alignment = Element.ALIGN_RIGHT;
            p1.Add(chunk1);

            Doc.Add(p);
            Doc.Add(p1);

            System.Xml.XmlTextReader xmlReader = new System.Xml.XmlTextReader(new StringReader(sw.ToString()));
            htmlparser.Parse(new StringReader(sw.ToString()));
            Doc.Close();
            string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\myPDF.pdf";

            ShowPdf(Path);
        }
  private void ShowPdf(string strS)
        {
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + strS);
            Response.TransmitFile(strS);
            Response.End();
            //Response.WriteFile(strS);
            Response.Flush();

            Response.Clear();
        }