日期:2014-06-10  浏览次数:20753 次

一:水晶报表的安装

a,下载: http://downloads.businessobjects.com/akdlm/cr4vs2010/CRforVS_13_0_1.exe

b,安装,很简单,基本就是“下一步”到头,这里不再介绍。

二:如何在vs2010里使用

a,运行环境不能为.NET FRAMEWORK 4.0 CLINET PROFILE,要改为.NET FRAMEWORK 4,选中项目右键属性就可以更改了

b,在项目中加一个APP.CONFIG(这是针对WINFORM项目),如果是WEB项目就不用加了,里面有一个WEB.CONFIG;在这配
置文件中加上  <startup useLegacyV2RuntimeActivationPolicy="true">
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
这可以避免ADO.NET与水晶报表运行时由于.NET版本问题出错,如图:

2014-04-09_163422

c,下面就是新建一个水晶报表了,新建一个水晶报表.rpt,右击 解决方案的报表文件夹 CR  -> 添加-> 新建项
    从左侧“已安装的模板”列表中选 “Reporting”,右侧列表中选 “Crystal Reports”
    在下侧 “名称”处 输入希望的名称,比如: CrystalReport.rpt, 按“添加”进入。如图:

2014-04-10_194428

d,新建一个用放置报表的显示控件的框体.cs,拖放一个crystalReportViewer,如图:

2014-04-10_194911

e,设计水晶报表.rpt,右键单击数据字段,新建字段,找到数据源的字段,(当然可以为水晶报表新建一个专用的类,用于水晶报表.rpt的数据设计操作)如图加入字段,然后就是拖放新建的字段到水晶报表.rpt,在其他字段里新建其他需要的字段,并拖放到需要的位置。

2014-04-09_173155

2014-04-10_195306

 

三:实例截图

2014-04-10_200230

四:PrintSalarySheet.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRMSys.DAL;
using HRMSys.Model;

namespace HYMSys.UI.EmployeeMgr
{
    public partial class PrintSalarySheet : Form
    {
        public PrintSalarySheet()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 窗口自动载入事件,载入部门
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PrintSalarySheet_Load(object sender, EventArgs e)
        {
            DepartmentDAL dal = new DepartmentDAL();
            cb_depart.DataSource= dal.ListAll();
            cb_depart.DisplayMember = "Name";
            //cb_depart.SelectedValue = "Id";//这里应该写 cb_depart.ValueMember = "Id",SelectedValue现在为空,因为就没有选择
            cb_depart.ValueMember = "Id";
        }
        /// <summary>
        /// 搜索某年某月的工资表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_search_Click(object sender, EventArgs e)
        {
            //取得文本框里的值
            int year = Convert.ToInt32( tb_year.Text);
            int month = Convert.ToInt32(tb_month.Text);
            Guid id = (Guid)cb_depart.SelectedValue;
            //判断是否已经生成工资表
            SalarySheetDAL sheetDAL = new SalarySheetDAL();
            if (sheetDAL.IsExists(year, month, id) == false)
            {
                MessageBox.Show("还未生成工资!");
                return;
            }

           //得到员工的工资表,将信息转换位水晶报表的字段

            Guid sheetId = sheetDAL.GetYearMonthDepartSheetId(year, month, id);
            SalarySheetItemList[] items = sheetDAL.GetSalaryItems(sheetId);
            //声明一个水晶报表的显示字段
            SalarySheetItemRpt[] rptItems = new SalarySheetItemRpt[items.Length];
            for (int i = 0; i < items.Length; i++)
            {
                SalarySheetItemList item = items[i];
                SalarySheetItemRpt rptItem = new SalarySheetItemRpt();
                rptItem.BaseSalary = item.BaseSalary;
                rptItem.Bonus = item.Bonus;
                rptItem.Fine = item.Fine;
                rptItem.Other = item.Other;
                //select emp.Name join T_employee

                rptItem.EmployeeName = item.EmployeeName;
                rptItems[i] = rptItem;
            }
            //声明一个水晶报表类
            SalarySheetReport report = new SalarySheetReport();
            report.SetDataSource(rptItems);
            report.SetParameterValue("年", year);
            report.SetParameterValue("月", month);
            report.SetParameterValue("部门名称", cb_depart.SelectedText);

            crystalReportViewer1.ReportSource = report;

        }
    }
}

五:SalarySheetItemRpt.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HYMSys.UI.EmployeeMgr
{
    public class SalarySheetItemRpt
    {
        public decimal Bonus { get; set; }
        public decimal BaseSalary { get; set; }
        public decimal Fine { get; set; }
        public decimal Other { get; set; }
        public string EmployeeName { get; set; }
    }
}

六:App.config

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="constr" connectionString="server=.;database=HRMSysDB1;uid=sa;pwd=123123"/>
  </connectionStrings>
  <appSettings>
    <add key="PasswodSalt" value="Love@.&gt;1"/>
  </appSettings>
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

七:其中的报错处理

解决,将承载进程去到

八:总结

今天为止我的小的人事管理系统就完成了,经历了将近两个多月的时间,太不容易了,特别对于我这种菜鸟来说,这个是仿照一个WPF的小项目做的winform程序,做到这里我感觉自己有太多太多的知识不是理解的很透彻,唯有“纸上得来终觉浅,绝知此事要躬行”这样才能学好编程!在这里希望开发界的前辈多来来吐槽、批评和指导。-------红马車