用微軟的RDLC報表直接打印不預覽app
直接上代碼。函數
//打印清單 System.Data.DataTable dt = print_QD(dr); ReportViewer rvDoc = new ReportViewer(); rvDoc.LocalReport.ReportEmbeddedResource = "SD_bcso.Report.rdlc";//加上報表的路徑 rvDoc.LocalReport.DataSources.Add(new ReportDataSource(dt.TableName, dt)); PrintStream(rvDoc.LocalReport);
/// <summary> /// 用來記錄當前打印到第幾頁了 /// </summary> private int m_currentPageIndex; /// <summary> /// 聲明一個Stream對象的列表用來保存報表的輸出數據,LocalReport對象的Render方法會將報表按頁輸出爲多個Stream對象。 /// </summary> private IList<Stream> m_streams; private bool isLandSapces = false; /// <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 = new MemoryStream(); m_streams.Add(stream); return stream; } /// <summary> /// 爲Report.rdlc建立本地報告加載數據,輸出報告到.emf文件,並打印,同時釋放資源 /// </summary> /// <param name="rv">參數:ReportViewer.LocalReport</param> public void PrintStream(LocalReport rvDoc) { //獲取LocalReport中的報表頁面方向 isLandSapces = rvDoc.GetDefaultPageSettings().IsLandscape; Export(rvDoc); PrintSetting(); Dispose(); } private void Export(LocalReport report) { string deviceInfo = @"<DeviceInfo> <OutputFormat>EMF</OutputFormat> </DeviceInfo>"; Warning[] warnings; m_streams = new List<Stream>(); //將報表的內容按照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(); //獲取配置文件的清單打印機名稱 System.Configuration.AppSettingsReader appSettings = new System.Configuration.AppSettingsReader(); printDoc.PrinterSettings.PrinterName = appSettings.GetValue("QDPrint", Type.GetType("System.String")).ToString(); 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(); } } /// <summary> /// 處理程序PrintPageEvents /// </summary> /// <param name="sender"></param> /// <param name="ev"></param> 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); } public void Dispose() { if (m_streams != null) { foreach (Stream stream in m_streams) stream.Close(); m_streams = null; } }