Report报表学习系列五
今天写了一天的代码,关于报表的一些基本控件,回来不想再写了,直接COPY上,有详细的注释。主要内容有上一页,下一页,放大,缩小,导为EXCEL和PDF,打印。
这里再次感谢“蜡人张”,这里有很多功能是参考该大侠的博客后修改的,这里是他的文章链接:http://waxdoll.cnblogs.com/archive/2006/03/03/342435.html
代友:
namespace POSReport.ReportForm
{
public partial class FrmReportViewBase : Form
{
#region 构造函数
public FrmReportViewBase()
{
InitializeComponent();
//this.SetCtlDesc();
//LanguageChange.ChangeDisplay(this);
}
#endregion
#region 自定义报表按钮的操作
#region 返回报表第一页
/// <summary>
/// 回到报表的第一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnFirst_Click(object sender, EventArgs e)
{
this.rptViewMain.CurrentPage = 1;
}
#endregion
#region 跳转到报表最后一页
/// <summary>
/// 跳转到报表最后一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnLast_Click(object sender, EventArgs e)
{
this.rptViewMain.CurrentPage = this.rptViewMain.LocalReport.GetTotalPages();
}
#endregion
#region 上一页
/// <summary>
/// 上一页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPre_Click(object sender, EventArgs e)
{
if (this.rptViewMain.CurrentPage > 1)
{
this.rptViewMain.CurrentPage--;
}
}
#endregion
#region 下一页
private void btnNext_Click(object sender, EventArgs e)
{
if (this.rptViewMain.CurrentPage < this.rptViewMain.LocalReport.GetTotalPages())
{
this.rptViewMain.CurrentPage++;
}
}
#endregion
#region 放大
/// <summary>
///放大
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnEnlarge_Click(object sender, EventArgs e)
{
if (this.rptViewMain.ZoomPercent < maxPercent)
{
this.rptViewMain.ZoomPercent += addPercent;
}
}
#endregion
#region 缩小
/// <summary>
/// 缩小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReduce_Click(object sender, EventArgs e)
{
if (this.rptViewMain.ZoomPercent > minPercent)
{
this.rptViewMain.ZoomPercent -= addPercent;
}
}
#endregion
#region 关闭
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
#endregion
#region 导出为Excel或PDF
private void btnSaveDest_Click(object sender, EventArgs e)
{
try
{
Microsoft.Reporting.WinForms.Warning[] Warnings;
string[] strStreamIds;
string strMimeType;
string strEncoding;
string strFileNameExtension;
//文件形式
string fileType = "Excel";
string fileName = "";
SaveFileDialog sfd = new SaveFileDialog();
sfd.AddExtension = true;
sfd.Filter = "excel|*.xls|PDF|*.PDF";
if (sfd.ShowDialog() == DialogResult.OK)
{
if (string.IsNullOrEmpty(sfd.FileName))
{
return;
}
if (sfd.FilterIndex == 1)
{
fileType = "Excel";
}
else if (sfd.FilterIndex == 2)
{
fileType = "PDF";
}
else
{
fileType = "Excel";
}
fileName = sfd.FileName;
byte[] bytes = this.rptViewMain.LocalReport.Render(fileType, null, out strMimeType, out
strEncoding, out strFileNameExtension, out strStreamIds, out Warnings);
SaveFile(fileName, bytes);
}
}
catch (Exception ex)
{
msgError.GetExcepiton(ex, MODULENAME, Common.CommonBase.MsgError.Title.Hint);
}
}
#endregion
#region 打印
private void btnPrint_Click(object sender, EventArgs e)
{
try
{
Export(this.rptViewMain.LocalReport);
currentPageIndex = 0;
const string printerName = "Microsoft XPS Document Writer";
if (streams == null || streams.Count == 0)
return;
PrintDocument printDoc = new PrintDocument();
printDoc.PrinterSettings.PrinterName = printerName;
if (!printDoc.PrinterSettings.IsValid)
{
return;
}
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
printDoc.Print();
//消毁streams
if (streams != null)
{
Foreach(Stream stream in streams)
{
stream.Close();
}
}
}
catch (Exception ex)
{
msgError.GetExcepiton(ex, MODULENAME, MsgError.Title.Hint);
}
}
#endregion
#endregion
#region 报表事件
private void rptViewMain_Load(object sender, EventArgs e)
{
// 设置自定义报表按钮是否为可用
SetControlVisble();
//设置报表的缩放模式
this.rptViewMain.ZoomMode = ZoomMode.Percent;
}
#region 报表的显示页号改变事件
private void rptViewMain_PageNavigation(object sender, PageNavigationEventArgs e)
{
// 设置自定义报表按钮是否为可用
SetControlVisble();
}
#endregion
#region 报表显示的百分比改变事件
private void rptViewMain_ZoomChange(object sender, ZoomChangeEventArgs e)
{
if (this.rptViewMain.ZoomPercent >= maxPercent)
{
this.btnEnlarge.Enabled = false;
}
else
{
this.btnEnlarge.Enabled = true;
}
if (this.rptViewMain.ZoomPercent <= minPercent)
{
this.btnReduce.Enabled = false;
}
else
{
this.btnReduce.Enabled = true;
}
}
#endregion
#endregion
#region 方法
#region 设置自定义报表按钮是否为可用
/// <summary>
/// 设置自定义报表按钮是否为可用
/// </summary>.
private void SetControlVisble()
{
if (this.rptViewMain.LocalReport.GetTotalPages() <= 1)
{
this.btnFirst.Enabled = false;
this.btnLast.Enabled = false;
this.btnNext.Enabled = false;
this.btnPre.Enabled = false;
return;
}
if (this.rptViewMain.CurrentPage >= this.rptViewMain.LocalReport.GetTotalPages() - 1)
{
this.btnNext.Enabled = false;
this.btnLast.Enabled = false;
}
else
{
this.btnLast.Enabled = true;
this.btnNext.Enabled = true;
}
if (this.rptViewMain.CurrentPage <= 0)
{
this.btnFirst.Enabled = false;
this.btnPre.Enabled = false;
}
else
{
this.btnPre.Enabled = true;
this.btnFirst.Enabled = true;
}
}
#endregion
#region 保存
protected void SaveFile(string fileName, byte[] bytes)
{
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Create))
{
fs.Write(bytes, 0, bytes.Length);
}
}
catch (IOException ex)
{
return;
}
}
#endregion
#region 创建文件流
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool
willSeek)
{
stream = new FileStream(name + "." + fileNameExtension, FileMode.Create);
streams.Add(stream);
name = string.Empty;
fileNameExtension = string.Empty;
return stream;
}
#endregion
#region 将本地报表导出为文件流
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;
streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream, out warnings);
foreach (Stream stream in streams)
{
stream.Position = 0;
}
}
#endregion
#region Page打印方法
private void PrintPage(object sender, PrintPageEventArgs e)
{
Metafile pageImage = new Metafile(streams[currentPageIndex]);
e.Graphics.DrawImage(pageImage, e.PageBounds);
currentPageIndex++;
e.HasMorePages = (currentPageIndex < streams.Count);
}
#endregion
#endregion
#region 私有成员
//报表显示的百分比的增量因子
private const int addPercent = 25;
//报表显示的最大百分比
private const int maxPercent = 200;
private const int minPercent = 25;
private MsgError msgError = MsgError.getInstance();//实例化错误异常提示类
private const string MODULENAME = "FrmReportViewBase"; //模块名称
//当前页的索引
private int currentPageIndex;
//打印需要的文件流序列
private IList<Stream> streams;
#endregion
}
}