[英]ASP.NET MVC Run Crystal Report straight to PDF bypassing Crystal viewer
What I'm trying to accomplish: - Run Crystal Report straight to PDF (bypassing the viewer) - It requires a database login. 我想要完成的任务: - 直接运行Crystal Report到PDF(绕过查看器) - 它需要数据库登录。 - It takes one parameter. - 它需要一个参数。 It shows as '@rpt_args' in CR 11 application. 它在CR 11应用程序中显示为“@rpt_args”。 - This Crystal report calls a Store procedure for it's result set. - 此Crystal报表为其结果集调用Store过程。
My code: 我的代码:
ReportClass rptH = new ReportClass();
//rptH.FileName = Server.MapPath("whkinvc.rpt");
rptH.FileName = "D:\\whkinvc.rpt";
rptH.Load();
rptH.SetDatabaseLogon("FAKEUSER", "FAKEPASSWORD", "FAKESERVER", "FAKEDB");
rptH.SetParameterValue("@rpt_args", atm.WorkingModel.Header.TicketNumber);
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
I'm currently getting a database login failure. 我目前正在收到数据库登录失败。 Do I have my code setup correctly to perform the action I'm looking for? 我是否正确设置了代码以执行我正在寻找的操作? Any possible reason for the database login failure? 数据库登录失败的任何可能原因?
Thanks, 谢谢,
** Update ** **更新**
I got it working through VS with the following code (The database login error was of result of a typo): 我使用以下代码通过VS工作(数据库登录错误是错误的结果):
ReportClass rptH = new ReportClass();
rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt"));
rptH.Load();
rptH.SetDatabaseLogon(...);
ParameterValues xyz = new ParameterValues();
ParameterDiscreteValue pdv = new ParameterDiscreteValue();
pdv.Value = atm.WorkingModel.Header.TicketNumber;
xyz.Add(pdv);
rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz);
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
I would like the output to go to a new tab or window. 我希望输出转到新的选项卡或窗口。 Currently it just replaces the current window. 目前它只是替换当前窗口。
I got it working through VS with the following code (The database login error was of result of a typo): 我使用以下代码通过VS工作(数据库登录错误是错误的结果):
ReportClass rptH = new ReportClass();
rptH.FileName = Server.MapPath(Url.Content("~/app_data/whkinvc.rpt"));
rptH.Load();
rptH.SetDatabaseLogon(...);
ParameterValues xyz = new ParameterValues();
ParameterDiscreteValue pdv = new ParameterDiscreteValue();
pdv.Value = atm.WorkingModel.Header.TicketNumber;
xyz.Add(pdv);
rptH.DataDefinition.ParameterFields["@rpt_args"].ApplyCurrentValues(xyz);
Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
return File(stream, "application/pdf");
In the past I've had to set the connection string for every table (or stored proc or whatever) in the report, and also in every table in every sub report. 在过去,我必须为报表中的每个表(或存储的proc或其他)设置连接字符串,并且还必须在每个子报表的每个表中设置连接字符串。
For example: 例如:
(Suppose rd is an instance of ReportDocument, and cn is a preprepared instance of ConnectionInfo) (假设rd是ReportDocument的一个实例,cn是ConnectionInfo的预备实例)
void SetupConnection(ReportDocument rd, ConnectionInfo cn)
{
foreach (Table t in rd.database.Tables)
{
TableLoginInfo li = t.LogOnInfo;
li.ConnectionInfo = cn;
t.ApplyLogOnInfo(li);
t.Location = t.Location;
}
if (!rd.IsSubReport)
{
foreach (ReportDocument sr in rd.SubReports)
SetupConnection(sr,cn);
}
}
I realise some of this makes little sense, (eg t.Location = t.Location
). 我意识到其中一些没有意义,(例如t.Location = t.Location
)。 Its a while since I used it, but I believe that these were necessary for it to work properly. 我使用它已经有一段时间了,但我相信这些对于它正常工作是必要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.