ReportDataSource和localreport [英] ReportDataSource and localreport

查看:65
本文介绍了ReportDataSource和localreport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用localreport.i绑定的reportviewer使用数据表作为datasource.once reportviewer显示在屏幕上我想将reportviewer保存为word file.i尝试下面的代码但是给我一个错误。我可以修复它?

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();

推荐答案

问题是你没有将RDLC报告导出到Word !!!这样做的方法如下:



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

//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:

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




这一个






by this one:

Response.BinaryWrite(docContent);


这篇关于ReportDataSource和localreport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆