我使用的是,DevExpress15.2.9版本
DevExpress.ReportServer.15.2.10.exe 报表服务器版本
DevExpress.ReportServer.Designer.15.2.10.exe 报表设计器版本
先上代码我的 webform的代码。
.aspx部分
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="TestWebApplication1.WebForm1" %>
<%@ Register assembly="DevExpress.XtraReports.v15.2.Web, Version=15.2.9.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.XtraReports.Web" tagprefix="dx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<dx:ASPxDocumentViewer ID="ASPxDocumentViewer1" runat="server"
DocumentViewerInternal="" Height="351px" ReportTypeName="XtraReport1"
Width="696px">
</dx:ASPxDocumentViewer>
</form>
</body>
</html>
aspx.cs部分
protected void Page_Load(object sender, EventArgs e)
{
var db = new testEntities1();//EF代码
UserClassReportModel rm = new UserClassReportModel();
rm.user_class = db.user_class
.Where(q=>q.id ==1)
.First();
rm.users = db.users
.Where(p => p.classid == rm.user_class.id)
.ToList() ;
//上面用ef取完数据库,下面加载模板
//第一种方式,用原生类库
//XtraReport1 s = new XtraReport1(); 这个是用开发工具添加cs文件的方式创建的类。可以打包的。
//第二种方式,从http服务器上取模板。 第一种方式创建的类,在设计模式下可以导出repx文件。
var url = "http://localhost:47720/repx/XtraReport1.repx";
MemoryStream ms = DownLoadFielToMemoryStream(url);
XtraReport s = XtraReport.FromStream(ms, true);
//第三种,直接读取报表模板文件,跟第二种一样,只不过是从本地加载。不如第二种灵活。
//XtraReport s = XtraReport.FromFile("D:\\repx\\XtraReport1.repx",true);
//第四种,从报表服务器直接导出pdf文件, 需要安装DevExpress.ReportServer.15.2.10.exe
// Create a connection provider.
ConnectionProvider connection = new WindowsUserConnectionProvider("http://127.0.0.1:83");//报表服务器地址
// Initialize a report server client.
IReportServerClient client = connection.ConnectAsync().Result;
var reportDtos = client
.GetReportsAsync(null)
.ContinueWith(task =>
{
if (task.IsFaulted)
{
throw new Exception("222222");
}
else
{
FillReportListBox(task.Result);
}
}, TaskScheduler.FromCurrentSynchronizationContext());
// Place your code here to interact with the a report service using the created client.
const string targetFileName = @"c:\temp\CustomerOrderHistory.pdf";
const int reportId = 3;//这个值,在FillReportListBox中能看到.就是reportDto.Id
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(int), Value = "1" } };//这个地方很容易出错
Task<byte[]> exportTask = Task.Factory.ExportReportAsync(client, new ReportIdentity(reportId), new PdfExportOptions(), parameters, null);
File.WriteAllBytes(targetFileName, exportTask.Result);
s.DataSource = new List<UserClassReportModel>() { rm };
this.ASPxDocumentViewer1.Report = s;
}
void FillReportListBox(IEnumerable<ReportCatalogItemDto> reports)
{
foreach (var reportDto in reports)
{
Response.Write(reportDto.Name);
Response.Write(reportDto.Id);
}
}
/// <summary>
/// 读取url文件到内存流中
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static MemoryStream DownLoadFielToMemoryStream(string url)
{
var wreq = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = wreq.GetResponse() as HttpWebResponse;
MemoryStream ms = null;
using (var stream = response.GetResponseStream())
{
Byte[] buffer = new Byte[response.ContentLength];
int offset = 0, actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
ms = new MemoryStream(buffer);
}
response.Close();
return ms;
}
几个错误点。
DevExpress.XtraReports.Service.ParameterValidationException: Cannot set value of unrelated type for a parameter ‘cid’.
at DevExpress.XtraReports.Service.Native.Services.ParametersSetter.ValidateParameter(Type parameterType, Boolean multiValue, ReportParameter clientParameter)
at DevExpress.XtraReports.Service.Native.Services.ParametersSetter.AssignToReport(XtraReport report, IEnumerable`1 clientParameters)
at DevExpress.ReportServer.Web.Worker.Services.JobExecutor.BuildReport(InstanceIdentity instanceIdentity, ReportParameter[] parameters, ReportConfiguration reportBuildConfiguration, DocumentId documentId, ContextInfo contextInfo)
这个错误是因为
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(int), Value = "1" } };
正确的应该是
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(int), Value = 1 } };//1不带引号
或者
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(string), Value = 1 } };//Type 为string。类型必须和Value匹配
错误2.
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at DevExpress.XtraReports.Service.Native.Services.ParametersSetter.AssignToReport(XtraReport report, IEnumerable`1 clientParameters)
at DevExpress.ReportServer.Web.Worker.Services.JobExecutor.BuildReport(InstanceIdentity instanceIdentity, ReportParameter[] parameters, ReportConfiguration reportBuildConfiguration, DocumentId documentId, ContextInfo contextInfo)
是因为
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Type = typeof(int), Value = "1" } };
正确的应该是
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(int), Value = "1" } };//这个Path必须有值,好像必须是跟Name相同
错误3
DevExpress.XtraReports.Service.ParameterValidationException: Cannot set value of unrelated type for a parameter 'cid'.
at DevExpress.XtraReports.Service.Native.Services.ParametersSetter.ValidateParameter(Type parameterType, Boolean multiValue, ReportParameter clientParameter)
at DevExpress.XtraReports.Service.Native.Services.ParametersSetter.AssignToReport(XtraReport report, IEnumerable`1 clientParameters)
at DevExpress.ReportServer.Web.Worker.Services.JobExecutor.BuildReport(InstanceIdentity instanceIdentity, ReportParameter[] parameters, ReportConfiguration reportBuildConfiguration, DocumentId documentId, ContextInfo contextInfo)
这个错误是因为没有给Name赋值
ReportParameter[] parameters = { new ReportParameter() { Path = "cid", Type = typeof(int), Value = 1 } };
正确代码应该是
ReportParameter[] parameters = { new ReportParameter() { Name = "cid", Path = "cid", Type = typeof(int), Value = 1 } };
参考文章
https://documentation.devexpress.com/#ReportServer/CustomDocument17640
如何用代码连接Report Server
https://www.devexpress.com/Support/Center/Example/Details/T155725
Report Server - how to export a report from code in a console application
https://www.devexpress.com/Support/Center/Example/Details/E5062
Report Server - How to get the list of available reports and display a report preview in a Windows Forms application
https://documentation.devexpress.com/#ReportServer/CustomDocument17462
DevExpress.ReportServer.Designer.15.2.10.exe 报表设计器中如何使用参数。cid
研究了2天。老了。