2012-06-28 25 views
5

Tôi đã tạo một Báo cáo Tinh thể và kết nối nó với một tệp XML tại C:\SomeDir\Data.xml.Thay đổi nguồn dữ liệu XML của Báo cáo Tinh thể

Khi chạy, tôi có thể cần phải nhập dữ liệu vào C:\SomeOtherDir\Data.xml.

Code tôi có cho đến nay trông như thế này:

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

Đó sẽ in báo cáo với dữ liệu ở C:\SomeDir\Data.xml. Tôi muốn in dữ liệu tại C:\SomeOtherDir\Data.xml.

Tôi làm cách nào để thực hiện việc này?

+0

Bạn có thể vui lòng đăng mã để tải báo cáo từ xml không? – Urik

+0

@Urik: Không có mã. Báo cáo được liên kết với tệp XML. –

+0

Nhưng không phải bạn đang sử dụng một cái gì đó như rpt.Database.Tables [0] .SetDataSource (ds_xml); ? – Urik

Trả lời

0

Trong Java nếu tôi muốn thay thế một tập dữ liệu mới vào thời gian chạy, tôi thường tải trong dữ liệu đó (có thể là tập hợp kết quả từ db hoặc xml hoặc bất kỳ thứ gì) và đẩy nó qua rpt.Database.Tables .setDataSource truyền 'bí danh bảng' của dữ liệu gốc để CR biết nội dung cần ghi đè. Nó có thể sẽ tương tự trong C#. Bạn có thể muốn tìm hiểu về điều đó, dùng thử và sau đó quay trở lại với nhiều câu hỏi hơn.

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

Nếu giản đồ của những thay đổi XML, bạn sẽ cần phải mở báo cáo trong trình soạn thảo CR và "xác minh cơ sở dữ liệu" để cập nhật các giản đồ nó được liên kết với, hoặc nó sẽ ném bí ẩn "Logon Không" lỗi.

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 
+0

có hoạt động không? – deltu100

0

Nếu bạn cần từ đầu, xem xét bạn cần dữ liệu DataGridView in hiển thị trong Crystelreport sử dụng XML

**(This is very helpful if you not using any database)** 
  1. làm đầu tiên bảng dữ liệu
  2. Rồi Thêm dữ liệu vào bảng dữ liệu (đây thêm từ DataGridview)
  3. Tạo tệp XML
  4. Chạy báo cáo

Đây Mẫu Mã

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

lưu ý Nếu lỗi đã làm thay đổi Xml App.config tập tin như sau

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

Sau khi tệp xml đánh dấu, bạn có thể gọi đến Báo cáo Crystel

frmreport obj = new frmreport(); //load report viwer form 
obj.ShowDialog(); 

trong báo cáo viwer

crBill cr = new crBill(); 
cr.SetDataSource(frmSell.ds); 
crystalReportViewer1.ReportSource = cr; 
crystalReportViewer1.RefreshReport(); 
crystalReportViewer1.Refresh(); 
Các vấn đề liên quan