Я пытаюсь создать отчет о кристалле. Для внешнего интерфейса веб-приложения я использую Angular, и я вызываю веб-API для создания отчета.
Как получить все записи из таблицы Customer и выполнить отчет Crystal. (Обратите внимание, что я использую таблицу клиентов для хранения требований клиента.)
Crystal Report, снимок экрана обозревателя решений
- вот мой код (серверная часть). (ReportsController.cs)
using CrystalDecisions.CrystalReports.Engine;
using ElephasWebAPI.Models;
using ElephasWebAPI.Reports;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Hosting;
using System.Web.Http;
using System.Web.Http.Cors;
namespace ElephasWebAPI.Controllers
{
public class ReportsController : ApiController
{
[HttpGet]
public HttpResponseMessage downloadReport() // I want pass the id of the customer(AKA customer requirement) here. for now I just keep it without parameters
{
int selection = 1;
int type = 1;
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
//DBModel db = new DBModel();
DBModel db = new DBModel();
db.Configuration.ProxyCreationEnabled = true;
List<Customer> customers = new List<Customer>();
if (selection == 1)
{
/*
* I want to get the particular record from the Customer table
* JUST LIKE THIS
* SELECT * FROM Customer WHERE ID == 2
*
*
* AND I also want to get (I am going to generate another report later)
* SELECT * FROM Customer
* */
//customers = db.Customers.Where(c => c.ID == 1).ToList();
/*
customers = db.Customers.Include(c => c.ID).Include(c => c.Name).
Include(c => c.Nationality).Include(c => c.PassportNo).Include(c => c.Phone)
.Include(c => c.Email).Include(c => c.NoDays).Include(c => c.NoPeople).Include(c => c.NoChildren)
.Include(c => c.ArrivalDate).Include(c => c.DepartureDate).Include(c => c.StarCategory)
.Include(c => c.Remarks).Include(c => c.TourExecutive).Include(c => c.ExchangeRate).Include(c => c.Status).ToList();
*/
}
return getCustomerRequirementFile(customers, type);
}
private HttpResponseMessage getCustomerRequirementFile(List<Customer> customers, int type)
{
ReportDocument report = new ReportDocument();
report.Load(Path.Combine(HostingEnvironment.MapPath("~/Reports/CrystalReportRequirements.rpt")));
CusRequirementDataSet model = new CusRequirementDataSet();
foreach (Customer customer in customers)
{
DataRow row = model.Customer.NewRow();
row["ID"] = customer.ID;
row["Name"] = customer.Name;
row["Nationality"] = customer.Nationality;
row["PassportNo"] = customer.PassportNo;
row["Phone"] = customer.Phone;
row["Email"] = customer.Email;
row["NoDays"] = customer.NoDays;
row["NoPeople"] = customer.NoPeople;
row["NoChildren"] = customer.NoChildren;
row["ArrivalDate"] = customer.ArrivalDate;
row["DepartureDate"] = customer.DepartureDate;
row["StarCategory"] = customer.StarCategory;
row["DepartureDate"] = customer.DepartureDate;
row["Remarks"] = customer.Remarks;
row["TourExecutive"] = customer.TourExecutive;
row["Status"] = customer.Status;
model.Customer.Rows.Add(row);
}
report.SetDataSource(model);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
if (type == 1)
{
Stream stream = report.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
stream.Seek(0,SeekOrigin.Begin);
httpResponseMessage.Content = new StreamContent(stream);
httpResponseMessage.Content.Headers.Add("x-filename", "Report.pdf");
httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = "CustomerRequirements.pdf";
}
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
}
}