C#如何直接调用rdlc报表进行打印

 我来答
浮生梦魇XZM
2013-08-25 · 超过74用户采纳过TA的回答
知道答主
回答量:139
采纳率:100%
帮助的人:58.4万
展开全部
using Microsoft.Reporting.WebForms;using System.Text;using System.Drawing.Printing;using System.Drawing.Imaging;using System.Configuration; //必须引用的几个命名空间 private void btnPrint_Click(object sender, EventArgs e) { Run(); } private int m_currentPageIndex; private IList<Stream> m_streams; private DataTable LoadSalesData() { //以下是取得数据源,可以自由丛晌发挥 SqlConnection con =new SqlConnection( System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand cmd = new SqlCommand("SELECT * FROM Employees", con); SqlDataAdapter adp = new SqlDataAdapter(); adp.SelectCommand = cmd; con.Open(); DataTable dt = new DataTable(); adp.Fill(dt); con.Close(); return dt; } private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) { Stream stream = new FileStream(name + "." + fileNameExtension, FileMode.Create); m_streams.Add(stream); return stream; } private void Export(LocalReport report) { //设置棚郑手导出的报表基本显示的位置格式PageWidth、PageHeight、MarginTop、MarginLeft、MarginRight string 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; m_streams = new List<Stream>(); try { report.Render("Image", deviceInfo, CreateStream, out warnings); } catch (Exception ex) { Exception innerEx = ex.InnerException;//取内异常。因为内异常的信息才有用,才能排除问题。 while (innerEx != null) { label1.Text= innerEx.Message; // 有异常最好断点此调试容易发现问题 innerEx = innerEx.InnerException; } } foreach (Stream stream in m_streams) stream.Position = 0; } private void PrintPage(object sender, PrintPageEventArgs ev) { Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]); ev.Graphics.DrawImage(pageImage, 0, 0); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } private static PrintDocument fPrintDocument = new PrintDocument(); private void Print() { string printerName = fPrintDocument.PrinterSettings.PrinterName; //取得本地的默认打印机 if (m_streams == null || m_streams.Count == 0) return; PrintDocument printDoc = new PrintDocument(); printDoc.PrinterSettings.PrinterName = printerName; if (!printDoc.PrinterSettings.IsValid) { //打印机不可用,直接返回 return; } printDoc.PrintPage += new PrintPageEventHandler(PrintPage); printDoc.Print(); } private void Run() { LocalReport report = new LocalReport(); string spath = ConfigurationManager.AppSettings["ServerFilePath"].ToString(); report.ReportPath =spath ;//报表存放的路径 report.DataSources.Add(new ReportDataSource("NorthwindDataSet_Employees", LoadSalesData())); // 其中NorthwindDataSet_Employees 代表的是做的RDLC报表的数据源 Export(report); m_currentPageIndex = 0; Print(); }
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式