zoukankan      html  css  js  c++  java
  • winform 使用 ReportViewer做报表

    之前用过的水晶报表觉得有些麻烦,因此尝试了使用微软自带的报表。

    第一种方法是 在winform界面上放置ReportViewer界面,相关的代码如下:

    public DataTable dt;

    private void FormReport_Load(object sender, EventArgs e)
    {
    string sPath = "D:\bzj\MyBooks\MyBooks\report1.rdlc";

    this.reportViewer1.LocalReport.ReportPath = sPath;

    Microsoft.Reporting.WinForms.ReportDataSource reportDataSource2 = new

    Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable1", dt);

    reportViewer1.LocalReport.DataSources.Add(reportDataSource2);


    this.reportViewer1.RefreshReport();
    }

    说明:

    dt在初始化时传入到FormReport中,如下:

    ConfigFile m_ConfigFile = new ConfigFile();

    int iLength = m_ConfigFile.m_iBookNameLength;

    System.Data.DataTable dt = new System.Data.DataTable();
    dt.Columns.Add("Name");
    dt.Columns.Add("Code");
    dt.Columns.Add("VIP");

    for (int i = 0; i < listView1.Items.Count; i++)
    {
    string sName = "";
    string sCode = "";


    string str = listView1.Items[i].SubItems[2].Text;

    if (GetLength(str) > iLength)
    {
    sName = (GetSubString(str, 0, iLength));
    }
    else
    {
    sName = (str);
    }

    sCode = (listView1.Items[i].SubItems[1].Text);

    DataRow dr = dt.NewRow();
    dr[0] = sName;
    dr[1] = sCode;
    dr[2] = textBoxName.Text+"("+textBoxID.Text+")";
    dt.Rows.Add(dr);
    }

    FormReport mFormReport = new FormReport();
    mFormReport.dt = dt;


    mFormReport.ShowDialog();

    当然,之前需要做好的准备包括:

    1 创建数据集文件

    通过系列命令:项目 右键菜单 添加 新建项 数据 数据集  来创建数据集文件(*.xsd)

    在其中添加DataTable,并添加column

    2 创建数据集文件

    通过系列命令:项目 右键菜单 添加 新建项 reporting 报表  来创建rdlc文件(*.rdlc)

    在该文件中需要指定数据源为1中创建的xsd文件

    然后,就可以设计报表了。

    第二种方法(不需要显示报表控件,直接用代码完成打印)

    当然,还是需要设计好的rdlc文件,相关代码如下:

    private int m_currentPageIndex;

    /// <summary>
    /// 声明一个Stream对象的列表用来保存报表的输出数据,LocalReport对象的Render方法会将报表按页输出为多个Stream对象。
    /// </summary>
    private IList<Stream> m_streams;

    private void Print3()
    {
    System.Data.DataTable dt = GetDataTable();
    ReportViewer rvDoc = new ReportViewer();
    string sPath = string.Format("{0}\report1.rdlc", System.Windows.Forms.Application.StartupPath);
    rvDoc.LocalReport.ReportPath = sPath;//加上报表的路径
    rvDoc.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1_DataTable1", dt));
    PrintStream(rvDoc.LocalReport);
    }

    private System.Data.DataTable GetDataTable()
    {
    ConfigFile m_ConfigFile = new ConfigFile();

    int iLength = m_ConfigFile.m_iBookNameLength;

    System.Data.DataTable dt = new System.Data.DataTable();

    dt.Columns.Add("Name");
    dt.Columns.Add("Code");
    dt.Columns.Add("VIP");

    for (int i = 0; i < listView1.Items.Count; i++)
    {
    string sName = "";
    string sCode = "";


    string str = listView1.Items[i].SubItems[2].Text;

    if (GetLength(str) > iLength)
    {
    sName = (GetSubString(str, 0, iLength));
    }
    else
    {
    sName = (str);
    }

    sCode = (listView1.Items[i].SubItems[1].Text);


    DataRow dr = dt.NewRow();
    dr[0] = sName;
    dr[1] = sCode;
    dr[2] = textBoxName.Text + "(" + textBoxID.Text + ")";
    dt.Rows.Add(dr);
    }

    return dt;
    }

    private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
    //如果需要将报表输出的数据保存为文件,请使用FileStream对象。
    Stream stream = new MemoryStream();
    m_streams.Add(stream);
    return stream;
    }

    public void PrintStream(LocalReport rvDoc)
    {


    Export(rvDoc);
    PrintSetting();
    Dispose();
    }
    private void Export(LocalReport report)
    {
    string deviceInfo =
    "<DeviceInfo>" +
    " <OutputFormat>EMF</OutputFormat>" +
    " <PageWidth>3in</PageWidth>" +
    " <PageHeight>20in</PageHeight>" +
    " <MarginTop>0.1in</MarginTop>" +
    " <MarginLeft>0in</MarginLeft>" +
    " <MarginRight>0in</MarginRight>" +
    " <MarginBottom>0.1in</MarginBottom>" +
    "</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();
    //1111printDoc.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
    {

    //声明PrintDocument对象的PrintPage事件,具体的打印操作需要在这个事件中处理。
    printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
    m_currentPageIndex = 0;
    //设置打印机打印份数
    printDoc.PrinterSettings.Copies = 1;
    //执行打印操作,Print方法将触发PrintPage事件。
    printDoc.Print();
    }
    }
    private void PrintPage(object sender, PrintPageEventArgs ev)
    {
    //Metafile对象用来保存EMF或WMF格式的图形,
    //我们在前面将报表的内容输出为EMF图形格式的数据流。
    Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
    //指定是否横向打印
    ev.PageSettings.Landscape = false;



    ev.Graphics.DrawImage(pageImage, 0, 0);

    m_streams[m_currentPageIndex].Close();

    // 准备下一个页,已确定操作尚未结束
    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;
    }
    }

    参考了以下几篇文章:

    http://www.cnblogs.com/junjie94wan/archive/2013/09/24/3337364.html

    http://blog.163.com/xu_shuhao/blog/static/52577487201072284619646/

    http://www.cnblogs.com/ljx2012/p/4093474.html

  • 相关阅读:
    [Effective Java读书笔记] 第二章 创建和销毁对象(1~7)
    [Guava官方文档翻译] 5. Guava的Object公共方法 (Common Object Utilities Explained)
    [Guava官方文档翻译] 4. 使用Guava Ordering排序 (Ordering Explained)
    iOS正则表达式
    ios UIKit动力
    iOS8 UISearchViewController搜索功能讲解
    APP被苹果APPStore拒绝的各种原因
    iOS tableViewCell plane格式下,接近section边缘不显示分割线却被复用解决办法
    企业证书APP发布流程
    iOS越狱包
  • 原文地址:https://www.cnblogs.com/gaotaozhaolei/p/5576101.html
Copyright © 2011-2022 走看看