使用 LocalReport 对象进行打印

Posted ChineseMoonGod

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用 LocalReport 对象进行打印相关的知识,希望对你有一定的参考价值。

  1. 从“项目”菜单中,选择“添加引用”。将显示“添加引用”对话框。

  2. 从“.NET”选项卡上显示的列表框中,选择 Winforms 和 Drawing 组件。

  1. 应打开 Program.cs 文件以供编辑。如果未打开,在“解决方案资源管理器”窗口中双击 Program.cs 文件。

  2. 使用以下代码替换 Program.cs 文件中的现有代码。确保使用本地计算机上示例报表的有效路径来替换报表引用。不要向项目添加 Data.xml 和 Report.rdlc。下面以编程方式访问这些文件。

using System;
using System.IO;
using System.Data;
using System.Text;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

public class Demo : IDisposable
{
    private int m_currentPageIndex;
    private IList<Stream> m_streams;

    private DataTable LoadSalesData()
    {
        DataSet dataSet = new DataSet();
        dataSet.ReadXml(@"c:\My Reports\data.xml");
        return dataSet.Tables[0];
    }

    private Stream CreateStream(string name, 
      string fileNameExtension, Encoding encoding,
      string mimeType, bool willSeek)
    {
    

//string strPath = @"D:\Report\";
//if (System.IO.Directory.Exists(strPath) == false)
//{
// System.IO.Directory.CreateDirectory(strPath);
//}
//string filenameext = DateTime.Now.Year.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
//string strFile = strPath + filenameext + name + "." + fileNameExtension;
//Stream stream = new FileStream(strFile, FileMode.Create);
//m_streams.Add(stream);
//return stream;



Stream stream
= new FileStream(@"c:\My Reports\" + name + "." + fileNameExtension, FileMode.Create); m_streams.Add(stream); return stream; } private void Export(LocalReport report) { 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>(); report.Render("Image", deviceInfo, CreateStream, out warnings); 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, ev.PageBounds); m_currentPageIndex++; ev.HasMorePages = (m_currentPageIndex < m_streams.Count); } private void Print() {

  

//if (m_streams == null || m_streams.Count == 0)
// return;
//PrintDocument printDoc = new PrintDocument();

//if (!printDoc.PrinterSettings.IsValid)
//{
// string msg = String.Format("Can‘t find printer \"{0}\".", "默认打印机!");
// MessageBox.Show(msg, "找不到默认打印机");
// return;
//}
//printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
//printDoc.Print();





const string printerName = "Microsoft Office Document Image Writer"; if (m_streams == null || m_streams.Count == 0) return; PrintDocument printDoc = new PrintDocument(); printDoc.PrinterSettings.PrinterName = printerName; if (!printDoc.PrinterSettings.IsValid) { string msg = String.Format("Can‘t find printer \"{0}\".", printerName); MessageBox.Show(msg, "Print Error"); return; } printDoc.PrintPage += new PrintPageEventHandler(PrintPage); printDoc.Print(); } private void Run() { LocalReport report = new LocalReport(); report.ReportPath = @"c:\My Reports\Report.rdlc"; report.DataSources.Add(new ReportDataSource("Sales", LoadSalesData())); Export(report); m_currentPageIndex = 0; Print(); } public void Dispose() { if (m_streams != null) { foreach (Stream stream in m_streams) stream.Close(); m_streams = null; } } public static void Main(string[] args) { using (Demo demo = new Demo()) { demo.Run(); } } }

 

参考:https://msdn.microsoft.com/zh-cn/library/ms252091(VS.80).aspx

以上是关于使用 LocalReport 对象进行打印的主要内容,如果未能解决你的问题,请参考以下文章

报告服务 LocalReport 和 WIF

python python中的漂亮(或漂亮打印)JSON对象具有这一功能。在尝试了解什么时,我总是使用这个片段

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

以编程方式将本地报告呈现给XPS

argparse 代码片段只打印部分日志

GLSL:使用片段着色器进行对象翻译