[英]Populating Crystal Report DataSource via Object and ExportToStream
我使用的是VS 2015,Crystal Report也是最新的SQL Server 2012。
我想通过viewmodel类填充Crystal Report数据源并将其导出为PDF
ReportDocument rptH = new ReportDocument();
rptH.FileName = Server.MapPath("~/reports/InvoiceReportsSample1.rpt");
var showInvoices =
(from ids in context.Invoices
where ids.InvoiceNumber == "01-04-2017-1"
select new Invoice_Report_ViewModel()
{
Invoiceid = ids.Invoiceid,
InvoiceNumber = ids.InvoiceNumber,
CustomerCompanyName = ids.CustomerCompanyName,
FirstBlankSpaceForPanel1 = ids.FirstBlankSpaceForPanel1,
MainDiscount = ids.MainDiscount,
MainTaxes = ids.MainTaxes,
MainTotal = ids.MainTotal,
TypeOfPortals = ids.TypeOfPortals,
TypeOfTickets = ids.TypeOfTickets,
SecondBlankSpaceForPanel2 = ids.SecondBlankSpaceForPanel2,
}).First();
var showInvoiceDetails = (from ids in context.InvoiceDetailses
where
ids.Invoiceid == showInvoices.Invoiceid
select new InvoiceDetails_Report_ViewModel()
{
}).ToList();
var querylist = new List<Invoice_Report_ViewModel> { showInvoices };
rptH.Database.Tables[0].SetDataSource(querylist);
rptH.Database.Tables[0].SetDataSource(showInvoiceDetails);
rptH.Refresh();
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
请帮助我得到一个错误
数据源登录失败
但是我没有将报表连接到数据库,而且我已经使用.NET对象配置了报表
请帮忙
我假设您正在尝试设置两个数据源。 您需要将它们设置为相关的数据表。 您可以尝试以下类似方法。
rptH.Database.Tables[0].SetDataSource(querylist);
rptH.Database.Tables[1].SetDataSource(showInvoiceDetails);
我遇到了此错误,首先您需要创建一个自定义DataTable,其中包含不同表中的字段。
那么您需要添加此代码。ToDatable来自ReportHelper
//domResult -> List of your View Model
DataTable dt = ReportHelper.ToDataTable(domResult);
// LCDraft_Domestic--> Crystal Report
LCDraft_Domestic rpt = new LCDraft_Domestic();
//My Data Table
rpt.Database.Tables["DraftData"].SetDataSource(dt);
为了避免Crystal报表引发的系统可为空的错误,您需要将列表查询转换为DataTable
创建一个静态的类名ReportHelper
public static class ReportHelper
{
public static DataTable ToDataTable<T>(this IList<T> items)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
foreach (T item in items)
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
return tb;
}
/// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type type)
{
return !type.IsValueType || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
/// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
public static Type GetCoreType(Type type)
{
if (type != null && IsNullable(type))
{
if (!type.IsValueType)
{
return type;
}
else
{
return Nullable.GetUnderlyingType(type);
}
}
else
{
return type;
}
}
static TableLogOnInfo crTableLogonInfo;
static ConnectionInfo crConnectionInfo;
static Tables crTables;
static Database crDatabase;
public static void ReportLogin(ReportDocument crDoc, string Server, string Database, string UserID, string Password)
{
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = Server;
crConnectionInfo.DatabaseName = Database;
crConnectionInfo.UserID = UserID;
crConnectionInfo.Password = Password;
crDatabase = crDoc.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
}
//No Login
public static void ReportLogin(ReportDocument crDoc, string Server, string Database)
{
crConnectionInfo = new ConnectionInfo();
crConnectionInfo.ServerName = Server;
crConnectionInfo.DatabaseName = Database;
crConnectionInfo.IntegratedSecurity = true;
crDatabase = crDoc.Database;
crTables = crDatabase.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
{
crTableLogonInfo = crTable.LogOnInfo;
crTableLogonInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo(crTableLogonInfo);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.