using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.IO;
using System.Drawing;
using Docs.Word;



public partial class Default : System.Web.UI.Page
{
    // Data source
    static public DataTable table = new DataTable();


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            table.Columns.Clear();
            table.Rows.Clear();

            table.Columns.Add("N");
            table.Columns.Add("First Name");
            table.Columns.Add("Last Name");
            table.Columns.Add("Sex");
            table.Columns.Add("Birth Date");
            table.Columns.Add("Occupation");
            table.Rows.Add(new object[] { "1", "Rodney", "Johnson", "M", "06.07.1964", "Adult literacy teacher" });
            table.Rows.Add(new object[] { "2", "Connie", "King", "F", "07.19.1989", "Occupational health and safety technician" });
            table.Rows.Add(new object[] { "3", "Steven", "Gray", "M", "12.24.1977", "Telephone repairer" });
            table.Rows.Add(new object[] { "4", "Cynthia", "Manigault", "F", "03.19.1969", "Employee assistance plan manager" });
            table.Rows.Add(new object[] { "5", "Gracie", "Salas", "F", "07.26.1967", "Heavy vehicle service technician" });

            GridView1.DataSource = table;
            GridView1.DataBind();
        }
    }


    protected void Button1_Click1(object sender, EventArgs e)
    {
        // Add new Record to DataTable 
        int num = table.Rows.Count + 1;
        table.Rows.Add(new object[] { num.ToString(), TextBox1.Text, TextBox2.Text, TextBox4.Text, TextBox3.Text, TextBox5.Text });

        // Show data on the Form
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        // Delete the lats Record
        int num = table.Rows.Count - 1;
        if (num >= 0) table.Rows.RemoveAt(num);

        // Show data on the Form
        GridView1.DataSource = table;
        GridView1.DataBind();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        // Create a new Word document
        Document Doc = Document.CreateNew();
        NodeList Nodes = Doc.Sections[0].Nodes;

        Paragraph ParTitle = new Paragraph("The Famous Company");
        ParTitle.Style.Spacings.SpacingBefore = 30;
        ParTitle.Style.Spacings.SpacingAfter = 12;
        ParTitle.Style.Alignment = Alignment.Center;
        ParTitle.Textruns[0].Style.FontSize = 30;
        ParTitle.Textruns[0].Style.TextColor = Color.DarkBlue;
        Nodes.Add(ParTitle);


        // Export GridView to Word directly
        int rows = table.Rows.Count;
        int cols = table.Columns.Count;

        // Create table Headers
        Docs.Word.Table Table1 = new Docs.Word.Table(rows + 1, cols, 1);
        Nodes.Add(Table1);
        Row Row1 = Table1.Rows[0];
        for (int col = 0; col < table.Columns.Count; col++)
        {
            string hd = table.Columns[col].Caption;
            Cell Cell1 = Row1.Cells[col];
            Cell1.AddParagraph(hd);
            Cell1.GetParagraphs()[0].Textruns[0].Style.TextColor = Color.White;
            Cell1.Style.Shading.BackColor = ColorTranslator.FromHtml("#5D7B9D");
            # region borders and widths
            Cell1.Style.Borders.Left.Color = Color.White;
            Cell1.Style.Borders.Top.Color = Color.White;
            Cell1.Style.Borders.Right.Color = Color.White;
            Cell1.Style.Borders.Bottom.Color = Color.White;
            if (hd == "N") Cell1.Style.Width = new PreferWidth(20, WidthType.Points);
            if (hd.Contains("Name")) Cell1.Style.Width = new PreferWidth(70, WidthType.Points);
            if (hd.Contains("Sex")) Cell1.Style.Width = new PreferWidth(30, WidthType.Points);
            if (hd.Contains("Birth")) Cell1.Style.Width = new PreferWidth(70, WidthType.Points);
            if (hd.Contains("Occupation")) Cell1.Style.Width = new PreferWidth(200, WidthType.Points);
            #endregion
        }


        // Fill the table
        for (int r = 0; r < rows; r++)
        {
            for (int c = 0; c < cols; c++)
            {
                string val = table.Rows[r][c].ToString();
                Cell Cell1 = Table1.Rows[r + 1].Cells[c];
                Cell1.AddParagraph(val);
                if ((r % 2) == 0) Cell1.Style.Shading.BackColor = ColorTranslator.FromHtml("#F7F6F3");
                #region borders and widths
                Cell1.Style.Borders.Left.Color = Color.White;
                Cell1.Style.Borders.Top.Color = Color.White;
                Cell1.Style.Borders.Right.Color = Color.White;
                Cell1.Style.Borders.Bottom.Color = Color.White;
                if (c == 0) Cell1.Style.Width = new PreferWidth(20, WidthType.Points);
                if (c == 1 || c == 2) Cell1.Style.Width = new PreferWidth(70, WidthType.Points);
                if (c == 3) Cell1.Style.Width = new PreferWidth(30, WidthType.Points);
                if (c == 4) Cell1.Style.Width = new PreferWidth(70, WidthType.Points);
                if (c == 5) Cell1.Style.Width = new PreferWidth(200, WidthType.Points);
                #endregion
            }
        }

        // Select the Word file type for export
        switch (RadioButtonList1.SelectedValue.ToUpper())
        {
            case "DOC":
                {
                    Response.AppendHeader("Content-Type", "application/msword");
                    Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "word.doc"));
                    Response.BinaryWrite(Doc.WriteDOC().ToArray());
                    Response.Flush();
                    Response.End();
                    break;
                }
            case "DOCX":
                {
                    Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
                    Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "word.docx"));
                    Response.BinaryWrite(Doc.WriteDOCX().ToArray());
                    Response.Flush(); Response.End();
                    break;
                }
            case "RTF":
                {
                    Response.AppendHeader("Content-Type", "application/rtf");
                    Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "word.rtf"));
                    Response.BinaryWrite(Doc.WriteRTF().ToArray());
                    Response.Flush(); Response.End();
                    break;
                }
        }
    }
}