using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
using Microsoft.Reporting.WebForms;
/// <summary> /// PrintHelper 的摘要描述 /// </summary> public class PrintHelper { private int m_currentPageIndex; private IList<Stream> m_streams; private bool isLandSapces=false; /// <summary> /// /// </summary> /// <param name="reportPath">rdlc路徑</param> /// <param name="printerName">打印機名稱</param> /// <param name="dt1">數據表集數據</param> /// <param name="dt1SourceName">數據對應rdlc中的數據集名稱</param> /// <param name="paramesList">報表參數集合</param> /// <param name="isHindeLogo">是否隱藏</param> /// <param name="msg">輸出信息</param> /// <param name="reportWidth">報表長度(單位:mm)</param> public void Run(string reportPath, string printerName, DataTable dt1, string dt1SourceName, List<Microsoft.Reporting.WebForms.ReportParameter> paramesList, bool isHindeLogo, out string msg, int reportWidth = 430) { LocalReport report = new LocalReport(); report.ReportPath = reportPath;//加上報表的路徑 report.DataSources.Add(new ReportDataSource(dt1SourceName, dt1)); report.EnableExternalImages = true; report.SetParameters(paramesList); //ReportParameter rp = new ReportParameter("isHindeLogoImg", isHindeLogo.ToString());//這里我在報表里弄的參數 //report.SetParameters(rp); isLandSapces = report.GetDefaultPageSettings().IsLandscape; Export(report, reportWidth); m_currentPageIndex = 0; Print(printerName, out msg); } /// <summary> /// /// </summary> /// <param name="report"></param> /// <param name="reportWidth">報表長度(單位:mm)</param> private void Export(LocalReport report,int reportWidth=430) { string deviceInfo = "<DeviceInfo>" + " <OutputFormat>EMF</OutputFormat>" + " <PageWidth>" + reportWidth + "mm</PageWidth>" + " <PageHeight>297mm</PageHeight>" + " <MarginTop>5mm</MarginTop>" + " <MarginLeft>10mm</MarginLeft>" + " <MarginRight>10mm</MarginRight>" + " <MarginBottom>5mm</MarginBottom>" + "</DeviceInfo>";//這里是設置打印的格式 邊距什么的 Warning[] warnings; m_streams = new List<Stream>(); try { report.Render("Image", deviceInfo, CreateStream, out warnings);//一般情況這里會出錯的 使用catch得到錯誤原因 一般都是簡單錯誤 } catch (Exception ex) { Exception innerEx = ex.InnerException;//取內異常。因為內異常的信息才有用,才能排除問題。 while (innerEx != null) { //MessageBox.Show(innerEx.Message); string errmessage = innerEx.Message; innerEx = innerEx.InnerException; } } foreach (Stream stream in m_streams) { stream.Position = 0; } } string ssss = "--"; private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { //name 需要進一步處理 Stream stream = null; try { stream = new FileStream("c:\\users\\administrator\\documents\\visual studio 2010\\Projects\\WebApplication1\\WebApplication1\\Report\\" + name + DateTime.Now.Millisecond + "." + fileNameExtension, FileMode.Create);//為文件名加上時間 m_streams.Add(stream); } catch (Exception ex) { ssss = ex.Message; } return stream; } private void Print(string printerName, out string msg) { msg = ""; //string printerName = this.TextBox1.Text.Trim();// "傳送至 OneNote 2007"; if (m_streams == null || m_streams.Count == 0) { msg = "沒有要打印的數據,請重試"; return; } PrintDocument printDoc = new PrintDocument(); if (printerName.Length > 0) { printDoc.PrinterSettings.PrinterName = printerName; printDoc.DefaultPageSettings.Landscape = isLandSapces; } foreach (PaperSize ps in printDoc.PrinterSettings.PaperSizes) { if (ps.PaperName == "A4") { printDoc.DefaultPageSettings.PaperSize = ps; // printDoc.PrinterSettings.IsDefaultPrinter;//知道是否是預設定的打印機 } } if (!printDoc.PrinterSettings.IsValid) { msg = String.Format("找不到打印機: " + printDoc.PrinterSettings.PrinterName); System.Diagnostics.Debug.WriteLine(msg); return; } printDoc.PrintPage += new PrintPageEventHandler(PrintPage); printDoc.Print(); } private void PrintPage(object sender, PrintPageEventArgs ev) { Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); //設置高質量插值法 ev.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設置高質量,低速度呈現平滑程度 ev.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; ev.Graphics.DrawImage(pageImage, new System.Drawing.Rectangle(0, 0, ev.PageBounds.Width, ev.PageBounds.Height), new System.Drawing.Rectangle(0, 0, pageImage.Width, pageImage.Height), System.Drawing.GraphicsUnit.Pixel); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); }
調用: new PrintHelper().Run(reportPath, printName, dtRep, "EleCostDetails", paramsList, true, out msg, reportWidth);
注意:
在report.Render()的時候,如果設置的reportWidth長度不夠,則會生成幾張拼接的圖片,那么在打印的時候,不管怎么設置,打印出來的也是列表欠缺的報表,如上圖所示。
public void Render(string format, string deviceInfo, CreateStreamCallback createStream, out Warning[] warnings);微軟介紹與例子:https://msdn.microsoft.com/zh-cn/library/ms252172.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2
CreateStreamCallback委托(為 ReportViewer 控件提供流以進行呈現)。 微軟的介紹: https://msdn.microsoft.com/zh-cn/library/microsoft.reporting.winforms.createstreamcallback(VS.80).aspx
附上PrinterSettings類的微軟介紹:https://msdn.microsoft.com/zh-cn/library/system.drawing.printing.printersettings_members(v=vs.85).aspx