【问题标题】:Modify dynamically a rdlc report (c#)动态修改 rdlc 报告 (c#)
【发布时间】:2015-11-17 17:12:32
【问题描述】:

我有一个存储在数据库中的字体,我必须使用该字体设置我的所有文件。 我这样设置绑定我的报告:

FormBudgReelReport form = new FormBudgReelReport();
form.Viewer.LocalReport.ReportEmbeddedResource = _NomRessourceRpt;
form.Viewer.LocalReport.DataSources.Add(source);
form.ShowDialog();

如果我可以将我的 rdlc 作为 XmlDocument 加载,我知道该怎么做,但是有没有办法做到这一点? 我不能使用像 =Parameters!Police.Value 这样的公式,因为我有很多报告要更改。

【问题讨论】:

    标签: report rdlc


    【解决方案1】:

    好的! 我可以通过此代码将我的 rdlc 作为 xmlDocument 加载:

    Stream st = this.GetType().Assembly.GetManifestResourceStream(_NomRessourceRpt);
    
    // convert stream to string
    StreamReader reader = new StreamReader(st);
    string reportDef = reader.ReadToEnd();
    
    XmlDocument document = new XmlDocument();
    document.LoadXml(reportDef);
    

    感谢您的帮助:)

    【讨论】:

      【解决方案2】:

      如果我可以将我的 rdlc 作为 XmlDocument 加载,我知道该怎么做,但是有没有办法做到这一点?

      在解决方案资源管理器中,您可以右键单击 .rdlc 文件并选择 Open With...> 以选择编辑器。


      更新:

      我正在寻找一种方法将我的 rdlc 加载到 xmlDocument 对象中,然后在运行时编辑 xml 节点。

      以下代码 sn -p 帮助您将 .rdlc 报告文件从资源文件夹加载到 Xml 文档:

      using System;
      using System.Windows.Forms;
      using System.Xml;
      using System.IO;
      using System.Resources;
      
       private void LoadRDLCFromResources()
       {
          //Declare variables
          XmlDocument objXmlDocument = new XmlDocument();
          Byte[] byteArray;
          Stream objStream = null;
          ResourceManager resourceManager = null;
      
          try
          {
              // Initialize ResourceManager object
              resourceManager = new ResourceManager("your_namespace_name.Properties.Resources", GetType().Assembly);
              if (resourceManager != null)
              {
                  //Load the resource file "Sample.rdlc" into ByteArray
                  byteArray = (Byte[])resourceManager.GetObject("Sample");
                  if (byteArray != null)
                  {
                      //Load this bytearray into MemoryStream
                      objStream = new MemoryStream(byteArray);
                      if (byteArray.Length > 0)
                      {
                          //Load this stream object into XML document and  
                          //thus you get the rdlc file loaded as an XML 
                          //document. 
                          objXmlDocument.Load(objStream);
      
                          // Code for using this xml document as per your use
                      }
                  }
              }
          } 
          //MissingManifestResourceException is an exception thrown when the resource manager fails to initialize appropriately. In such case, please check the namespace name.
          catch (MissingManifestResourceException ex)
          {
      
              MessageBox.Show("Exception -> " + ex.Message,
                 "Sample Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          catch (Exception ex)
          {
              MessageBox.Show("Exception -> " + ex.Message,
                  "Sample Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
          finally
          {
              // Clear the objects in finally block. This is required for memory management issues.
              // If xml document is not required further then clear it in the following manner
              if (objXmlDocument != null)
              {
                  objXmlDocument.DocumentElement.RemoveAllAttributes();
                  objXmlDocument.DocumentElement.RemoveAll();   
      
                  objXmlDocument.RemoveAll();
              }
              //Clear the memory stream object 
              if (objStream != null)
              {
                  objStream.Flush();
                  objStream.Close();
                  objStream.Dispose();
              }
              // Clear resource manager 
              resourceManager.ReleaseAllResources();
          }
      }
      

      来源:How to load a rdlc file from resources folder into an XML document dynamically?

      【讨论】:

      • 如果我这样做,我必须自己编辑我的所有报告。我正在寻找一种方法将我的 rdlc 加载到 xmlDocument 对象中,然后在运行时编辑 xml 节点。感谢您的帮助,如果没有其他解决方案,我会这样做
      • 我应该意识到这就是你要找的东西。我更新了我的答案。
      • 再次感谢您,但我仍有问题。如果我的 .rdlc 不在 Resources.resx 中,则此代码无法运行。我试图用 resourceManager.GetResourceSet 和一个 foreach 来展示其中的所有内容,但是没有 rdlc。我认为我们已经接近目标了!
      • 使用 this.GetType().Assembly.GetManifestResourceStream(_NomRessourceRpt);我可以将我的 rdlc 加载到 xmlDocument 中。听起来不错,我继续
      猜你喜欢
      • 1970-01-01
      • 2013-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多