Sweety

Sweety

  • NA
  • 2
  • 2.6k

Display Database values in MS-word using asp.net

May 15 2013 12:43 AM
Hi,


    I am new to asp.net i need to create a project.

   My requirement is

I have a table where i will store. This is my main table  under each id i have 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 contiues...I have 3 dropdownlist 1st one is msg_id,2nd is src and 3rd one is dest and submit button the user can select any one of the dropdownlist and the corresponding table should be display MS-Word.


Thank you




Answers (1)

0
Ravi Shekhar

Ravi Shekhar

  • 360
  • 4.5k
  • 2.1m
May 15 2013 2:34 AM



Hi Sweety,

Use below code...


 public void ExportData(DataSet dset)
        {
            HttpResponse response = HttpContext.Current.Response;
            // first let's clean up the response.object   
            response.Clear();
            response.Charset = "";
            // set the response mime type for excel  
            //Dim filename As String = "Record.xls"
            string filename = "Report(" + "" + DateTime.Now.ToString("dd-MM-yyyy") + ").doc";

            response.ContentType = "application/vnd.ms-word";
            response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

            // create a string writer   
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    // instantiate a datagrid   
                    DataGrid dg = new DataGrid();
                    dg.DataSource = dset.Tables[0];
                    dg.DataBind();
                    dg.RenderControl(htw);
                    response.Write(sw.ToString());
                    response.End();
                }
            }

        }



Now on show button click...



protected void Button2_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("Data Source=BCILGGN13\\SQLEXPRESS; Initial Catalog=test; Uid=sa; Pwd=bcil;");
            SqlDataAdapter adap = new SqlDataAdapter("select * from tblPopulation", cn);
            DataSet ds = new DataSet();
            adap.Fill(ds);

            ExportData(ds);
        }