admin 发表于 2019-3-13 12:26:19

创建您的 ActiveReports Web端在线报表设计器

<h3 id="-">概述</h3>
<p>ActiveReports
Web端在线报表设计器已经正式上线!看到它这么帅气、实用,你是不是也想自己动手创建一个?</p>
<p>现在我们就来教您,如何创建一个简单的 ActiveReports
Web端在线报表设计器(文末有示例源码,请大家下载体验),如您想体验ActiveReports
在线报表设计器的全部功能,请登陆 ActiveReports
官网,点击<a href="https://www.grapecity.com.cn/developer/activereports/designer">在线报表设计器</a>菜单。</p>
<h3 id="-">创建步骤</h3>
<ol>
<li><p>打开VS,然后创建一个 <strong>.Net Framework 4.6.2</strong>的<strong>ASP .NET Empty Web
Application</strong>的空项目</p>
<p><img src="https://www.grapecity.com.cn/media/1b82bff602ecbbfcf90a0687befa4580.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>选中引用,然后右键&gt;&gt; 管理NuGet程序包 &gt;&gt;点击</p>
<p><img src="https://www.grapecity.com.cn/media/304fae25b048d3d2ac9d46c773fc32c3.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>选择“浏览”,然后在查询框中输入” <strong>Nunit</strong>”, 点击安装</p>
<p><img src="https://www.grapecity.com.cn/media/02f38585f387b93c5d9a9135d27c9b3a.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入“<strong>Microsoft ASP.NET MVC</strong>”,点击安装,然后选择“<strong>我接受</strong>”</p>
<p><img src="https://www.grapecity.com.cn/media/1620d9c21caaa8152bc4a1b4b6d57beb.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入“<strong>OWIN</strong>”,点击安装</p>
<p><img src="https://www.grapecity.com.cn/media/3a744b4a2a9908d7519914462864d407.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入” <strong>Microsoft.Owin</strong>” ,点击安装,然后选择“<strong>我接受</strong>”</p>
<p><img src="https://www.grapecity.com.cn/media/d76b3b48c28adfca4ce16981bc89e81d.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入“ <strong>Microsoft.Owin.Host.SystemWeb</strong>”,点击安装,然后选择“<strong>我接受</strong>”</p>
<p><img src="https://www.grapecity.com.cn/media/e0fd27100cd972a6e20b5779846249e3.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入“<strong>Microsoft.Owin.StaticFiles</strong>”,点击安装,然后选择“<strong>我接受</strong>”</p>
<p><img src="https://www.grapecity.com.cn/media/6cb2a02f1d2b8f2db67950c0ec4c818c.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在查询框中输入“   <strong>Microsoft.Owin.FileSystems</strong>”,如果显示“<strong>已安装</strong>”,则跳过此步骤。</p>
<p><img src="https://www.grapecity.com.cn/media/10eaf879091eee93cfb66d0686a1f491.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>添加引用</p>
<p><img src="https://www.grapecity.com.cn/media/d9185e8d46841c7f8a467cdb0c3d9418.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>添加如下引用,引用的具体路径:</p>
<p>C:\Program Files (x86)\Common Files\GrapeCity\ActiveReports 13</p>
<p>添加引用如下:</p>
<ul>
<li><p>GrapeCity.ActiveReports.Aspnet.Viewer</p>
</li>
<li><p>GrapeCity.ActiveReports.Aspnet.Designer</p>
</li>
<li><p>GrapeCity.ActiveReports.Core.Diagnostics</p>
</li>
<li><p>Grapecity.ActiveReports.Core.Rdl</p>
</li>
</ul>
<p><img src="https://www.grapecity.com.cn/media/e5ec58e00e6eb9396231f3e9ccf18acf.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在项目中新添加一个新的文件</p>
<p><img src="https://www.grapecity.com.cn/media/1892acd5b9656e0bdce6d3a6531c4dff.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在新的 <strong>Startup.cs 的代码替换如下代码:</strong></p>
<pre class="hljs cs"><code><code class="lang-c#"><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.IO;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Web;
<span class="hljs-keyword">using</span> GrapeCity.ActiveReports.Aspnet.Designer;
<span class="hljs-keyword">using</span> Owin;
<span class="hljs-keyword">using</span> Microsoft.Owin;
<span class="hljs-keyword">using</span> Microsoft.Owin.StaticFiles;
<span class="hljs-keyword">using</span> Microsoft.Owin.FileSystems;
<span class="hljs-keyword">using</span> System.Web.Mvc;
<span class="hljs-keyword">using</span> System.Web.Routing;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">AspNetDesignerSample</span>
{
   <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Startup</span>
   {
       <span class="hljs-comment">// resources (reports, themes, images) location</span>
       <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">readonly</span> DirectoryInfo ResourcesRootDirectory =
         <span class="hljs-keyword">new</span> DirectoryInfo(String.Format(<span class="hljs-string">"{0}.\\resources\\"</span>, HttpRuntime.AppDomainAppPath));
       <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">Configuration</span>(<span class="hljs-params">IAppBuilder app</span>)
       </span>{
         <span class="hljs-comment">// web designer middleware</span>
         app.UseDesigner(config =&gt; config.UseFileStore(ResourcesRootDirectory));
         <span class="hljs-comment">// static files middlewares</span>
         <span class="hljs-keyword">var</span> fileSystem = <span class="hljs-keyword">new</span> PhysicalFileSystem(String.Format(<span class="hljs-string">"{0}.\\wwwroot\\"</span>, HttpRuntime.AppDomainAppPath));
         app.UseDefaultFiles(<span class="hljs-keyword">new</span> DefaultFilesOptions { DefaultFileNames = <span class="hljs-keyword">new</span>[] { <span class="hljs-string">"index.html"</span> }, FileSystem = fileSystem });
         app.UseStaticFiles(<span class="hljs-keyword">new</span> StaticFileOptions { FileSystem = fileSystem });
       }
   }
}
</code></code></pre>
</li>
<li><p>在项目中创造一个’resourse’
的文件,然后在可以在这个文件里放报表文件、主题、图片。为了方便你可以直接把web在线报表设计器源码里的
’resourse’整个文件直接赋值粘贴到你当前的项目中,然后把 ’resourse’
里的所有文件都是全部选择包含在项目中</p>
<p>源码路径:</p>
<p>C:\Users\******\Documents\GrapeCitySamples\ActiveReports
13\Web\WebDesigner_MVC</p>
<p><img src="https://www.grapecity.com.cn/media/1d1164d539172ae3d19ec1d52f54155c.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在该路径下C:\Program Files (x86)\GrapeCity\ActiveReports
13\Deployment\WebDesigner folder on 64-bit Windows.</p>
<p>复制 Web.Config 去替换项目中的已存在的 Web.Config 文件,注意需要移除Web.Config
中的如下配置。</p>
<pre class="hljs javascript"><code><code class="lang-html">&lt;handlers&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">add</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"AllUris"</span> <span class="hljs-attr">path</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">verb</span>=<span class="hljs-string">"*"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"System.Web.Handlers.TransferRequestHandler"</span> <span class="hljs-attr">preCondition</span>=<span class="hljs-string">"integratedMode"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">handlers</span>&gt;</span></span>
</code></code></pre>
</li>
<li><p>在项目中新建一个 ’ wwwroot’ 文件,然后再在里面添加如下文件</p>
<ul>
<li>baseServerApi.js</li>
<li>web-designer.css</li>
<li>web-designer.js</li>
<li>vendor</li>
</ul>
<p><img src="https://www.grapecity.com.cn/media/1208584a6215ee64f4b46ee72b527c15.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在项目中新添加一个新的 HTMLpage</p>
<p><img src="https://www.grapecity.com.cn/media/151ee4bf414d901bbef37209a046aec9.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>在项目中打开 index页面,然后修改其内容,如下所示</p>
<pre class="hljs xml"><code><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Web Designer Sample<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"utf-8"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"x-ua-compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
   <span class="hljs-comment">&lt;!-- designer-related css --&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"vendor/css/materialdesignicons.min.css"</span> <span class="hljs-attr">media</span>=<span class="hljs-string">"all"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span> /&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"vendor/css/bootstrap.min.css"</span> /&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"vendor/css/font-awesome.min.css"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"vendor/css/ionicons.min.css"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"vendor/css/fonts-googleapis.css"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/css"</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"web-designer.css"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"theme-blue"</span>&gt;</span>
   <span class="hljs-comment">&lt;!-- designer-related js --&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"vendor/js/jquery.min.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"vendor/js/bootstrap.min.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"baseServerApi.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"web-designer.js"</span>&gt;</span><span class="undefined"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
   <span class="hljs-comment">&lt;!-- designer root div --&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"designer-id"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width: 100%; height: 100%;"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
       <span class="hljs-comment">// create designer options</span>
       <span class="hljs-keyword">var</span> designerOptions = GrapeCity.ActiveReports.WebDesigner.createDesignerOptions(baseServerApi);
       <span class="hljs-comment">// render designer application</span>
       GrapeCity.ActiveReports.WebDesigner.renderApplication(<span class="hljs-string">'designer-id'</span>, designerOptions);
   </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></code></pre>
</li>
<li><p>最后运行结果:</p>
<p><img src="https://www.grapecity.com.cn/media/183932e6b915710f0096fd7d7db7f242.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>如果你想打开的编辑界面,不是一个空白的页面,而是一个已经存在的报表。</p>
<p>首先你需要在index.html的 <strong>createDesignerOptions()</strong> 的函数后面添加</p>
<pre class="hljs apache"><code><code class="lang-js"><span class="hljs-attribute">designerOptions</span>.reportInfo.id = <span class="hljs-string">"MyReport.rdlx"</span>;
</code></code></pre>
<p>"MyReport.rdlx"报表是在项目’resources’中存在的报表文件,不然就会找不到报表路径,显示不出来。</p>
<p><img src="https://www.grapecity.com.cn/media/9069860133fea901acf554d0ee3408ce.png" style="max-width:800px;" alt=""></p>
</li>
<li><p>添加 css 文件</p>
<pre class="hljs xml"><code><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"file-dialog.css"</span> /&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"web-designer.css"</span> /&gt;</span>
</code></code></pre>
</li>
<li><p>添加 JS 文件</p>
<pre class="hljs xml"><code><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"file-dialog.css"</span> /&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"web-designer.css"</span> /&gt;</span>
</code></code></pre>
</li>
<li><p>复制下面的<code class="hljs apache"><span class="hljs-section">&lt;div&gt;</span></code>替换 index.html 中原来的<code class="hljs apache"><span class="hljs-section">&lt;div&gt;</span></code></p>
<pre class="hljs xml"><code><code class="lang-html"><span class="hljs-comment">&lt;!-- designer root div --&gt;</span>
<span class="hljs-tag">&lt; <span class="hljs-attr">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"designer-id"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"width: 100%; height: 100%;"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-comment">&lt;!-- save as dialog root div --&gt;</span>
<span class="hljs-tag">&lt; <span class="hljs-attr">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"save-as-dialog-id"</span> <span class="hljs-attr">style</span>=<span class="hljs-string">"position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; z-index: 9999;"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></code></pre>
</li>
<li><p>替换 index.html中的<code class="hljs xml"><span class="hljs-tag">&lt;<span class="hljs-name">Script</span>&gt;</span><span class="undefined"></span></code>中的内容:</p>
<pre class="hljs xml"><code><code class="lang-html">   <span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span><span class="javascript">
   <span class="hljs-keyword">var</span> showElement = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">id</span>) </span>{
       <span class="hljs-keyword">if</span> (!id) <span class="hljs-keyword">return</span>;
       ($(<span class="hljs-string">'#'</span> + id)).css(<span class="hljs-string">'display'</span>, <span class="hljs-string">'block'</span>);
   };
   <span class="hljs-keyword">var</span> hideElement = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">id</span>) </span>{
       <span class="hljs-keyword">if</span> (!id) <span class="hljs-keyword">return</span>;
       ($(<span class="hljs-string">'#'</span> + id)).css(<span class="hljs-string">'display'</span>, <span class="hljs-string">'none'</span>);
   };
   <span class="hljs-keyword">var</span> designerId = <span class="hljs-string">'designer-id'</span>;
   <span class="hljs-keyword">var</span> saveAsDialogId = <span class="hljs-string">'save-as-dialog-id'</span>;
   <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">onSaveAs</span>(<span class="hljs-params">options</span>) </span>{
       showElement(saveAsDialogId);
       <span class="hljs-comment">// render save-as dialog</span>
       fileDialog.createSaveReportAsDialog(saveAsDialogId, {
         <span class="hljs-attr">locale</span>: options.locale,
         <span class="hljs-attr">api</span>: {
               <span class="hljs-attr">getReportsList</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                   <span class="hljs-keyword">return</span> baseServerApi.getReportsList()
                     .then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reportsList</span>) </span>{
                           <span class="hljs-keyword">return</span> reportsList.map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">reportInfo</span>) </span>{
                               <span class="hljs-keyword">return</span> { <span class="hljs-attr">path</span>: reportInfo.Name };
                           });
                     });
               },
               <span class="hljs-attr">saveReport</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">saveOptions</span>) </span>{
                   <span class="hljs-keyword">return</span> baseServerApi.saveNewReport({
                     <span class="hljs-attr">name</span>: saveOptions.path,
                     <span class="hljs-attr">content</span>: options.reportInfo.content,
                   }).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">saveResult</span>) </span>{
                     <span class="hljs-keyword">return</span> { <span class="hljs-attr">id</span>: saveResult.Id };
                   });
               },
         },
         <span class="hljs-attr">reportInfo</span>: {
               <span class="hljs-attr">path</span>: options.reportInfo.name,
         },
         <span class="hljs-attr">onSuccess</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">saveResult</span>) </span>{
               hideElement(saveAsDialogId);
               options.onSuccess({ <span class="hljs-attr">id</span>: saveResult.id, <span class="hljs-attr">name</span>: saveResult.path });
         },
         <span class="hljs-attr">onClose</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
               hideElement(saveAsDialogId);
         },
       });
   };
   <span class="hljs-comment">// create designer options</span>
   <span class="hljs-keyword">var</span> designerOptions = GrapeCity.ActiveReports.WebDesigner.createDesignerOptions(baseServerApi);

   designerOptions.reportInfo.id = <span class="hljs-string">" MyReport.rdlx"</span>;
   <span class="hljs-comment">// enable showing save-as button</span>
   designerOptions.saveAsButton.visible = <span class="hljs-literal">true</span>;
   <span class="hljs-comment">// specify behavior on save-as</span>
   designerOptions.onSaveAs = onSaveAs;
   <span class="hljs-comment">// render designer application</span>
   GrapeCity.ActiveReports.WebDesigner.renderApplication(designerId, designerOptions);
</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></code></pre>
</li>
<li><p>至此,一个简单的 ActiveReport 在线报表设计器已经创建成功,最终运行结果如下:</p>
<p><img src="https://www.grapecity.com.cn/media/6392ea348e543623ea55ce83e762a0ce.png" style="max-width:800px;" alt=""></p>
</li>
</ol>
<h3 id="activereport-">ActiveReport 在线报表设计器示例源码</h3>
<p>&gt;&gt;<strong><a href="http://www.grapecity.com.cn/downloads/demo/WebDesignerSample.zip">点击下载“WebDesignerSample”</a></strong></p>
页: [1]
查看完整版本: 创建您的 ActiveReports Web端在线报表设计器