Click here to Skip to main content
15,909,332 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i wanna work with a localreport.i bound reportviewer with a datatable as a datasource.once reportviewer display on the screen i want to save the reportviewer as a word file.i tried below code but is give me an error.how can fix it?
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["Applicationservices"].ConnectionString;
            SqlConnection con= new SqlConnection(strConnString);

            //Get the data from database into datatable
            string strQuery = "select *from Customers";
            SqlCommand cmd = new SqlCommand(strQuery, con);

            // Command nesnesini kullanarak adaptörümüzü olusturuyoruz.
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            // bilgi tasiyicisi olarak datatable veya dataset e ihtiyacimiz var
            DataTable dt = new DataTable();

            try
            {
                con.Open();//bağlantıyı aç
                sda.SelectCommand = cmd;//dataadapter ın çalıştıracağı komutu seç.
                sda.Fill(dt);//dataadapter ın seçtiği komutla gelen bilgileri datatable a doldur.
            }
            catch (Exception ex)
            {
                throw ex;
            }
 
            ReportDataSource datasource = new ReportDataSource("Report1",dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(datasource);
            ReportViewer1.LocalReport.Refresh();
         
            
            if (Session["whichbutton"].ToString()=="Buton1")
            {
                ReportViewer1.DataBind();
                Response.Clear();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", "attachment;filename=Customers.doc");

                Response.Charset = "";
                Response.ContentType = "application/vnd.ms-word ";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                
                ReportViewer1.RenderControl(hw);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
Posted

1 solution

The problem is that you are not exporting the RDLC report to Word!!! The way to do that is as follows:

C#
//Export to Word and get binary content
            string mimeType;
            string encoding;
            string fileNameExtension;
            string[] streams;
            Warning[] warnings;

		string deviceInfo =
            "<deviceinfo>" +
            "  <outputformat>Word</outputformat>" +
            "  <pagewidth>8.5in</pagewidth>" +
            "  <pageheight>11in</pageheight>" +
            "  <margintop>0.5in</margintop>" +
            "  <marginleft>0.75in</marginleft>" +
            "  <marginright>0.5in</marginright>" +
            "  <marginbottom>0.5in</marginbottom>" +
            "</deviceinfo>";

            byte[] docContent = ReportViewer1.LocalReport.Render("Word", deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);


And replace these lines in your code:

C#
Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();


by this one:

C#
Response.BinaryWrite(docContent);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900