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
winform 使用 ReportViewer做报表的更多相关文章
- 在大型软件中用Word做报表: 书签的应用
本文转载:http://www.cnblogs.com/huyong/archive/2011/08/24/2151599.html 报表基本上在每一个项目中占有很大的比例,做报表也是我们开发人员必须 ...
- Winform开发框架之图表报表在线设计器-报表-SNF.EasyQuery项目--SNF快速开发平台3.3-+Spring.Net.Framework
带过项目和做过项目的人都知道,在客户现场客户的需求是百般多样的,今天要查销售出库情况,明天要看整个月的各部门销售情况,后天要查全年每个客户的项目金额.一直以前都有新需求,虽然会有售后收益,但如果有一个 ...
- 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了
引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中? 本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...
- orale做报表常用函数和表达式的总结
最近一段时间连续的做了几十张报表,通过原生sql对数据进行分析 ,也算是有了一定的了解,发现其中一些函数和表达式使用频率较高,现总结如下: (1).round()函数 round函数说白了就是把一 ...
- C# 利用ReportViewer生成报表
本文主要是利用微软自带的控件ReportViewer进行报表设计的小例子,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: ReportViewer :位于Microsoft.Reportin ...
- Winform开发框架之图表报表在线设计器2-图表-SNF.EasyQuery项目--SNF快速开发平台3.3-Spring.Net.Framework
上一篇讲到,如何快速创建报表程序了.这篇教大家如何快速制作图表报表. 继上一篇,Winform开发框架之图表报表在线设计器-报表 上一篇讲到如何了创建数据源,这里就不在介绍了.那我们就直接从图表设计器 ...
- 做报表需要知道的基本的SQL语句
为客户做报表需要操作数据库,基本的SQL是必须要掌握的,下面介绍做报表最常用的SQL语句. 方法/步骤 1 查询数据:编号表示查询顺序. (8) select (9) distinct (11 ...
- 【BIEE】使用BIPublisher做报表时,选择多个参数使用IN的问题
在使用BIPublisher做报表的时候,报表出现xml数据加载错误的情况 环境描述 仪表盘提示是表示变量,并且支持多选 报表使用xdo方式制作的,直接使用JDBC直连数据库获取数据 数据集中的SQL ...
- 5、VS2010+ASP.NET MVC4+EF4+JqueryEasyUI+Oracle该项目的开发——使用datagrid做报表
来点需要:我使用的数据库访问EF框架,但是,因为使用一个动态表来的统计报告中.单独是每天产生基于数据表,它是很难使用EF加盟前.所以我包装在两组数据库存取层的框内,一个是EF,一种是传统的ADO.NE ...
随机推荐
- hdoj 5074
Problem Description Hatsune Miku is a popular virtual singer. It is very popular in both Japan and C ...
- 前端工程师IE6兼容性问题随笔(未完待续)
1 height.在IE6下元素高度小于19px的时候,会被当做19px来处理.解决办法:用overflow:hidden;来处理.box{height:2px;background:red;over ...
- Java Mysql分页显示
public class View { private int currentPage; private int pageSize; private int recordCount; public V ...
- 支付宝APP支付后台参数生成Java版(一)
一.支付参数组装: String[] parameters={ "service=\"mobile.securitypay.pay\"",//固定值 " ...
- JAVA 中文转GBK内码方法
不能谷歌,百度了很久,没有直接的转换方法,参考 byte[]数组与十六进制字符串与字符串的互相转换 http://blog.163.com/roadwalker@126/blog/static/113 ...
- Unity3D之C# yield waitforseconds
Wait for seconds requires a couple things in order to work properly and chances are if it's not work ...
- 【转】Linux下如何清除系统日志
使用过Windows的人都知道,在使用windows系统的过程中系统会变得越来越慢.而对于Windows下饱受诟病的各种垃圾文件都需要自己想办法删除,不然系统将会变得越来越大,越来越迟钝!window ...
- 《我是一只IT小小鸟》读后感
过了半个学期的大学生活,说实话,我是迷茫的,因为我还没有足够的了解IT这门课程,也不知道怎么学好这门课程. 直到老师推荐我们读一本书<我是一只it小小鸟>,起初,我并不认为它是一本多么好的 ...
- PHP store session with couchbase
如何用couchbase存储session 有两种常见方式:1.采用memcache模式连接couchbase 只需两句修改: ini_set('session.save_handler', 'mem ...
- 【转】输入/输出流 - 深入理解Java中的流 (Stream)
基于流的数据读写,太抽象了,什么叫基于流,什么是流?Hadoop是Java语言写的,所以想理解好Hadoop的Streaming Data Access,还得从Java流机制入手.流机制也是JAVA及 ...