请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

Lenka.Guo 讲师达人认证 悬赏达人认证
超级版主   /  发表于:2020-10-30 16:04  /   查看:2704  /  回复:0
在线设计器往往是集成到项目中,且是为不懂技术的业务人员使用的,因此开发人员不期望用户来自己配置数据源,而是在打开设计器时直接配置好数据源他们就可以直接设计即可。
本篇文章就来分享下如何实现新建报表时内置数据源。


1. 打开 View-Design-Index.cshtml 页面,在代码后面调用以下接口,默认新建时创建模板报表:

  1.   GrapeCity.ActiveReports.WebDesigner.renderApplication(designerId, designerOptions);

  2.                 // 在Create时候为报表打开模板文件,方便为新建报表绑定数据源
  3.         GrapeCity.ActiveReports.WebDesigner.api.createReport({
  4.             templateInfo: {
  5.                                 id: "OrganizationBrandedReport.rdlx",
  6.                        
  7.             },
  8.         });
复制代码
2. 修改 Services中的ITemplatesService.cs
  1. public interface ITemplatesService
  2.     {
  3.                 TemplateInfo[] GetTemplatesList();
  4.                 byte[] GetTemplate(string id,string ds);
  5.                 TemplateThumbnail GetTemplateThumbnail(string id);
  6.     }
复制代码
3. 修改 Implementation 中的FileSystemTemplate.cs

  1. public byte[] GetTemplate(string id,string ds)
  2.                 {
  3.                         var fullPath = Path.Combine(_rootFolder.FullName, id);

  4.                         if (!File.Exists(fullPath)) throw new FileNotFoundException();

  5.                         var templateXml = File.ReadAllBytes(fullPath);
  6.                         var template = ReportConverter.FromXML(templateXml);
  7.                        
  8.                         // 为Template绑定数据源
  9.                         AddDataSetDataSource(template,ds);

  10.                         var thumbnail = template.EmbeddedImages.FirstOrDefault(i => i.Name == templateThumbnailName);
  11.                         if (thumbnail != null) template.EmbeddedImages.Remove(thumbnail);

  12.                         var templateJson = ReportConverter.ToJson(template);
  13.                         return templateJson;
  14.                 }

  15.                 // 为报表创建数据源,并指定Json 字串
  16.                 public static Report AddDataSetDataSource(Report report, string ds)
  17.                 {
  18.                         // create DataSource for the report
  19.                         DataSource dataSource = new DataSource();
  20.                         dataSource.Name = "TESTData";
  21.                         dataSource.ConnectionProperties.DataProvider = "JSON";
  22.                         string datasource1 = "jsondoc="+ds;
  23.                         dataSource.ConnectionProperties.ConnectString = datasource1;
  24.                         //Create DataSet with specified query and load database fields to the DataSet
  25.                         DataSet dataSet = new DataSet();
  26.                         Query query = new Query();
  27.                         dataSet.Name = "Sample DataSet";
  28.                         query.DataSourceName = "TESTData";                       
  29.                         query.CommandText = ExpressionInfo.FromString("$.[*]");
  30.                         dataSet.Query = query;

  31.                         String[] fieldsList = new String[] { "postId", "id", "name","body" };

  32.                         foreach (string fieldName in fieldsList)
  33.                         {
  34.                                 Field field = new Field(fieldName, fieldName, null);
  35.                                 dataSet.Fields.Add(field);
  36.                         }
  37.                         //create report definition with specified DataSet and DataSource
  38.                         report.DataSources.Add(dataSource);
  39.                         report.DataSets.Add(dataSet);
  40.                         return report;
  41.                 }
复制代码
4. 修改 Contollers里面的 DesignController.cs  Create方法
  1. public ActionResult Create([FromQuery(Name = "datasource")] string datasource1,[FromQuery(Name = "theme")] string theme = "blue")
  2.                 {
  3.                        
  4.                         TempData["datasource"] = datasource1;// 保存Query中的DataSource String
  5.                         ViewBag.Theme = theme;
  6.                         return View("Index");
  7.                 }
复制代码


5. 修改 Contollers里面的 TemplatesController.cs  GetTemplate方法

  1.                         if (string.IsNullOrWhiteSpace(id)) return BadRequest();

  2.                         //获取前台传递的 datasource String
  3.                         var datasource01 = TempData["datasource"] as string;                       
  4.                         var template = templatesService.GetTemplate(id, datasource01);// 将DataSource String 传递给模板,对应的要修改 ItempaletService 的GetTemplate方法增加参数2
  5.                         return File(template, "application/json");
复制代码





0 个回复

您需要登录后才可以回帖 登录 | 立即注册
返回顶部