rdlc报表预览打印

    xiaoxiao2021-03-26  8

             本来在编写rdlc报表,总是不能成功打印/预览,提示本地报表处理期间出错,调试后发现可能出现此种情况的原因

    1、  rdlc报表文件加载路径不对

    2、  rdlc的数据库表名称与传递进去的表名称不同

    备注:虽然rdlc报表都关联了数据库表,但是使用时,传递进去的表的字段类型不一定要与数据库中类型保持一致,比如数据库PRINT_VW表中,minweight类型为int,但是传递时,为string类型仍然会成功,调用表预览仍然会成功。以下截图说明

    设置一页只预览一个表单步骤:

    选择表单

    →单击组属性,并设置

    →效果图

    在设置过程中,有可能会多打一页,可能原因见图

    以下提供打印rdlc代码

    public class MyPrint:IDisposable { /// <summary> /// 释放资源 /// </summary> public void Dispose() { if (m_streams != null) { foreach (Stream st in m_streams) { st.Close(); } m_streams.Clear(); m_streams = null; } } //声明一个Stream对象的列表用来保存报表的输出数据 //LocalReport对象的Render方法会将报表按页输出为多个Stream对象。 private IList<Stream> m_streams = null; //用来记录当前打印到第几页了 private int m_currentPageIndex; /// <summary> /// 用来提供Stream对象的函数,用于LocalReport对象的Render方法的第三个参数 /// </summary> /// <param name="name"></param> /// <param name="fileNameExtension"></param> /// <param name="encoding"></param> /// <param name="mimeType"></param> /// <param name="willSeek"></param> /// <returns></returns> private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { //如果需要将报表输出的数据保存为文件,请使用FileStream对象。 Stream stream = null; try { stream = new MemoryStream(); m_streams.Add(stream); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } return stream; } /// <summary> /// 打印 /// </summary> private void Print(string strPrinterName = null) { m_currentPageIndex = 0; if (m_streams == null || m_streams.Count == 0) return; try { //声明PrintDocument对象用于数据的打印 PrintDocument printDoc = new PrintDocument(); foreach (string strName in PrinterSettings.InstalledPrinters) { Console.WriteLine(strName); } //指定需要使用的打印机的名称,使用空字符串""来指定默认打印机 if (strPrinterName != String.Empty) printDoc.PrinterSettings.PrinterName = strPrinterName; //判断指定的打印机是否可用 if (!printDoc.PrinterSettings.IsValid) { MessageBox.Show("Can't find printer"); return; } //声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。 printDoc.PrintPage += new PrintPageEventHandler(PrintPage); //执行打印操作,Print方法将触发PrintPage事件。 //此句代码取消正在打印框 printDoc.PrintController = new System.Drawing.Printing.StandardPrintController(); printDoc.Print(); Dispose(); } catch (System.Exception ex) { MessageBox.Show("Print Err:" + ex.Message); } } private void PrintPage(object sender, PrintPageEventArgs ev) { //Metafile对象用来保存EMF或WMF格式的图形, //我们在前面将报表的内容输出为EMF图形格式的数据流。 m_streams[m_currentPageIndex].Position = 0; Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); //指定是否横向打印 ev.PageSettings.Landscape = false; //这里的Graphics对象实际指向了打印机 System.Drawing.Rectangle adjustedRect = new System.Drawing.Rectangle( ev.PageBounds.X,ev.PageBounds.Y,ev.PageBounds.Width,ev.PageBounds.Height); // 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.DrawImage(pageImage,adjustedRect); m_streams[m_currentPageIndex].Close(); m_currentPageIndex++; //设置是否需要继续打印 ev.HasMorePages = (m_currentPageIndex<m_streams.Count); } public void PintData(string strPath,DataSet dt,string strPrinterName = null) { try { LocalReport report = new LocalReport(); report.ReportPath = strPath; ReportDataSource source = new ReportDataSource(dt.Tables[0].TableName, dt.Tables[0]); report.DataSources.Add(source); report.Refresh(); string deviceInfo = /*@"<DeviceInfo> <OutputFormat>EMF</OutputFormat> <MarginTop>0in</MarginTop> <MarginLeft>0in</MarginLeft> <MarginRight>0in</MarginRight> <MarginBottom>0in</MarginBottom> </DeviceInfo>";*/ "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>" + " <PageWidth>8.5in</PageWidth>" + " <PageHeight>11in</PageHeight>" + " <MarginTop>0.25in</MarginTop>" + " <MarginLeft>0.25in</MarginLeft>" + " <MarginRight>0.25in</MarginRight>" + " <MarginBottom>0.25in</MarginBottom>" + "</DeviceInfo>"; Warning[] warnings; if (m_streams != null) { foreach (Stream st in m_streams) { st.Close(); } m_streams.Clear(); } else m_streams = new List<Stream>(); //将报表的内容按照deviceInfo指定的格式输出到CreateStream函数提供的Stream中。 report.Render("Image", deviceInfo, CreateStream, out warnings); Print(strPrinterName); } catch (System.Exception ex) { MessageBox.Show(ex.Message); } } }以下代码为使用C#自带报表预览

    ReportDataSource rds = new ReportDataSource("DT3_BASIC_PRINTVW", listData.dataSet.Tables[0]); this.reportViewer1.LocalReport.DataSources.Clear(); this.reportViewer1.LocalReport.DataSources.Add(rds); //为报表浏览器指定报表文件 //指定数据集,数据集名称后为表,不是DataSet类型的数据集 //reportViewer1.LocalReport.ReportPath this.reportViewer1.LocalReport.ReportPath = @"D:\xaqproject\AutoSorter\AutoSorter\ReportView\ReportTest.rdlc"; this.reportViewer1.RefreshReport();

    转载请注明原文地址: https://ju.6miu.com/read-600191.html

    最新回复(0)