更改Crystal Report的XML数据源

时间:2012-06-28 20:01:40

标签: c# xml ado.net crystal-reports

我创建了一个Crystal Report并将其连接到C:\SomeDir\Data.xml上的XML文件。

在运行时,我可能需要将数据放在C:\SomeOtherDir\Data.xml

到目前为止我的代码看起来像这样:

ReportDocument report = new ReportDocument();
report.Load("Report.rpt");
PrinterSettings printerSettings = new PrinterSettings();
PageSettings pageSettings = printerSettings.DefaultPageSettings;
report.PrintToPrinter(printerSettings, pageSettings, false);

这将打印包含C:\SomeDir\Data.xml数据的报告。我希望它在C:\SomeOtherDir\Data.xml打印数据。

我该怎么做?

4 个答案:

答案 0 :(得分:1)

ReportDocument report = new ReportDocument();
report.Load("Report.rpt");

DataSet reportData = new DataSet();
reportData.ReadXml(@"C:\SomeOtherDir\Data.xml");
report.SetDataSource(reportData);

PrinterSettings printerSettings = new PrinterSettings();
PageSettings pageSettings = printerSettings.DefaultPageSettings;
report.PrintToPrinter(printerSettings, pageSettings, false);

如果XML的架构发生变化,您将需要在CR编辑器中打开报告并“验证数据库”以更新它所绑定的架构,否则它将引发神秘的“登录失败”错误。

答案 1 :(得分:0)

在Java中,如果我希望在运行时替换一组新数据,我通常会加载该数据(无论是db或xml中的结果集还是其他),并通过rpt.Database.Tables将其推入。 setDataSource传递原始数据的“表别名”,因此CR知道要覆盖的内容。在C#中它可能类似。您可能想要了解它,尝试一下然后再回答更多问题。

答案 2 :(得分:0)

Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports.Engine
Imports BL

Public Class frmRptViewer
    Dim strReportName As String
    Dim ds As New DataSet
    Public bl As New BL.InvoiceBL
    Private Sub configureCrystalReports()
        Dim strReportName As String
        Try
            strReportName = "Inv"
            ds = bl.FillHDDT(TrnCode)
            Dim strReportPath As String = Application.StartupPath & "\Reports\" & strReportName & ".rpt"
            Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
            ds.WriteXml(Application.StartupPath & "\xmlFiles\INVOICE.xml", XmlWriteMode.WriteSchema)
            rptDocument.Load(strReportPath)

            Dim crpConnectionInfo As New CrystalDecisions.Shared.ConnectionInfo
            With crpConnectionInfo
                .ServerName = Application.StartupPath & "\xmlFiles\INVOICE.xml"
                .DatabaseName = "NewDataset"
                .UserID = ""
                .Password = ""
            End With

            Dim tblCurrent As Table
            Dim crpTableLogOnInfo As New CrystalDecisions.Shared.TableLogOnInfo()
            For Each tblCurrent In rptDocument.Database.Tables
                tblCurrent.LogOnInfo.ConnectionInfo.ServerName = Application.StartupPath & "\xmlFiles\INVOICE.xml"
                tblCurrent.LogOnInfo.ConnectionInfo.DatabaseName = "NewDataset"
                tblCurrent.LogOnInfo.ConnectionInfo.UserID = ""
                tblCurrent.LogOnInfo.ConnectionInfo.Password = ""
                tblCurrent.LogOnInfo.ConnectionInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.MetaData
            Next
            rptDocument.Database.Tables(0).SetDataSource(ds.Tables("Table"))
            rptDocument.Database.Tables(1).SetDataSource(ds.Tables("Table1"))

            crptViewer.ShowRefreshButton = False
            crptViewer.ShowCloseButton = False
            crptViewer.ShowGroupTreeButton = False
            crptViewer.ReportSource = rptDocument
        Catch ex As Exception
        End Try
    End Sub

答案 3 :(得分:0)

如果你需要从开始, 考虑您需要使用XML

在Crystelreport中显示打印DataGridView数据
**(This is very helpful if you not using any database)**
  1. 首先制作数据表
  2. 然后将数据添加到数据表(此处从DataGridview添加)
  3. 制作XML文件
  4. 运行报告
  5. 此处示例代码

     DataTable dt = new DataTable();
     dt.Columns.Add("Item_Id", typeof(string));
     dt.Columns.Add("Categry", typeof(string));
     dt.Columns.Add("Item_Name", typeof(string));
     dt.Columns.Add("Unit_Price", typeof(double));
     dt.Columns.Add("Quantity", typeof(int));
     dt.Columns.Add("Discount", typeof(string));
     dt.Columns.Add("Total_Payable", typeof(double));
      foreach (DataGridViewRow dgr in DGVsell.Rows) 
     {
       dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value, dgr.Cells[4].Value, dgr.Cells[5].Value, dgr.Cells[6].Value);
     }
     ds.Tables.Add(dt);
     ds.WriteXmlSchema("Bill.xml");
    

    注意如果错误使Xml更改 App.config 文件如下

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      </startup>-->
        <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      </startup>
    </configuration>
    

    制作xml文件后,您可以拨打Crystel报告

     frmreport obj = new frmreport(); //load report viwer form
     obj.ShowDialog();
    
    报告viwer中的

    crBill cr = new crBill();
    cr.SetDataSource(frmSell.ds);
    crystalReportViewer1.ReportSource = cr;
    crystalReportViewer1.RefreshReport();
    crystalReportViewer1.Refresh();
    
相关问题