user2384291
user2384291

Reputation: 17

Export datatable value to word using c#

I am new to asp.net and need to create a project. My requirement is I have a table where I will store data. This is my main table. Under each id I have a separate table:

Msg_id         Src                 Dest
701            RADAR               MSC
702            MSC                 RADAR

Msg_id         Message_size        Mgs_desc
701            256                 PFM_Load

Like that it continues... I have 3 dropdown lists. The 1st one is msg_id, the 2nd is src and the 3rd is dest. I also have a submit button the user can select any one of the dropdown lists and the corresponding table should be displayed in MS-Word.

Upvotes: 0

Views: 3542

Answers (3)

Asif Mahamud
Asif Mahamud

Reputation: 583

u can use this: THis code for export into csv formate which can open in both msword&msexcel:

private void OutPutFileToCsv(DataTable dt, string fileName, string seperator)
        {
            StringWriter stringWriter = new StringWriter();
            Int32 iColCount = dt.Columns.Count;
            for (Int16 i = 0; i < iColCount; i++)
            {
                stringWriter.Write(dt.Columns[i]);
                if (i < iColCount - 1)
                {
                    if (seperator.Contains(";"))
                        stringWriter.Write(";");
                    else
                        stringWriter.Write(",");
                }
            }
            stringWriter.Write(stringWriter.NewLine);
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        stringWriter.Write(dr[i].ToString().Trim());
                    }
                    if (i < iColCount - 1)
                    {
                        if (seperator.Contains(";"))
                            stringWriter.Write(";");
                        else
                            stringWriter.Write(",");
                    }
                }
                stringWriter.Write(stringWriter.NewLine);
            }

            Response.ClearContent();
            Response.ClearHeaders();

            Response.ContentType = "text/csv";
            Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", fileName));

            Response.ContentEncoding = Encoding.GetEncoding("iso-8859-1");
            //Response.BinaryWrite(Encoding.Unicode.GetPreamble());

            Response.Write(stringWriter.ToString());
            Response.End();
        }

Upvotes: 1

Freelancer
Freelancer

Reputation: 9074

You will need to create report of this data in .NET

Use EnableRenderExtension( "HTML4.0", "MS Word" ); for this purpose.

Then will have to export that report into word file.

Follow link:

http://www.codeproject.com/Articles/35225/Advanced-Report-Viewer

Or

Step by Step Approach:

http://www.accelebrate.com/sql_training/ssrs_2008_tutorial.htm

Hope Its helpful.

Upvotes: 1

Neeraj
Neeraj

Reputation: 4489

just pass your datatable in function and get grid data in ms-word.

 private void ExportToWord(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            string filename = "DownloadReport.xls";
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            DataGrid dgGrid = new DataGrid();
            dgGrid.DataSource = dt;
            dgGrid.DataBind();

            //Get the HTML for the control.
            dgGrid.RenderControl(hw);
            //Write the HTML back to the browser.
            Response.ContentType = "application/msword";
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
            this.EnableViewState = false;
            Response.Write(tw.ToString());
            Response.End();
        }
    }

Hope it helps you.

Upvotes: 0

Related Questions