2. Use Crystal Report in ASP. net mvc, 2. asp. netmvc

Source: Internet
Author: User
Tags actionlink

2. Use Crystal Report in ASP. net mvc, 2. asp. netmvc

The previous article describes how to export an Excel file. This article describes how to use Crystal Reports in ASP. net mvc.

Project source code download: https://github.com/caofangsheng93/CrystalReportInMac

Prerequisites: You need VS. Of course, the most important thing about SQL Server is to install Crystal Report.

Here I provide the installation file for my Baidu Network Disk: http://pan.baidu.com/s/1bpck3zd. here is the version of Crystal Report for VS2013. You can search for other versions to download them.

 

After the environment is properly matched, we will start to work. We will first create a database:

USE master go if exists (SELECT * FROM sysdatabases WHERE name = 'mermerdb') drop database CustomerDBGO create database CustomerDBGO USE CustomerDBGO if exists (SELECT * FROM sysobjects WHERE name = 'customer ') drop table CustomersGO create table Customers (CustomerID int not null primary key identity (1, 1), CustomerName NVARCHAR (50) NULL, CustomerEmail NVARCHAR (50) NULL, CustomerZipCode int null, customerCountry NVARCHAR (50) NULL, CustomerCity NVARCHAR (50) NULL) -- INSERT test data INTO dbo. customersSELECT N 'Li Yifeng ', 'Li Yifeng @ 163.com', '000000', N 'China', N 'shenzhen 'union allselect n' sun shangxiang', 'Sun shangxiang @ 163.com ', '123', n' China', n' Shanghai' union allselect n 'liling Wang ', 'liling Wang @ 163.com', '123', n' China ', N 'beijing' union allselect n' Sun Wukong ', 'Sun Wukong @ 163.com', '000000', N 'China', N 'wuhan 'union allselect n 'cao Cao ', 'cao Cao @ 163.com ', '123', n'china', n'hangzhou'

 

Then, our project is as follows:

 

After the database is ready, we create an ADO. NET object data model named casually. Here we name it DbContextCustomer.

 

 

The next step is to create a new Crystal Report:

 

Next, window will pop up as given below, in this example we are going to choose "As a Blank Report" option, and click OK.

 

After clicking OK, our Crystal Report has been created with success. The next step is to right click on Database Fields> Database Expert...

 

Now, a new window will pop up as shown below. We need to select model which will be using to display data (in this case our model is Customer ).

After clicking OK, we proceed to drag all fields to the report as shown below.

 

 

Then create a controller:

Public class CustomerController: Controller {private CustomerDBEntities context = new CustomerDBEntities (); public ActionResult Index () {var customerList = context. MERs. toList (); return View (customerList);} public ActionResult ExportCustomers () {List <Customer> allCustomer = new List <Customer> (); allCustomer = context. MERs. toList (); ReportDocument rd = new ReportDocument (); rd. load (Pat H. Combine (Server. MapPath ("~ /CrystalReports ")," ReportCustomer. rpt "); rd. setDataSource (ToDataTable <Customer> (allCustomer); Response. buffer = false; Response. clearContent (); Response. clearHeaders (); Stream stream = rd. exportToStream (CrystalDecisions. shared. exportFormatType. portableDocFormat); stream. seek (0, SeekOrigin. begin); return File (stream, "application/pdf", "CustomerList.pdf");} // <summary> // converts a generic collection class to a DataTable/ /// </Summary> /// <typeparam name = "T"> set item type </typeparam> /// <param name = "list"> set </param> /// <param name = "propertyName"> name of the column to be returned </param> /// <returns> dataset (table) </returns> public static DataTable ToDataTable <T> (IList <T> list, params string [] propertyName) {List <string> propertyNameList = new List <string> (); if (propertyName! = Null) propertyNameList. addRange (propertyName); DataTable result = new DataTable (); if (list. count> 0) {PropertyInfo [] propertys = list [0]. getType (). getProperties (); foreach (PropertyInfo pi in propertys) {if (propertyNameList. count = 0) {result. columns. add (pi. name, pi. propertyType);} else {if (propertyNameList. contains (pi. name) result. columns. add (pi. name, pi. propertyType) ;}}for (int I = 0; I <list. count; I ++) {ArrayList tempList = new ArrayList (); foreach (PropertyInfo pi in propertys) {if (propertyNameList. count = 0) {object obj = pi. getValue (list [I], null); tempList. add (obj);} else {if (propertyNameList. contains (pi. name) {object obj = pi. getValue (list [I], null); tempList. add (obj) ;}} object [] array = tempList. toArray (); result. loadDataRow (array, true) ;}} return result ;}}

 

View code:

@ Model IEnumerable <CryStalReportInMvc. customer >@{ ViewBag. title = "Index" ;}< h2> Index  

:

 

 

Click Report PDF:

 

In this way, the report function is implemented.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.