using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Reflection;
/// <summary>
///
/// </summary>
public class GridViewExportUtil
{
    /// <summary>
    ///
    /// </summary>
    /// <param name="fileName"></param>
    /// <param name="gv"></param>

      static Hashtable htControls = new Hashtable();

    public static void ExportGridView(string fileName, GridView gv)
    {
        htControls.Clear();
        htControls.Add("LinkButton", "Text");
        htControls.Add("HyperLink", "Text");
        htControls.Add("DropDownList", "SelectedItem");
        htControls.Add("CheckBox", "Checked");

        gv = PrepareGridViewForExport(gv) as GridView;
        HttpContext.Current.Response.ClearContent();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        gv.RenderControl(htw);
       
        //string style = @"<style> .textmode { mso-number-format:\@; } </style>";
        //HttpContext.Current.Response.Write(style);

        HttpContext.Current.Response.Output.Write(sw.ToString());
        HttpContext.Current.Response.End();
    }

    public static Control PrepareGridViewForExport(Control control)
    {
        /*Literal l = new Literal();

        for (int i = 0; i < gv.Controls.Count; i++)
        {
            if ((null != htControls[gv.Controls[i].GetType().Name]) || (null != htControls[gv.Controls[i].GetType

                ().BaseType.Name]))
            {

                l.Text = GetControlPropertyValue(gv.Controls[i]);
                gv.Controls.Remove(gv.Controls[i]);
                gv.Controls.AddAt(i, l);
            }

            if (gv.Controls[i].HasControls())
            {
                PrepareGridViewForExport(gv.Controls[i]);
            }
        }*/
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
        return control;
    }

    public static string GetControlPropertyValue(Control control)
    {

        Type controlType = control.GetType();
        string strControlType = controlType.Name;
        string strReturn = "Error";
        bool bReturn;

        PropertyInfo[] ctrlProps = controlType.GetProperties();
        string ExcelPropertyName = (string)htControls[strControlType];

        if (ExcelPropertyName == null)
        {
            ExcelPropertyName = (string)htControls[control.GetType().BaseType.Name];
            if (ExcelPropertyName == null)
                return strReturn;
        }

        foreach (PropertyInfo ctrlProp in ctrlProps)
        {
            if (ctrlProp.Name == ExcelPropertyName &&
            ctrlProp.PropertyType == typeof(String))
            {
                try
                {
                    strReturn = (string)ctrlProp.GetValue(control, null);
                    break;
                }
                catch
                {
                    strReturn = "";
                }
            }

            if (ctrlProp.Name == ExcelPropertyName &&
            ctrlProp.PropertyType == typeof(bool))
            {
                try
                {
                    bReturn = (bool)ctrlProp.GetValue(control, null);
                    strReturn = bReturn ? "True" : "False";
                    break;
                }
                catch
                {
                    strReturn = "Error";
                }
            }

            if (ctrlProp.Name == ExcelPropertyName &&

            ctrlProp.PropertyType == typeof(ListItem))
            {
                try
                {
                    strReturn = ((ListItem)(ctrlProp.GetValue(control, null))).Text;
                    break;
                }
                catch
                {
                    strReturn = "";
                }
            }
        }
        return strReturn;
    }

    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;


            
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);

                }
               
                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
    public static void Exportsum(string fileName, GridView gv, int KS, int FV, int VC, int VE, int VEC, int VFC, int TD)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;


                TableRow trFirst = new TableRow();
                trFirst = PrepareHeaderForExport(KS,FV,VC,VE,VEC,VFC,TD);
                table.Rows.Add(trFirst);

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);

                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }
    public static void Exportsumn(string fileName, GridView gv, int KS, int VC, int VE, int VEC, int TD,string fdate,string tdate,int rtype)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;

                TableRow trFirst1 = new TableRow();
                TableCell tc = new TableCell();
                tc.Text = "Viking Report from " + fdate + " to " + tdate;
                trFirst1.Cells.Add(tc);
                tc.HorizontalAlign = HorizontalAlign.Left;
                tc.Font.Bold = true;
                tc.ColumnSpan = 5;
                table.Rows.Add(trFirst1);

                TableRow trFirst2 = new TableRow();
                TableCell tc1 = new TableCell();
                tc1.Text = "Report Generated on:" + DateTime.Now.ToString("MMM-dd-yyyy");
                trFirst2.Cells.Add(tc1);
                tc1.HorizontalAlign = HorizontalAlign.Left;
                tc1.Font.Bold = true;
                tc1.ColumnSpan = 5;
                table.Rows.Add(trFirst2);

               
                TableRow trFirst = new TableRow();
                trFirst = PrepareHeaderForExportn(KS, VC, VE, VEC, TD,rtype);
                table.Rows.Add(trFirst);

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    gv.HeaderRow.BackColor = System.Drawing.Color.LightGray;

                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

public static void Exportsumn(string fileName, GridView gv, int KS, int TD, string fdate, string tdate, int rtype)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;

                TableRow trFirst1 = new TableRow();
                TableCell tc = new TableCell();
                tc.Text = "Viking Report from " + fdate + " to " + tdate;
                trFirst1.Cells.Add(tc);
                tc.HorizontalAlign = HorizontalAlign.Left;
                tc.Font.Bold = true;
                tc.ColumnSpan = 5;
                table.Rows.Add(trFirst1);

                TableRow trFirst2 = new TableRow();
                TableCell tc1 = new TableCell();
                tc1.Text = "Report Generated on:" + DateTime.Now.ToString("MMM-dd-yyyy");
                trFirst2.Cells.Add(tc1);
                tc1.HorizontalAlign = HorizontalAlign.Left;
                tc1.Font.Bold = true;
                tc1.ColumnSpan = 5;
                table.Rows.Add(trFirst2);


                TableRow trFirst = new TableRow();
                trFirst = PrepareHeaderForExportn(KS,TD, rtype);
                table.Rows.Add(trFirst);

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    gv.HeaderRow.BackColor = System.Drawing.Color.LightGray;

                    table.Rows.Add(gv.HeaderRow);
                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    public static void ExportDiv(string fileName, GridView gv, HtmlControl div)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;
                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);

                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                table.RenderControl(htw);
                div.RenderControl(htw);
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

public static void ExportDiv(string fileName,HtmlControl div)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        div.RenderControl(htw);

        HttpContext.Current.Response.Output.Write(sw.ToString());
        HttpContext.Current.Response.End();

       
    }

    public static void ExportGR(string fileName, GridView gv,GridViewRow gr)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader(
            "content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                //  Create a form to contain the grid
                Table table = new Table();

                table.GridLines = gv.GridLines;
              

                //  add the header row to the table
                if (gv.HeaderRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);

                }

                //  add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    GridViewExportUtil.PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                //  add the footer row to the table
                if (gv.FooterRow != null)
                {
                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                //  render the table into the htmlwriter
                gr.RenderControl(htw);
                table.RenderControl(htw);
               
                //  render the htmlwriter into the response
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

    private static TableRow PrepareHeaderForExport(int KS, int FV, int VC, int VE, int VEC, int VFC, int TD)
    {
        TableRow control = new TableRow();
        TableCell tc = new TableCell();
        tc.Text = "#";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.Text = "Customer";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.Text = "Project";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = KS;
        tc.Text = "KeyStrokes";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = FV;
        tc.Text = "Field Visited";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VC;
        tc.Text = "Valid Characters";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VE;
        tc.Text = "Verify Character";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VEC;
        tc.Text = "Character Change";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VFC;
        tc.Text = "Field Change";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "Time Taken (Seconds)";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = 1;
        tc.Text = "Total Characters";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "KS / Hr";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

        return control;
    }

    private static TableRow PrepareHeaderForExportn(int KS, int VC, int VE, int VEC, int TD,int RTYPE)
    {
       

        TableRow control = new TableRow();
        TableCell tc = new TableCell();
        tc.Text = "Location";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        tc = new TableCell();
        tc.Text = "Customer";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        if (RTYPE==2)
        {
        tc = new TableCell();
        tc.Text = "UserId";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        }
        tc = new TableCell();
        tc.Text = "Project";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        tc = new TableCell();
        tc.Text = "Exported";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
       
        tc = new TableCell();
        tc.ColumnSpan = KS;
        tc.Text = "KeyStrokes";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
      
        tc = new TableCell();
        tc.ColumnSpan = VC;
        tc.Text = "Valid Characters";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VE;
        tc.Text = "Verify Character";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VEC;
        tc.Text = "Character Change";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
     
        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "Time Taken (Seconds)";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
       
        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "KS / Hr";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

        return control;
       
    }

  private static TableRow PrepareHeaderForExportn(int KS, int TD, int RTYPE)
    {


        TableRow control = new TableRow();
        TableCell tc = new TableCell();
        tc.Text = "Location";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        tc = new TableCell();
        tc.Text = "Customer";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        if (RTYPE == 2)
        {
            tc = new TableCell();
            tc.Text = "UserId";
            control.Cells.Add(tc);
            tc.HorizontalAlign = HorizontalAlign.Center;
            tc.BackColor = System.Drawing.Color.LightGray;
            tc.Font.Bold = true;
            tc.RowSpan = 2;
        }
        tc = new TableCell();
        tc.Text = "Project";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc.RowSpan = 2;
        tc = new TableCell();
        tc.Text = "Exported";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

        tc = new TableCell();
        tc.ColumnSpan = KS;
        tc.Text = "KeyStrokes";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

       /* tc = new TableCell();
        tc.ColumnSpan = VC;
        tc.Text = "Valid Characters";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VE;
        tc.Text = "Verify Character";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;
        tc = new TableCell();
        tc.ColumnSpan = VEC;
        tc.Text = "Character Change";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true; */

        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "Time Taken (Seconds)";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

        tc = new TableCell();
        tc.ColumnSpan = TD;
        tc.Text = "KS / Hr";
        control.Cells.Add(tc);
        tc.HorizontalAlign = HorizontalAlign.Center;
        tc.BackColor = System.Drawing.Color.LightGray;
        tc.Font.Bold = true;

        return control;

    }

    /// <summary>
    /// Replace any of the contained controls with literals
    /// </summary>
    /// <param name="control"></param>
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                GridViewExportUtil.PrepareControlForExport(current);
            }
        }
    }
}

Comments (1)

On 6 January 2014 at 02:08 , Anonymous said...

export gridview data to excel in c#