1. 首先当然是安装Crystal Report啦,不同的vs版本自带不同的CR,安装VS的时候选择安装就可以了。
  2. 然后就是新建一个report文件,该文件其实一个CrystalDocment类。一般常用的是使用PUSH模式,也就是利用DataSet作为Report文件数据源的方式。
  3. 新建一个DataSet,该DS里面可以存放很多表,这边不用跟你数据库的表一致,跟你要显示的内容一致就好了。举个例子,数据库有一个字段存放的是总价格,是浮点型,但是你的表单想显示的是中文的大写价格。当然有多种方法,其中的一种就是在DS里面的某个表添加一个string类型的字段来存放中文的大写价格。因为内容的填充是手动增加的。
  4. 新建一个窗体,拖放一个CrystalReportView控件,在代码中指定Report文件的位置,指定相应的DataSet,并给DataSet填充数据(因为一般作为报表,是需要显示符合某种条件的数据,例如某月份的营业额等等,手工指定数据的灵活性就体现出来了。)然后一点很重要的是要更新更改后的DataSet,利用AcceptChange()方法。 最后指定CrystalReportView.ReportSource属性为需要的DataSet即可。
  5. 至于报表显示的样式,就在report文件里面修改。可以很方便就是啦。能够使用css,但怎么用还没详细研究。 构想的一种设计方式是先在Word里面设计好了样式,再粘贴到report文件里就行了。

基本上报表的使用就是这样了。 附上一个步骤4的代码。

public partial class OrderReport : Form
    
{

        
private ReportDocument reportDoc = new ReportDocument();
        
//private string Path = Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf(@"\"));
        private string Path = Application.StartupPath;

        
private int _orderId = 0;
        
public int OrderId
        
{
            
get return _orderId; }
            
set { _orderId = value; }
        }


        
public OrderReport()
        
{
            InitializeComponent();
        }


        
private void crystalReportViewer1_Load(object sender, EventArgs e)
        
{
            ReportDAL reportingDAL 
= new ReportDAL();

            CompanysTableAdapter companyAdt 
= new CompanysTableAdapter();
            ProductsTableAdapter productAdt 
= new ProductsTableAdapter();
            OrdersTableAdapter orderAdt 
= new OrdersTableAdapter();
            OrderItemsTableAdapter orderitemAdt 
= new OrderItemsTableAdapter();

            Path 
= Path + @"\Report\";

            
try
            
{
                
string reportPath = Path + "OrderReport.rpt";
                reportDoc.Load(reportPath);

                
// Get Order
                OrderDs.OrdersDataTable orderTable = orderAdt.GetOneOrder(OrderId);
                
if (orderTable.Count < 1)
                
{
                    MessageBox.Show(
"订单不存在或订单信息读取失败!""打印送货单", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    
return;
                }

                reportingDAL.Orders.ImportRow(orderTable[
0]);
                reportingDAL.Orders[
0].OrderName = "NO:GB-2008-06-" + orderTable[0].OrderId.ToString();
                reportingDAL.Orders[
0].PriceName = ChineseNum.GetUpperMoney(Convert.ToDouble(orderTable[0].Price));

                
// Get Company Infos
                CompanyDs.CompanysDataTable companyTable = companyAdt.GetOneCompany(orderTable[0].CompanyId);

                reportingDAL.Companys.ImportRow(companyTable[
0]);

                
// Get Order Items Infos
                OrderDs.OrderItemsDataTable orderitemTable = orderitemAdt.GetOrderItemsByOrderId(OrderId);
                ProductDs.ProductsDataTable productTable;
                
foreach (OrderDs.OrderItemsRow row in orderitemTable)
                
{
                    reportingDAL.OrderItems.ImportRow(row);
                    productTable 
= productAdt.GetOneProduct(row.ProductId);
                    reportingDAL.OrderItems[reportingDAL.OrderItems.Count 
- 1].ProductName = productTable[0].ProductName;
                    reportingDAL.OrderItems[reportingDAL.OrderItems.Count 
- 1].Code = productTable[0].Code;
                    reportingDAL.OrderItems[reportingDAL.OrderItems.Count 
- 1].Price = productTable[0].Price.ToString("F2");
                    reportingDAL.OrderItems[reportingDAL.OrderItems.Count 
- 1].Spec = productTable[0].Spec;
                    reportingDAL.OrderItems[reportingDAL.OrderItems.Count 
- 1].OneProductTotalPrice = productTable[0].Price * row.Amount;
                }


                
// Accepte Changes
                reportingDAL.Orders.AcceptChanges();
                reportingDAL.Companys.AcceptChanges();
                reportingDAL.OrderItems.AcceptChanges();
                reportingDAL.Products.AcceptChanges();
                reportDoc.SetDataSource(reportingDAL);
                crystalReportViewer1.ReportSource 
= reportDoc;
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(
"加载报表出错: " + ex.Message, "加载报表", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }


        
private void OrderReport_FormClosing(object sender, FormClosingEventArgs e)
        
{
            reportDoc.Dispose();
        }

}
posted on 2008-06-20 19:11  rabbler  阅读(365)  评论(0编辑  收藏  举报