5

我创建了一个 Crystal Report 并将其连接到位于C:\SomeDir\Data.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

4 回答 4

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 编辑器中打开报表并“验证数据库”以更新其绑定的架构,否则会抛出神秘的“登录失败”错误。

于 2012-12-01T18:46:26.213 回答
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
于 2015-03-23T22:10:12.907 回答
0

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

于 2012-11-27T15:30:12.557 回答
0

如果您需要从头开始,请考虑您需要使用 XML 在 Crystelreport 中打印 DataGridView 数据

**(This is very helpful if you not using any database)**
  1. 首先制作数据表
  2. 然后将数据添加到数据表(这里从 DataGridview 添加)
  3. 制作 XML 文件
  4. 运行报告

这里示例代码

 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();

在报告查看器中

crBill cr = new crBill();
cr.SetDataSource(frmSell.ds);
crystalReportViewer1.ReportSource = cr;
crystalReportViewer1.RefreshReport();
crystalReportViewer1.Refresh();
于 2017-04-05T10:39:42.663 回答