C#如何直接调用rdlc报表进行打印
1个回答
展开全部
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(); }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询