在C#中将TextObject添加到ReportDocument

时间:2014-02-14 12:25:35

标签: c# visual-studio-2010 reportdocument

我使用Visual Studio 2010 SAP Crystal Reports 我使用报表查看器设置了一个表单,并将自定义设计的rpt文件加载到查看器的源中 它工作正常 我甚至可以从后面的代码中更改rpt文件的自定义设计字段:

ReportDocument reportDocument = new ReportDocument();

string filePath = AppDomain.CurrentDomain.BaseDirectory; 

filePath = filePath.Replace("bin\\Debug\\", "MyReportClass.rpt");

reportDocument.Load(filePath);

CrystalDecisions.CrystalReports.Engine.TextObject MyText1= ((CrystalDecisions.CrystalReports.Engine.TextObject)reportDocument.ReportDefinition.Sections[3].ReportObjects["MyText1"]);

MyText1.Text = "Test Work";

MyReportViewer.ReportSource = reportDocument;

问题:如何在代码后面的ReportDocument中添加新的TextObject?

我试过了:

reportDocument.Sections(2).AddTextObject(..

但该方法不存在

1 个答案:

答案 0 :(得分:0)

Crystal Reports TextObject并不那么简单。您必须首先创建一个ParagraphTextElement并将其放在ParagraphElements对象中,在Paragraphs对象的Paragraph对象中,并将该对象分配给TextObject.Paragraphs属性。

然后,您需要在ReportDefinition中找到要添加的报表部分,然后通过ReportDefinition将该对象添加到该部分。

虽然足够解释,但这是我的使用陈述:

#region Using

using System;
using System.Windows;

using CrystalDecisions.ReportAppServer.Controllers;
using CrystalDecisions.ReportAppServer.ReportDefModel;

#endregion

这是我使用它的代码:

var reportDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();

var filePath = AppDomain.CurrentDomain.BaseDirectory;
filePath = filePath.Replace("bin\\Debug\\", "MyReport.rpt");
reportDocument.Load(filePath);

ReportDefController2 reportDefinitionController = reportDocument.ReportClientDocument.ReportDefController;

ISCRParagraphTextElement paraTextElementClass = new ParagraphTextElement { Text = "Text Work", Kind = CrParagraphElementKindEnum.crParagraphElementKindText };

ParagraphElements paragraphElements = new ParagraphElements();
paragraphElements.Add(paraTextElementClass);

Paragraph paragraph = new Paragraph();
paragraph.ParagraphElements = paragraphElements;

Paragraphs paragraphs = new Paragraphs();
paragraphs.Add(paragraph);

TextObject newTextObject = new TextObject();
newTextObject.Paragraphs = paragraphs;

var detailSection = reportDefinitionController.ReportDefinition.DetailArea.Sections[0];
reportDefinitionController.ReportObjectController.Add(newTextObject, detailSection, 0);

// MyReportViewer.ReportSource = reportDocument;
this.crystalReportsViewer.ViewerCore.ReportSource = reportDocument;

最后一行是不同的,因为我正在使用WPF应用程序进行测试。我希望它有所帮助!