动态RDLC报表类DynamicReport:显示报表和打印
在前面已经人寿了一个空的报表文档,各种节点的数据也已经生成,现在只需把各种节点的数据放进报表文档里,然后加载到ReportViewer中就可以了。
显示报表之前要先传入一个ReportViewer。
private ReportViewer _report;
public void SetReport(ReportViewer reportViewer, DisplayMode displayMode = DisplayMode.PrintLayout, ZoomMode zoomMode = ZoomMode.PageWidth)
{
reportViewer.Dock = System.Windows.Forms.DockStyle.Fill;
reportViewer.SetDisplayMode(displayMode);
reportViewer.ZoomMode = zoomMode;
this._report = reportViewer;
}
生成RDLC报表并显示
public void ShowReport()
{
//将每一个patter转换
if (_reportItemPatterns.Count > 0)
{
var dataSetsString = new StringBuilder();
var tablixString = new StringBuilder();
foreach (var reportItemPattern in _reportHeadPatterns)
{
dataSetsString.Append(reportItemPattern.DataSetString);
tablixString.Append(reportItemPattern.TablixString);
}
foreach (var reportItemPattern in _reportItemPatterns)
{
dataSetsString.Append(reportItemPattern.DataSetString);
tablixString.Append(reportItemPattern.TablixString);
}
foreach (var reportRemarksPattern in reportRemarks)
{
tablixString.Append(reportRemarksPattern);
}
//是否显示签名签章
if (isSignature)
{
if (isSignatureImg) tablixString.Append(signaturePattern.Replace("@TopPosition", tabelTopPosition.ToString()).Replace("@LeftPosition", (pageWidth - leftMargin - 9F).ToString()));
}
//把文档中的文字替换掉
if (tablixString.ToString() == "") _docTemplate = _docTemplate.Replace("@Tablix ", "");
_docTemplate = _docTemplate.Replace("@DataSets", dataSetsString.ToString())
.Replace("@Tablix", tablixString.ToString())
.Replace("@HeaderHeight", headerHeight.ToString())
.Replace("@FooterHeight", footerHeight.ToString())
.Replace("29.7cm ", "" + pageHeight.ToString() + "cm ")
.Replace("21.0cm ", "" + pageWidth.ToString() + "cm ");
if (reportLogoString.ToString() == "")
{
if (reportLabelPatterns.Count > 0)
{
foreach (var reportLabelPattern in reportLabelPatterns)
{
reportLogoString.Append(reportLabelPattern);
}
foreach (var reportTitlePattern in _reportTitlePatterns)
{
reportLogoString.Append(reportTitlePattern);
}
_docTemplate = _docTemplate.Replace("@PageHeader", reportLogoString.ToString());
}
else
{
if (_reportTitlePatterns.Count > 0)
{
foreach (var reportTitlePattern in _reportTitlePatterns)
{
reportLogoString.Append(reportTitlePattern);
}
_docTemplate = _docTemplate.Replace("@PageHeader", reportLogoString.ToString());
}
else
{
_docTemplate = _docTemplate.Replace("@PageHeader ", "");
}
}
_docTemplate = _docTemplate.Replace("@LogoImageData ", "");
}
else
{
foreach (var reportLabelPattern in reportLabelPatterns)
{
reportLogoString.Append(reportLabelPattern);
}
foreach (var reportTitlePattern in _reportTitlePatterns)
{
reportLogoString.Append(reportTitlePattern);
}
_docTemplate = _docTemplate.Replace("@PageHeader", reportLogoString.ToString());
}
foreach (var reportPageFooterPattern in _reportPageFooter)
{
reportPageFooter.Append(reportPageFooterPattern);
}
if (reportPageFooter.ToString() == "")
{
_docTemplate = _docTemplate.Replace("@PageFooter ", "");
}
else
{
_docTemplate = _docTemplate.Replace("@PageFooter", reportPageFooter.ToString());
}
var doc = new XmlDocument();
doc.LoadXml(_docTemplate);
//doc.Save(System.AppDomain.CurrentDomain.BaseDirectory + "\\docTemplate.xml");
Stream stream = GetRdlcStream(doc);
//加载报表定义
_report.LocalReport.LoadReportDefinition(stream);
_report.LocalReport.DataSources.Clear();
foreach (var reportItemPattern in _reportItemPatterns)
{
_report.LocalReport.DataSources
.Add(new ReportDataSource(reportItemPattern.DataSetName + "Data",
reportItemPattern.Data));
}
foreach (var reportItemPattern in _reportHeadPatterns)
{
_report.LocalReport.DataSources
.Add(new ReportDataSource(reportItemPattern.DataSetName + "Data",
reportItemPattern.Data));
}
_report.LocalReport.Refresh();
if (onlyPrint) PrintStream(_report.LocalReport);
}
}
protected Stream GetRdlcStream(XmlDocument xmlDoc)
{
Stream ms = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(typeof(XmlDocument));
serializer.Serialize(ms, xmlDoc);
ms.Position = 0;
return ms;
}
可以选择是否直接打印出来
private Boolean onlyPrint = false;
/// 用来记录当前打印到第几页了
///
private int m_currentPageIndex;
///
/// 声明一个Stream对象的列表用来保存报表的输出数据,LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
///
private IList m_streams;
private bool isLandSapces = false;
///
/// 用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数。
///
///
///
///
///
///
///
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
{
//如果需要将报表输出的数据保存为文件,请使用FileStream对象。
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
///
/// 为Report.rdlc创建本地报告加载数据,输出报告到.emf文件,并打印,同时释放资源
///
/// 参数:ReportViewer.LocalReport
public void PrintStream(LocalReport rvDoc)
{
//获取LocalReport中的报表页面方向
isLandSapces = rvDoc.GetDefaultPageSettings().IsLandscape;
Export(rvDoc);
PrintSetting();
Dispose();
}
private void Export(LocalReport report)
{
string deviceInfo =
@"
EMF
";
Warning[] warnings;
m_streams = new List();
//将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
}
private void PrintSetting()
{
if (m_streams == null || m_streams.Count == 0)
throw new Exception("错误:没有检测到打印数据流");
//声明PrintDocument对象用于数据的打印
PrintDocument printDoc = new PrintDocument();
//获取配置文件的清单打印机名称,不设置则用默认打印机
//printDoc.PrinterSettings.PrinterName = "Adobe PDF";
//foreach (string sPrint in PrinterSettings.InstalledPrinters)//获取所有打印机名称
//{
//}
printDoc.PrintController = new System.Drawing.Printing.StandardPrintController();//指定打印机不显示页码
//判断指定的打印机是否可用
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("错误:找不到打印机");
}
else
{
//设置打印机方向遵从报表方向
printDoc.DefaultPageSettings.Landscape = isLandSapces;
//声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
//设置打印机打印份数
printDoc.PrinterSettings.Copies = 1;
//执行打印操作,Print方法将触发PrintPage事件。
printDoc.Print();
}
}
///
/// 处理程序PrintPageEvents
///
///
///
private void PrintPage(object sender, PrintPageEventArgs ev)
{
//Metafile对象用来保存EMF或WMF格式的图形,
//我们在前面将报表的内容输出为EMF图形格式的数据流。
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
//调整打印机区域的边距
System.Drawing.Rectangle adjustedRect = new System.Drawing.Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);
//绘制一个白色背景的报告
//ev.Graphics.FillRectangle(Brushes.White, adjustedRect);
//获取报告内容
//这里的Graphics对象实际指向了打印机
ev.Graphics.DrawImage(pageImage, adjustedRect);
//ev.Graphics.DrawImage(pageImage, ev.PageBounds);
// 准备下一个页,已确定操作尚未结束
m_currentPageIndex++;
//设置是否需要继续打印
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
动态RDLC报表(一)
动态RDLC报表(二)
动态RDLC报表(三)
动态RDLC报表(四)
动态RDLC报表(五)
动态RDLC报表(六)
动态RDLC报表(七)
动态RDLC报表完整实例下载