Wednesday, May 9, 2012

Hướng dẫn tạo Report bằng Crystal Report trong Visual Studio 2010

Download:
-          Tải bài viết này ở dạng file tại đây.
-          Tải Project mẫu tại đây.
-          Tải Database Script của project tại đây.
Download Crytal Reports cho Visual studio 2010
- Downloads tại địa chỉ :
http://www.businessobjects.com/jump/...s2_default.asp

- Giải nén và cài đặt file CRforVS_XX_XX

Tạo project WPF Aplication trong Visual studio 2010


Nhấn F5 chạy chương trình sẽ có được Windows như sau:
PHP Code:
<Window x:Class="CrytalReport_Tutorial.MainWindow"
        
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        
Title="In danh sách môn học" Height="428" Width="605">
    <
Grid>
        <
ComboBox Height="26" HorizontalAlignment="Left" Margin="76,353,0,0" Name="cboNganh" VerticalAlignment="Top" Width="206" />
        <
Button Content="Xem" Height="26" HorizontalAlignment="Left" Margin="300,353,0,0" Name="cmdXem" VerticalAlignment="Top" Width="80" />
        <
Button Content="In" Height="26" HorizontalAlignment="Left" Margin="390,353,0,0" Name="cmdIn" VerticalAlignment="Top" Width="80" />
        <
Button Content="Thoát" Height="26" HorizontalAlignment="Left" Margin="480,353,0,0" Name="cmdThoat" VerticalAlignment="Top" Width="80" />
        <
Label Content="Ngành" Height="26" HorizontalAlignment="Left" Margin="13,352,0,0" Name="label1" VerticalAlignment="Top" Width="52" />
    </
Grid>
</
Window> 
Add CrytalReportViewer vào Toolbox
Click phải chuột bất kỳ tại vị trí nào trên Toolbox sau đó chọn Choos Items..


Vẽ CrystalReportViewer vào Window (giống như Button, Listbox, ComboBox…)
PHP Code:
<Window x:Class="CrytalReport_Tutorial.MainWindow"
        
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        
Title="In danh sách môn học" Height="428" Width="605" xmlns:my="clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=SAPBusinessObjects.WPF.Viewer">
    <
Grid>
        <
ComboBox Height="26" HorizontalAlignment="Left" Margin="76,353,0,0" Name="cboNganh" VerticalAlignment="Top" Width="206" />
        <
Button Content="Xem" Height="26" HorizontalAlignment="Left" Margin="300,353,0,0" Name="cmdXem" VerticalAlignment="Top" Width="80" />
        <
Button Content="In" Height="26" HorizontalAlignment="Left" Margin="390,353,0,0" Name="cmdIn" VerticalAlignment="Top" Width="80" />
        <
Button Content="Thoát" Height="26" HorizontalAlignment="Left" Margin="480,353,0,0" Name="cmdThoat" VerticalAlignment="Top" Width="80" />
        <
Label Content="Ngành" Height="26" HorizontalAlignment="Left" Margin="13,352,0,0" Name="label1" VerticalAlignment="Top" Width="52" />
        <
my:CrystalReportsViewer Name="crystalReportsViewer"  Height="340" VerticalAlignment="Top" />
    </
Grid>
</
Window> 
Nhấn F5 chạy chương trình sẽ gặp một lỗi như sau

Error 4 The tag 'CrystalReportsViewer' does not exist in XML namespace 'clr-namespace:SAPBusinessObjects.WPF.Viewer;assembly=S APBusinessObjects.WPF.Viewer'. Line 11 Position 10. E:\CrytalReport_Tutorial\CrytalReport_Tutorial\Mai nWindow.xaml

Giải quyết lỗi trên như sau:

-Click phải chuột trên project CrytalReport_Tutorial chọn Properties, đổi Target Framework từ .NET Framework 4 Client Profile thành .NET Framework 4



Nhấn F5 chạy chương trình sẽ được màn hình như sau:



Tạo mới một file Report
Click phải chuột trên project CrytalReport_Tutorial chọn Add sau đó chọn New Items…





Tạo một xự kiện cho Button Xem có nội dung như sau:



Tao database
PHP Code:
create database qlsv
go
use qlsv
go
create table nganh
(
        
manganh  nvarchar(30),
        
tennganh  nvarchar(255)
)
go
create table monhoc
(
        
mamh  nvarchar(30),
        
tenmh  nvarchar(255),
        
manganh  nvarchar(30),
)
go 
insert into nganh values
('N01', 'Cong nghe thong tin')
insert into nganh values('N02', 'Ke toan kiem toan')
insert into nganh values('N03', 'Dien tu')

insert into monhoc values('M01', 'Ky thuat lap trinh','N01')
insert into monhoc values('M02', 'Co so du lieu','N01' )
insert into monhoc values('M03', 'Lap trinh huong doi tuong','N01' )
insert into monhoc values('M04', 'Tri tue nhan tao','N01')
insert into monhoc values('M05', 'Lap trinh web','N01' )
insert into monhoc values('M06', 'Phan tich thiet ke he thong','N01' )
insert into monhoc values('M07', 'He quan tri CSDL','N01')
insert into monhoc values('M08', 'Chuyen de Java','N01' )
insert into monhoc values('M09', 'Lap trinh do hoa','N01' )
insert into monhoc values('M10', 'Cao truc du lieu','N01' )

insert into monhoc values('M11', 'Nguyen ly ke toan','N02')
insert into monhoc values('M12', 'Ke toan san xuat','N02' )
insert into monhoc values('M13', 'Tai chinh doanh nghiep','N02' )
insert into monhoc values('M14', 'Kinh te vi mo','N02')
insert into monhoc values('M15', 'Phan tich bao cao tai chinh','N02' )
insert into monhoc values('M16', 'Kinh te luong','N02' )
insert into monhoc values('M17', 'Tieng Anh thuong mai','N02')
insert into monhoc values('M18', 'Thue','N02' ) 


Sửa lại xự kiện của Button Xem như sau
PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
using System.Data;

namespace 
CrytalReport_Tutorial
{

    public 
partial class MainWindow : Window
    
{
        public 
MainWindow()
        {
            
InitializeComponent();
        }

        private 
void cmdXem_Click(object sender, RoutedEventArgs e)
        {
            
SqlConnection conn = new SqlConnection();
            
conn.ConnectionString = "Data Source=localhost;Initial Catalog=qlsv; User Id=sa; Password=sa";
            
SqlDataAdapter da = new SqlDataAdapter();
            
SqlCommand secmd = new SqlCommand("select * from monhoc", conn);
            
da.SelectCommand = secmd;
            
DataSet ds = new DataSet();
            
da.Fill(ds, "mh");    

            
ReportDocument report = new ReportDocument();
            
report.Load("..\\..\\DSMonHoc.rpt");
            
report.SetDataSource(ds);
            
crystalReportsViewer.ViewerCore.ReportSource = report;
        }
    }
} 
Tạo Dataset cho CrytalReport

Click phải chuột trên project CrytalReport_Tutorial chọn Add sau đó chọn New Items…















Lần luợt kéo các file trong dataset mh vào Detail của Report

Save và chạy chương trình

Click button Xem sẽ xuất hiện một lỗi như sau

Xử lý lỗi như sau:

PHP Code:
<?xml version="1.0"?>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
    <requiredRuntime version="v4.0.20506"/>
  </startup>
</configuration>




 
 


Để thuận tiện trong quá trình làm việc các Line Object nên thực hiện các thao tác như sau:
- Thay đổi Grid size : 0.01
- Tăng Zoom: 300








Tạo Fiel số thứ tự
Click phải chuột trên Running Toltal Fields sau đó chọn New…





Download:
-          Tải bài viết này ở dạng file tại đây.
-          Tải Project mẫu tại đây.
Tải Database Script của project tại đây.

No comments:

Post a Comment