C# RDLC報表不出現預覽窗體直接輸出到打印機
阿新 • • 發佈:2019-02-19
per put urn tom epo .repo ted meta eset
#region 直接打印區域 /// <summary> /// 直接打印到打印機 /// </summary> /// <param name="reportFullFileName"></param> /// <param name="reportTitle"></param> /// <param name="reportDSTable1"></param> /// <param name="reportDSTable2"></param>/// <param name="reportDSTable3"></param> /// <param name="reportDSTable4"></param> /// <param name="reportDSTable5"></param> /// <param name="hasSubReport"></param> /// <param name="subReportDSTable1"></param> ///<param name="subReportDSTable2"></param> /// <param name="tPageSettings"></param> /// <param name="tPrinterSettings"></param> /// <param name="logoFileName1"></param> /// <param name="logoFileName2"></param> /// <param name="logoFileName3"></param>public void PrintReportDrc(string reportFullFileName, string reportTitle, DataTable reportDSTable1, DataTable reportDSTable2 = null, DataTable reportDSTable3 = null, DataTable reportDSTable4 = null, DataTable reportDSTable5 = null, bool hasSubReport = false, DataTable subReportDSTable1 = null, DataTable subReportDSTable2 = null, System.Drawing.Printing.PageSettings tPageSettings = null, System.Drawing.Printing.PrinterSettings tPrinterSettings = null, string logoFileName1 = null, string logoFileName2 = null, string logoFileName3 = null) { if (!File.Exists(reportFullFileName)) { MyMsg.Error("T.1201/指定的報表文件不存在,請檢查!"); this.Close(); return; } this.Text += "(" + reportTitle + ")"; this.RptvMain.Reset(); this.RptvMain.ProcessingMode = ProcessingMode.Local; this.RptvMain.LocalReport.ReportPath = reportFullFileName;//必須在前面,否則出現The source of the report definition has not been specified錯誤 this.RptvMain.LocalReport.EnableExternalImages = true; // RDLC報表顯示本地圖片必須設置(默認是關閉) #region 處理參數 ReportParameterInfoCollection reportParameterInfos = RptvMain.LocalReport.GetParameters(); List<string> parasNames = new List<string>(); foreach (ReportParameterInfo rpi in reportParameterInfos) { parasNames.Add(rpi.Name); } List<ReportParameter> reportParameters = new List<ReportParameter>(); if (parasNames.Contains("LogoFile1")) { if (string.IsNullOrEmpty(logoFileName1)) { logoFileName1 = "RptLogoDft1.jpg"; } logoFileName1 = AppInfo.GetPath(AppInfo.AppPath.Report) + @"\Images\" + logoFileName1; logoFileName1 = @"file:///" + logoFileName1.Replace(@"\", @"/"); ReportParameter reportParameter = new ReportParameter("LogoFile1", logoFileName1); reportParameters.Add(reportParameter); } if (parasNames.Contains("LogoFile2")) { if (string.IsNullOrEmpty(logoFileName2)) { logoFileName2 = "RptLogoDft2.jpg"; } logoFileName2 = AppInfo.GetPath(AppInfo.AppPath.Report) + @"\Images\" + logoFileName2; logoFileName2 = @"file:///" + logoFileName2.Replace(@"\", @"/"); ReportParameter reportParameter = new ReportParameter("LogoFile2", logoFileName2); reportParameters.Add(reportParameter); } if (parasNames.Contains("LogoFile3")) { if (string.IsNullOrEmpty(logoFileName3)) { logoFileName3 = "RptLogoDft3.jpg"; } logoFileName3 = AppInfo.GetPath(AppInfo.AppPath.Report) + @"\Images\" + logoFileName3; logoFileName3 = @"file:///" + logoFileName3.Replace(@"\", @"/"); ReportParameter reportParameter = new ReportParameter("LogoFile3", logoFileName3); reportParameters.Add(reportParameter); } if (reportParameters.Count > 0) { this.RptvMain.LocalReport.SetParameters(reportParameters); } #endregion #region 處理Dataset this.RptvMain.LocalReport.DataSources.Clear(); if (reportDSTable1 != null) { ReportDataSource rds1 = new ReportDataSource("DataSet1", reportDSTable1); this.RptvMain.LocalReport.DataSources.Add(rds1); } if (reportDSTable2 != null) { ReportDataSource rds2 = new ReportDataSource("DataSet2", reportDSTable2); this.RptvMain.LocalReport.DataSources.Add(rds2); } if (reportDSTable3 != null) { ReportDataSource rds3 = new ReportDataSource("DataSet3", reportDSTable3); this.RptvMain.LocalReport.DataSources.Add(rds3); } if (reportDSTable4 != null) { ReportDataSource rds4 = new ReportDataSource("DataSet4", reportDSTable4); this.RptvMain.LocalReport.DataSources.Add(rds4); } if (reportDSTable5 != null) { ReportDataSource rds5 = new ReportDataSource("DataSet5", reportDSTable5); this.RptvMain.LocalReport.DataSources.Add(rds5); } #endregion //定義子報表處理方法 if (hasSubReport) { subDSTable1 = subReportDSTable1; subDSTable2 = subReportDSTable2; RptvMain.LocalReport.SubreportProcessing += new SubreportProcessingEventHandler(LocalReport_SubreportProcessing); } RptvMain.SetDisplayMode(DisplayMode.PrintLayout); RptvMain.ZoomMode = ZoomMode.Percent; RptvMain.ZoomPercent = 100; try { if (tPageSettings != null) RptvMain.SetPageSettings(tPageSettings); if (tPrinterSettings != null) RptvMain.PrinterSettings = tPrinterSettings; } catch (Exception) { } RptvMain.LocalReport.Refresh(); RptvMain.RefreshReport(); #region 直接打印定義 m_streams = new List<Stream>(); ReportPageSettings pageSettings = RptvMain.LocalReport.GetDefaultPageSettings(); decimal pgWidth = decimal.Round(HpString.NZ2Decimal(pageSettings.PaperSize.Width * 2.54 / 100), 2); decimal pgHeight = decimal.Round(HpString.NZ2Decimal(pageSettings.PaperSize.Height * 2.54 / 100), 2); decimal mgTop = decimal.Round(HpString.NZ2Decimal(pageSettings.Margins.Top * 2.54 / 100), 2); decimal mgLeft = decimal.Round(HpString.NZ2Decimal(pageSettings.Margins.Left * 2.54 / 100), 2); decimal mgRight = decimal.Round(HpString.NZ2Decimal(pageSettings.Margins.Right * 2.54 / 100), 2); decimal mgBottom = decimal.Round(HpString.NZ2Decimal(pageSettings.Margins.Bottom * 2.54 / 100), 2); string deviceInfo = "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>" + " <PageWidth>"+ pgWidth + "cm</PageWidth>" + " <PageHeight>" + pgHeight + "cm</PageHeight>" + " <MarginTop>" + mgTop + "cm</MarginTop>" + " <MarginLeft>" + mgLeft + "cm</MarginLeft>" + " <MarginRight>" + mgRight + "cm</MarginRight>" + " <MarginBottom>" + mgBottom + "cm</MarginBottom>" + "</DeviceInfo>"; RptvMain.LocalReport.Render("Image", deviceInfo, CreateStream, out Warning[] warnings); //==================================================開始打印 m_currentPageIndex = 0; if (m_streams == null || m_streams.Count == 0) return; //聲明PrintDocument對象用於數據的打印 PrintDocument printDoc = new PrintDocument { DefaultPageSettings = RptvMain.GetPageSettings() }; //如果沒有指定打印機設置,則使用空字符串""來指定默認打印機 if (RptvMain.PrinterSettings == null) { printDoc.PrinterSettings.PrinterName = ""; } else { printDoc.PrinterSettings = RptvMain.PrinterSettings; } //判斷指定的打印機是否可用 if (!printDoc.PrinterSettings.IsValid) { MyMsg.Warning("沒有找到指定的打印機:" + printDoc.PrinterSettings.PrinterName); return; } printDoc.PrintPage += new PrintPageEventHandler(PrintPage); //執行打印操作,Print方法將觸發PrintPage事件。 printDoc.Print(); //釋放資源 foreach (Stream stream in m_streams) { stream.Dispose(); stream.Close(); } m_streams = null; #endregion } //聲明一個Stream對象的列表用來保存報表的輸出數據 //LocalReport對象的Render方法會將報表按頁輸出為多個Stream對象。 private List<Stream> m_streams; //用來提供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; } //用來記錄當前打印到第幾頁了 private int m_currentPageIndex; #region 打印頁面 private void PrintPage(object sender, PrintPageEventArgs ev) { m_streams[m_currentPageIndex].Position = 0; 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(System.Drawing.Brushes.White, adjustedRect); ev.Graphics.DrawImage(pageImage, adjustedRect); m_streams[m_currentPageIndex].Close(); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } #endregion #endregion
C# RDLC報表不出現預覽窗體直接輸出到打印機