环境:vs2019+ar13,mvc5
在项目中使用H5方式完全正常,当使用JSViewer方式后,js和css都能加载,但在展示页总是提示:Report not found: xxx.rdlx。调试了很久都没解决。
项目中的Startup1文件为:
- using System;
- using System.Data;
- using System.IO;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Routing;
- using GrapeCity.ActiveReports;
- using GrapeCity.ActiveReports.Aspnet.Viewer;
- using GrapeCity.ActiveReports.PageReportModel;
- using GrapeCity.Enterprise.Data.DataEngine.Expressions;
- using Microsoft.Owin;
- using Owin;
- [assembly: OwinStartup(typeof(Ntnet.Application.Web.Startup1))]
- namespace Ntnet.Application.Web
- {
- public class Startup1
- {
- public object GetReport(string P)
- {
- PageReport rep = BuildReportLayout();
- rep = AddDataSetDataSource(rep);
- rep.Report.Name = P;
- string path = System.Web.Hosting.HostingEnvironment.MapPath("~/");
- creatReportFile(rep, @"" + path + "Reports/" + P);
- rep.Load(new FileInfo(@"" + path + "Reports/" + P));
- return rep.Report;
- }
- /// <summary>
- /// UseCustomStore方式
- /// </summary>
- /// <param name="app"></param>
- public void Configuration1(IAppBuilder app)
- {
- app.UseReporting(settings =>
- {
- settings.UseCompression = true;
- settings.UseCustomStore(GetReport);
- settings.LocateDataSource = args =>
- {
- if (args.Report.Name.ToString() == "静态报表.rdlx")
- {
- if (args.DataSet.Query.DataSourceName == "DataSource1")
- {
- if (args.DataSet.Name == "DataSet1")
- {
- return GetData();
- }
- }
- return null;
- }
- else if (args.Report.Name.ToString() == "动态报表.rdlx")
- {
- if (args.DataSet.Query.DataSourceName == "DataSource1")
- {
- if (args.DataSet.Name == "DataSet1")
- {
- return GetData();
- }
- }
- return null;
- }
- else
- {
- return null;
- }
- };
- });
- }
- /// <summary>
- /// UseFileStore模式
- /// </summary>
- /// <param name="app"></param>
- public void Configuration(IAppBuilder app)
- {
- app.UseReporting(settings =>
- {
- settings.UseCompression = true;
- string path = System.Web.Hosting.HostingEnvironment.MapPath("~/");
- settings.UseFileStore(new DirectoryInfo(@"" + path + "Reports"));
- settings.LocateDataSource = args =>
- {
- if (args.Report.Name.ToString() == "静态报表.rdlx")
- {
- if (args.DataSet.Query.DataSourceName == "DataSource1")
- {
- if (args.DataSet.Name == "DataSet1")
- {
- return GetData();
- }
- }
- return null;
- }
- else if (args.Report.Name.ToString() == "动态报表.rdlx")
- {
- if (args.DataSet.Query.DataSourceName == "DataSource1")
- {
- if (args.DataSet.Name == "DataSet1")
- {
- return GetData();
- }
- }
- return null;
- }
- else
- {
- return null;
- }
- };
- });
- }
- #region 创建UI界面元素
- /// <summary>
- /// 创建UI界面元素
- /// </summary>
- /// <returns></returns>
- public static PageReport BuildReportLayout()
- {
- PageReport report = new PageReport();
- report.Report.Body.Height = "5cm";
- report.Report.Width = "20cm";
- //创建table
- Table table = new Table();
- table.Name = "Table1";
- table.Top = "1cm";
- table.Left = "0.635cm";
- table.Style.BackgroundColor = ExpressionInfo.FromString("Transparent");
- GrapeCity.ActiveReports.PageReportModel.Border border = new GrapeCity.ActiveReports.PageReportModel.Border();
- border.Color = ExpressionInfo.FromString("DarkGray");
- border.Style = ExpressionInfo.FromString("Solid");
- border.Width = ExpressionInfo.FromString("1pt");
- table.Style.Border = border;
- //创建行
- TableRow[] tableRows = new TableRow[2];
- tableRows.SetValue(new TableRow(), 0);
- tableRows[0].Height = "1.25cm";
- table.Height += "1.25cm";
- tableRows.SetValue(new TableRow(), 1);
- tableRows[1].Height = "1.25cm";
- table.Height += "1.25cm";
- //创建列
- TableColumn[] tableColumns = new TableColumn[3];
- tableColumns.SetValue(new TableColumn(), 0);
- tableColumns[0].Width = "3cm";
- table.Width += "3cm";
- table.TableColumns.Add(tableColumns[0]);
- tableColumns.SetValue(new TableColumn(), 1);
- tableColumns[1].Width = "3cm";
- table.Width += "3cm";
- table.TableColumns.Add(tableColumns[1]);
- tableColumns.SetValue(new TableColumn(), 2);
- tableColumns[2].Width = "3cm";
- table.Width += "3cm";
- table.TableColumns.Add(tableColumns[2]);
- //创建单元格
- TableCell[] tableCells = new TableCell[6];
- tableCells.SetValue(new TableCell(), 0);
- tableCells.SetValue(new TableCell(), 1);
- tableCells.SetValue(new TableCell(), 2);
- tableCells.SetValue(new TableCell(), 3);
- tableCells.SetValue(new TableCell(), 4);
- tableCells.SetValue(new TableCell(), 5);
- //创建文本框并将文本框添加到单元格中
- TextBox[] tableTextBoxes = new TextBox[6];
- tableTextBoxes.SetValue(new TextBox(), 0);
- tableTextBoxes[0].Name = "textBox0";
- tableTextBoxes[0].Value = ExpressionInfo.FromString("用户姓名");
- tableTextBoxes[0].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[0].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- tableTextBoxes[0].Style.FontWeight = ExpressionInfo.FromString("Bold");
- tableTextBoxes[0].Style.FontSize = ExpressionInfo.FromString("10pt");
- GrapeCity.ActiveReports.PageReportModel.Border border0 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border0.Color = ExpressionInfo.FromString("DarkGray");
- border0.Style = ExpressionInfo.FromString("Solid");
- border0.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[0].Style.Border = border0;
- tableCells[0].ReportItems.Add(tableTextBoxes[0]);
- tableTextBoxes.SetValue(new TextBox(), 1);
- tableTextBoxes[1].Name = "textBox1";
- tableTextBoxes[1].Value = ExpressionInfo.FromString("用户密码");
- tableTextBoxes[1].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[1].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- tableTextBoxes[1].Style.FontWeight = ExpressionInfo.FromString("Bold");
- tableTextBoxes[1].Style.FontSize = ExpressionInfo.FromString("10pt");
- GrapeCity.ActiveReports.PageReportModel.Border border1 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border1.Color = ExpressionInfo.FromString("DarkGray");
- border1.Style = ExpressionInfo.FromString("Solid");
- border1.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[1].Style.Border = border1;
- tableCells[1].ReportItems.Add(tableTextBoxes[1]);
- tableTextBoxes.SetValue(new TextBox(), 2);
- tableTextBoxes[2].Name = "textBox2";
- tableTextBoxes[2].Value = ExpressionInfo.FromString("用户年龄");
- tableTextBoxes[2].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[2].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- tableTextBoxes[2].Style.FontWeight = ExpressionInfo.FromString("Bold");
- tableTextBoxes[2].Style.FontSize = ExpressionInfo.FromString("10pt");
- GrapeCity.ActiveReports.PageReportModel.Border border2 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border2.Color = ExpressionInfo.FromString("DarkGray");
- border2.Style = ExpressionInfo.FromString("Solid");
- border2.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[2].Style.Border = border2;
- tableCells[2].ReportItems.Add(tableTextBoxes[2]);
- tableTextBoxes.SetValue(new TextBox(), 3);
- tableTextBoxes[3].Name = "textBox3";
- tableTextBoxes[3].Value = ExpressionInfo.FromString("=Fields!user_name.Value");
- tableTextBoxes[3].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[3].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- GrapeCity.ActiveReports.PageReportModel.Border border3 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border3.Color = ExpressionInfo.FromString("DarkGray");
- border3.Style = ExpressionInfo.FromString("Solid");
- border3.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[3].Style.Border = border3;
- tableCells[3].ReportItems.Add(tableTextBoxes[3]);
- tableTextBoxes.SetValue(new TextBox(), 4);
- tableTextBoxes[4].Name = "textBox4";
- tableTextBoxes[4].Value = ExpressionInfo.FromString("=Fields!user_password.Value");
- tableTextBoxes[4].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[4].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- GrapeCity.ActiveReports.PageReportModel.Border border4 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border4.Color = ExpressionInfo.FromString("DarkGray");
- border4.Style = ExpressionInfo.FromString("Solid");
- border4.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[4].Style.Border = border4;
- tableCells[4].ReportItems.Add(tableTextBoxes[4]);
- tableTextBoxes.SetValue(new TextBox(), 5);
- tableTextBoxes[5].Name = "textBox1";
- tableTextBoxes[5].Value = ExpressionInfo.FromString("=Fields!user_age.Value");
- tableTextBoxes[5].Style.TextAlign = ExpressionInfo.FromString("Right");
- tableTextBoxes[5].Style.VerticalAlign = ExpressionInfo.FromString("Middle");
- GrapeCity.ActiveReports.PageReportModel.Border border5 = new GrapeCity.ActiveReports.PageReportModel.Border();
- border5.Color = ExpressionInfo.FromString("DarkGray");
- border5.Style = ExpressionInfo.FromString("Solid");
- border5.Width = ExpressionInfo.FromString("1pt");
- tableTextBoxes[5].Style.Border = border2;
- tableCells[5].ReportItems.Add(tableTextBoxes[5]);
- //将单元格添加到行中
- tableRows[0].TableCells.Add(tableCells[0]);
- tableRows[0].TableCells.Add(tableCells[1]);
- tableRows[0].TableCells.Add(tableCells[2]);
- tableRows[1].TableCells.Add(tableCells[3]);
- tableRows[1].TableCells.Add(tableCells[4]);
- tableRows[1].TableCells.Add(tableCells[5]);
- //将行添加到table中
- table.Header.TableRows.Add(tableRows[0]);
- table.Details.TableRows.Add(tableRows[1]);
- //将table添加到模板Body中
- report.Report.Body.ReportItems.Add(table);
- return report;
- }
- #endregion
- #region 创建数据源,数据集
- /// <summary>
- /// 创建数据源,数据集
- /// </summary>
- /// <param name="report"></param>
- /// <returns></returns>
- public static PageReport AddDataSetDataSource(PageReport report)
- {
- // create DataSource for the report
- DataSource dataSource = new DataSource();
- dataSource.Name = "DataSource1";
- dataSource.ConnectionProperties.DataProvider = "DATASET";
- dataSource.ConnectionProperties.ConnectString = ExpressionInfo.FromString("");
- //Create DataSet with specified query and load database fields to the DataSet
- GrapeCity.ActiveReports.PageReportModel.DataSet dataSet = new GrapeCity.ActiveReports.PageReportModel.DataSet();
- Query query = new Query();
- dataSet.Name = "DataSet1";
- query.DataSourceName = "DataSource1";
- dataSet.Query = query;
- String[] fieldsList = new String[] { "user_name", "user_password", "user_age" };
- foreach (string fieldName in fieldsList)
- {
- Field field = new Field(fieldName, fieldName, null);
- dataSet.Fields.Add(field);
- }
- //create report definition with specified DataSet and DataSource
- report.Report.DataSources.Add(dataSource);
- report.Report.DataSets.Add(dataSet);
- return report;
- }
- #endregion
- #region 生成报表物理文件
- /// <summary>
- /// 生成报表物理文件
- /// </summary>
- /// <param name="rep"></param>
- /// <param name="FilePath"></param>
- public static void creatReportFile(PageReport report, string fileName)
- {
- string rpt = report.ToRdlString();
- byte[] data = Encoding.UTF8.GetBytes(rpt);
- Stream stream = new MemoryStream();
- stream.Read(data, 0, data.Length);
- // 设置当前流的位置为流的开始
- stream.Seek(0, SeekOrigin.Begin);
- // 把 byte[] 写入文件
- FileStream fs = new FileStream(fileName, FileMode.Create);
- BinaryWriter bw = new BinaryWriter(fs);
- bw.Write(data);
- bw.Close();
- bw.Dispose();
- fs.Close();
- fs.Dispose();
- stream.Close();
- stream.Dispose();
- }
- #endregion
- #region 创建动态数据
- /// <summary>
- /// 创建动态数据
- /// </summary>
- /// <returns></returns>
- public DataTable GetData()
- {
- DataTable dt = new DataTable("User");
- dt.Columns.Add("user_name", typeof(string));
- dt.Columns.Add("user_password", typeof(string));
- dt.Columns.Add("user_age", typeof(int));
- System.Data.DataRow dr = dt.NewRow();
- dr[0] = "张三";
- dr[1] = "123";
- dr[2] = 20;
- dt.Rows.Add(dr);
- dr = dt.NewRow();
- dr[0] = "李四";
- dr[1] = "123";
- dr[2] = 23;
- dt.Rows.Add(dr);
- return dt;
- }
- #endregion
- }
- }
复制代码
附截图:
|