转载: ReportViewer : RDLC自定义工具栏
RDLC自定义工具栏:
既然我们使用这种方法进行报表的打印,那么Visual Studio的控件ReportViewer的工具栏就不再符合我们的要求了。因为这个报表浏览器的工具栏上的按钮虽然可以设置属性显示或隐藏其中的一部分,但是我们却不能自己往这个工具栏上添加按钮(显然,我们需要实现自己的页面设置、预览和打印按钮),在这一点上,建议Microsoft将工具栏和报表浏览器分离,应该做得和BindingNavigator那样就好了。
我们先设置ReportViewer控件的ShowToolBar方法为false,然后在ReportViewer控件纸上添加除页面设置、预览、打印外的应该有的按钮,像刷新、终止、导出、缩放、搜索、导航等,这些按钮的Click事件定义如下:
/// <summary>
/// 获取当前时间组成的字符串,用作生成不会重复的文件名
/// </summary>
/// <returns></returns>
private string GetTimeStamp()
{
string strRet = string.Empty;
System.DateTime dtNow = Pub.DateTimeEx.ServerTime;
strRet += dtNow.Year.ToString() +
dtNow.Month.ToString("00") +
dtNow.Day.ToString("00") +
dtNow.Hour.ToString("00") +
dtNow.Minute.ToString("00") +
dtNow.Second.ToString("00") +
System.DateTime.Now.Millisecond.ToString("000");
return strRet;
}
/// <summary>
/// 导出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolExcel_Click(object sender, EventArgs e)
{
Microsoft.Reporting.WinForms.Warning[] Warnings;
string[] strStreamIds;
string strMimeType;
string strEncoding;
string strFileNameExtension;
byte[] bytes = this.rptViewer.LocalReport.Render("Excel", null, out strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);
string strFilePath = @"D:\" + this.GetTimeStamp() + ".xls";
using (System.IO.FileStream fs = new FileStream(strFilePath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
if (Pub.WinForm.Msg.Question("报表打印: \r\n 成功导出Excel文件!" + strFilePath + "\r\n 要现在打开文件" + strFilePath + "吗?") == DialogResult.Yes)
{
System.Diagnostics.Process.Start(strFilePath);
}
}
/// <summary>
/// 刷新报表数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool刷新_Click(object sender, EventArgs e)
{
this.rptViewer.RefreshReport();
}
/// <summary>
/// 在加载报表数据时终止报表数据的加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool终止_Click(object sender, EventArgs e)
{
this.rptViewer.CancelRendering(0);
}
/// <summary>
/// 从DrillThrough报表返回到导航页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool返回_Click(object sender, EventArgs e)
{
if (this.rptViewer.LocalReport.IsDrillthroughReport)
this.rptViewer.PerformBack();
}
/// <summary>
/// 回到报表的第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool第一页_Click(object sender, EventArgs e)
{
this.rptViewer.CurrentPage = 1;
}
/// <summary>
/// 跳转到报表的最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool最后一页_Click(object sender, EventArgs e)
{
this.rptViewer.CurrentPage = this.rptViewer.LocalReport.GetTotalPages();
}
/// <summary>
/// 以25%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool25_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 25;
}
/// <summary>
/// 以50%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool50_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 50;
}
/// <summary>
/// 以100%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool100_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 100;
}
/// <summary>
/// 以200%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool200_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 200;
}
/// <summary>
/// 以400%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool400_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 400;
}
/// <summary>
/// 将缩放模式设置为整页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool整页_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.FullPage;
}
/// <summary>
/// 将缩放模式设置为页宽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool页宽_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.PageWidth;
}
/// <summary>
/// 在报表中搜索txtSearch中的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool搜索_Click(object sender, EventArgs e)
{
if (this.txtSearch.Text.Trim() == string.Empty)
return;
this.rptViewer.Find(this.txtSearch.Text.Trim(), 1);
}
/// <summary>
/// 搜索报表中下一处txtSearch中的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool搜索下一个_Click(object sender, EventArgs e)
{
if (this.txtSearch.Text.Trim() == string.Empty)
return;
this.rptViewer.FindNext();
}
/// <summary>
/// 跳转到上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool上一页_Click(object sender, EventArgs e)
{
if (this.rptViewer.CurrentPage != 1)
this.rptViewer.CurrentPage--;
}
/// <summary>
/// 跳转到下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool下一页_Click(object sender, EventArgs e)
{
if (this.rptViewer.CurrentPage != this.rptViewer.LocalReport.GetTotalPages())
this.rptViewer.CurrentPage++;
}
/// <summary>
/// 跳转到由txt跳转中指定的页数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool跳转_Click(object sender, EventArgs e)
{
if (this.txt跳转.Text.Trim() == string.Empty)
return;
int intJump = 0;
if (System.Int32.TryParse(this.txt跳转.Text.Trim(), out intJump))
if (intJump <= this.rptViewer.LocalReport.GetTotalPages())
this.rptViewer.CurrentPage = intJump;
}
/// 获取当前时间组成的字符串,用作生成不会重复的文件名
/// </summary>
/// <returns></returns>
private string GetTimeStamp()
{
string strRet = string.Empty;
System.DateTime dtNow = Pub.DateTimeEx.ServerTime;
strRet += dtNow.Year.ToString() +
dtNow.Month.ToString("00") +
dtNow.Day.ToString("00") +
dtNow.Hour.ToString("00") +
dtNow.Minute.ToString("00") +
dtNow.Second.ToString("00") +
System.DateTime.Now.Millisecond.ToString("000");
return strRet;
}
/// <summary>
/// 导出到Excel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolExcel_Click(object sender, EventArgs e)
{
Microsoft.Reporting.WinForms.Warning[] Warnings;
string[] strStreamIds;
string strMimeType;
string strEncoding;
string strFileNameExtension;
byte[] bytes = this.rptViewer.LocalReport.Render("Excel", null, out strMimeType, out strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);
string strFilePath = @"D:\" + this.GetTimeStamp() + ".xls";
using (System.IO.FileStream fs = new FileStream(strFilePath, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
if (Pub.WinForm.Msg.Question("报表打印: \r\n 成功导出Excel文件!" + strFilePath + "\r\n 要现在打开文件" + strFilePath + "吗?") == DialogResult.Yes)
{
System.Diagnostics.Process.Start(strFilePath);
}
}
/// <summary>
/// 刷新报表数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool刷新_Click(object sender, EventArgs e)
{
this.rptViewer.RefreshReport();
}
/// <summary>
/// 在加载报表数据时终止报表数据的加载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool终止_Click(object sender, EventArgs e)
{
this.rptViewer.CancelRendering(0);
}
/// <summary>
/// 从DrillThrough报表返回到导航页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool返回_Click(object sender, EventArgs e)
{
if (this.rptViewer.LocalReport.IsDrillthroughReport)
this.rptViewer.PerformBack();
}
/// <summary>
/// 回到报表的第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool第一页_Click(object sender, EventArgs e)
{
this.rptViewer.CurrentPage = 1;
}
/// <summary>
/// 跳转到报表的最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool最后一页_Click(object sender, EventArgs e)
{
this.rptViewer.CurrentPage = this.rptViewer.LocalReport.GetTotalPages();
}
/// <summary>
/// 以25%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool25_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 25;
}
/// <summary>
/// 以50%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool50_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 50;
}
/// <summary>
/// 以100%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool100_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 100;
}
/// <summary>
/// 以200%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool200_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 200;
}
/// <summary>
/// 以400%的比例显示报表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool400_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.Percent;
this.rptViewer.ZoomPercent = 400;
}
/// <summary>
/// 将缩放模式设置为整页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool整页_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.FullPage;
}
/// <summary>
/// 将缩放模式设置为页宽
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool页宽_Click(object sender, EventArgs e)
{
this.rptViewer.ZoomMode = ZoomMode.PageWidth;
}
/// <summary>
/// 在报表中搜索txtSearch中的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool搜索_Click(object sender, EventArgs e)
{
if (this.txtSearch.Text.Trim() == string.Empty)
return;
this.rptViewer.Find(this.txtSearch.Text.Trim(), 1);
}
/// <summary>
/// 搜索报表中下一处txtSearch中的字符
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool搜索下一个_Click(object sender, EventArgs e)
{
if (this.txtSearch.Text.Trim() == string.Empty)
return;
this.rptViewer.FindNext();
}
/// <summary>
/// 跳转到上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool上一页_Click(object sender, EventArgs e)
{
if (this.rptViewer.CurrentPage != 1)
this.rptViewer.CurrentPage--;
}
/// <summary>
/// 跳转到下一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool下一页_Click(object sender, EventArgs e)
{
if (this.rptViewer.CurrentPage != this.rptViewer.LocalReport.GetTotalPages())
this.rptViewer.CurrentPage++;
}
/// <summary>
/// 跳转到由txt跳转中指定的页数
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tool跳转_Click(object sender, EventArgs e)
{
if (this.txt跳转.Text.Trim() == string.Empty)
return;
int intJump = 0;
if (System.Int32.TryParse(this.txt跳转.Text.Trim(), out intJump))
if (intJump <= this.rptViewer.LocalReport.GetTotalPages())
this.rptViewer.CurrentPage = intJump;
}
未完...