ponveni

ponveni

  • NA
  • 29
  • 18k

how to export Multiple GridView into multiple worksheets?

Feb 5 2014 2:59 AM
 

      I am able to export the data in one excel scheet horizontal way using this below code.but i want to export the data same excel scheet but 4 different Tab.(like tab1,tab2,tab3,tab4 need to display grid) 

 protected void PrepareForExport(GridView Gridview)

        {

 

           Gridview.DataBind();

           Gridview.HeaderRow.Style.Add("background-color", "Black");

           for (int k = 0; k < Gridview.HeaderRow.Cells.Count; k++)

           {

 

               Gridview.HeaderRow.Cells[k].Style.Add("background-color", "green");

 

           }

            for (int i = 0; i < Gridview.Rows.Count; i++)

            {

                GridViewRow row = Gridview.Rows[i];

                row.BackColor = System.Drawing.Color.White;

                row.Attributes.Add("class", "textmode");

                if (i % 2 != 0)

                {

                    for (int j = 0; j < Gridview.Rows[i].Cells.Count; j++)

                    {

 

                        row.Cells[j].Style.Add("background-color", "#C2D69B");

 

                    }

 

                }

 

            }

 

        }

      

 

public override void VerifyRenderingInServerForm(Control control)

        {

 

        }

     protected void btnExport_Click(object sender, EventArgs e)

        {

            Response.ClearContent();

            Response.Buffer = true;

            string attachment = "attachment; filename=Metrics.xls";

            Response.ClearContent();

            Response.AddHeader("content-disposition", attachment);

           Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType = "application/vnd.ms-excel";

            Response.Charset = "";

            StringWriter sw = new StringWriter();

            HtmlTextWriter htw = new HtmlTextWriter(sw);

            string tmpChartName = "test2.jpg";

            string imgPath = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName;

            ResponseMinsHistogram.SaveImage(imgPath);

            string imgPath2 = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName);

            PrepareForExport(GvAgTicket);

            PrepareForExport(GvAgent);

            PrepareForExport(Gvresolve);

            string headerTable = @"<Table><tr><td><br><img src='" + imgPath2 + @"' \></br></td></tr></Table>";

            Table tb = new Table();

            TableRow NextLine = new TableRow();

            TableRow SecondLine1 = new TableRow();

            //graPh

            TableCell Graph = new TableCell();

            Graph.Controls.Add(new TableCell());

            NextLine.Cells.Add(Graph);

            NextLine.Cells[0].Width = 10;

            NextLine.Cells[0].HorizontalAlign = HorizontalAlign.Left;

            tb.Rows.Add(NextLine);

            NextLine = new TableRow();

            NextLine.Cells.Add(new TableCell());

            NextLine.Cells[0].Controls.Add(new Literal { Text = headerTable });

            TableCell SecondNext1 = new TableCell();

            SecondNext1.Text = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";

            SecondLine1.Cells.Add(SecondNext1);

            //resolve Grid

            TableCell Resolve = new TableCell();

            Resolve.Controls.Add(Gvresolve);

            NextLine.Cells.Add(Resolve);

            NextLine.Cells[0].HorizontalAlign = HorizontalAlign.Right;

            // AgentBind Grid

            TableCell Agendbind = new TableCell();

            Agendbind.Controls.Add(GvAgent);

            SecondLine1.Cells.Add(Agendbind);

            SecondLine1.Cells[0].HorizontalAlign = HorizontalAlign.Left;

            //AgentTicket

            TableCell AgentTicket = new TableCell();

            AgentTicket.Controls.Add(GvAgTicket);

            SecondLine1.Cells.Add(AgentTicket);

            SecondLine1.Cells[0].HorizontalAlign = HorizontalAlign.Right;

            //Cell Next Line

            TableCell CellNextLine = new TableCell();

            CellNextLine.Text = " ";

            TableCell NextSecond = new TableCell();

            NextSecond.Text = " ";

            TableCell SecondNext = new TableCell();

            SecondNext.Text = "<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>";

            NextLine.Cells.Add(CellNextLine);

            NextLine.Cells.Add(SecondNext);

            NextLine.Cells.Add(Resolve);

            SecondLine1.Cells.Add(Agendbind);

            SecondLine1.Cells.Add(NextSecond);

            SecondLine1.Cells.Add(AgentTicket);

              //add data into table

            tb.Rows.Add(NextLine);

            tb.Rows.Add(SecondLine1);

            tb.RenderControl(htw);

 

            DBConnect dbcon = new DBConnect();

            List<OutageDetails> listDetails = new List<OutageDetails>();

 

            DateTime fromDate = DateTime.Today.AddDays(-1);

            DateTime toDate = DateTime.Today;

            if (!string.IsNullOrWhiteSpace(txtFrom.Text) && !string.IsNullOrWhiteSpace(txtTo.Text))

            {

                fromDate = Convert.ToDateTime(txtFrom.Text);

                toDate = Convert.ToDateTime(txtTo.Text);

            }

 

            if (rdoSI.Checked)

                listDetails = dbcon.GetIPCData(TicketType.SI, fromDate, toDate);

            else if (rdoNonSI.Checked)

                listDetails = dbcon.GetIPCData(TicketType.NonSI, fromDate, toDate);

            else

                listDetails = dbcon.GetIPCData(TicketType.Both, fromDate, toDate);

            Response.Write(sw.ToString());

            Response.End();

        }

 

 

 

    }

 

 

}