asp.net webapi 中使用rdlc 报表

1:首先检查自己安装的VS 是否安装了报表程序,如果没有,重新修改VS安装选项,将rdlc 报表添加进来,具体方法请百度,这里不做更多叙述,保证自己开发的VS环境和正式环境发布的VS环境一致,以避免rdlc 报表出现版本兼容问题。

2:将报表应用程序转化为字节流以用来保存到本地。

 public static byte[] ExportByte<T>(string ptype, string rdlcFileUrl, List<ReportParameter> reportparamts, IQueryable<T> result, string filename, int haveimg = 0) //ptype 为"Excel" 或"PDF" 
        {


            try
            {
                ReportViewer ReportViewer2 = new ReportViewer();
                ReportViewer2.LocalReport.DataSources.Clear();
                ReportViewer2.LocalReport.ReportPath = rdlcFileUrl;
                #region 参数
                if (haveimg > 0)
                {
                    ReportViewer2.LocalReport.EnableExternalImages = true;
                }
                if (reportparamts != null)
                {
                    ReportViewer2.LocalReport.SetParameters(reportparamts);
                }
                #endregion
                #region 数据

                ReportViewer2.LocalReport.DataSources.Add(new Microsoft.Reporting.WebForms.ReportDataSource("DataSet1", result));
                #endregion
                ReportViewer2.LocalReport.Refresh();
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string extension;

                string contentType;
                switch (ptype)
                {
                    case "PDF":
                        contentType = "application/pdf";
                        break;

                    case "EXCEL":
                        contentType = "application/vnd.ms-excel";
                        break;

                    default:
                        contentType = "application/pdf";
                        break;
                }


                byte[] bytes = ReportViewer2.LocalReport.Render(ptype, null, out mimeType, out encoding, out extension, out streamids, out warnings);


                return bytes;
            }
            catch(Exception err)
            {

                LogHelper.Debug(err.ToString());
                return null;
            }


        }

  

原文地址:https://www.cnblogs.com/TallkingIsEasying/p/15049103.html