【问题标题】:Create xtrareport with xml in asp.net在 asp.net 中使用 xml 创建 xtrareport
【发布时间】:2015-11-04 07:32:40
【问题描述】:

我正在尝试使用 c# 在 asp.net 中创建带有 xml 的 xtrareport。我创建了 xml 并引用了 xtrareport。我还在 xtrareport 设计中选择了数据源 schmema 和数据成员,并将字段放入标签。我也调试了数据集,它不是空的。但我在报告页面上看不到数据。

SqlConnection conn = new SqlConnection(@"blabla");
SqlCommand select = new SqlCommand(@"select * from table",conn);

conn.Open();
SqlDataAdapter da = new SqlDataAdapter(select);
DataSet ds = new DataSet();
da.Fill(ds);

//ds.WriteXmlSchema(@"C:\dataset.xml");

XtraReport1 rpr = new XtraReport1();
rpr.DataSource = ds;

rpr.PrintingSystem.SetCommandVisibility(PrintingSystemCommand.ClosePreview, DevExpress.XtraPrinting.CommandVisibility.None);
rpr.CreateDocument(true);`

【问题讨论】:

  • 不清楚您是如何在您的 asp.net 页面中使用您的报告的。

标签: c# asp.net xml devexpress xtrareport


【解决方案1】:

我个人创建扩展来加载和保存报告的 xml 布局。 我使用 win 应用程序或其他任何方式创建它们,然后保存 xml 布局:

public static void RestoreFromXmlXtraReportLayout(this DevExpress.XtraReports.UI.XtraReport report, string xmlXtraReportLayout)
{
    if (!string.IsNullOrEmpty(xmlXtraReportLayout))
    {
      string fileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".repx";
      System.IO.File.WriteAllText(fileName, xmlXtraReportLayout);
      report.LoadLayout(fileName);
      System.IO.File.Delete(fileName);
    }
}

public static string GetXmlXtraReportLayout(this DevExpress.XtraReports.UI.XtraReport report)
{
    string tmpFileName = System.IO.Path.GetTempPath() + Guid.NewGuid().ToString() + ".repx";
    report.SaveLayout(tmpFileName);
    string xmlXtraReportLayout = System.IO.File.ReadAllText(tmpFileName);
    System.IO.File.Delete(tmpFileName);
    return xmlXtraReportLayout;
}

使用Memory Stream(但效果不好,有些数据丢失了,你可以试试)

public static void RestoreLayoutFromNavigationItem(this DevExpress.XtraReports.UI.XtraReport report, string xmlXtraReportLayout)
{
    if (!string.IsNullOrEmpty(xmlXtraReportLayout))
    {
        using (Stream xmlStream = new System.IO.MemoryStream())
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(xmlXtraReportLayout);
            xDoc.Save(xmlStream);
            xmlStream.Flush();
            xmlStream.Position = 0;
            report.LoadLayoutFromXml(xmlStream);
        }
    }
 }

为了加载报告,我使用了一个包含 ASPxDocumentViewer 的网页:

<body>
     <form id="formDocumentViewer" runat="server">
    <div>
         <dx:ASPxDocumentViewer ID="mainDocumentViewer" runat="server">
    </dx:ASPxDocumentViewer>
    </div>
</form>

在页面加载:

protected void Page_Load(object sender, EventArgs e)
{
  report.DataSource = "your data source";
  string xmlXtraReportLayout = "load it from the saved file";
  report.RestoreFromXmlXtraReportLayout(xmlXtraReportLayout);
  this.mainDocumentViewer.SettingsSplitter.SidePaneVisible = false;
  this.mainDocumentViewer.Report = report;
}

但首先您必须使用报表设计器创建报表,将其加载到 win 应用程序中,使用 GetXmlXtraReportLayout 将布局保存到文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多