Crystal Reports TextObject 并不是那么简单。您必须首先创建一个 ParagraphTextElement 并将其放置在 ParagraphElements 对象中,在 Paragraphs 对象中的 Paragraph 对象中,并将该对象分配给 TextObject.Paragraphs 属性。
然后,您需要在 ReportDefinition 中找到要添加的报表部分,然后通过 ReportDefinition 将该对象添加到该部分。
足够解释了,这是我的 using 语句:
#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 应用程序进行测试。我希望它有所帮助!