MVC+Angular+Net Core 平台如何实现纯代码拼接报表
客户需求:客户只需要简单的二维表展示,二维表中的所有列都是根据输入SQL 语句后,执行的查询结果,来展示所选择的数据列。
因为又牵扯到MVC+Angular+.Net Core平台展示,所以在这个需求内,需要使用报表的API来动态生成报表,其实报表结构不复杂,主要修改Startup.cs 类中。
1. 在 Startup.cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseSpaStaticFiles();
// Configure reporting to get reports from folder.
// You can also use the reports embedded into the assembly (UseEmbeddedTemplates method)
// or your own reports store (UseCustomStore method).
app.UseReporting(settings =>
{
//settings.UseEmbeddedTemplates(EmbeddedReportsPrefix, Assembly.GetAssembly(GetType()));
settings.UseCustomStore(GetReport);
settings.UseCompression = true;
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
2. 在GetReport 方法里面调用报表API 生成报表
private object GetReport(string arg)
{
int columnNumber=3;// 用于获取前台传递SQL 语句的列数,根据数量去生成表格的列数量
int tableTextbox = columnNumber * 2;
GrapeCity.ActiveReports.PageReportModel.Report report = new GrapeCity.ActiveReports.PageReportModel.Report();
// 从 SQL 语句中获取到列名,及为表格控件绑定数据字段,前三个为列标题,后三个为数据字段引用
String[] columnNameFromSQL = new String { "MoviedID", "Title", "YearReleased", "=Fields!MoviedID.Value", "=Fields!Title.Value", "=Fields!YearReleased.Value" };
report.Body.Height = "5cm";
report.Width = "20cm";
// 根据列名及字段名创建表格控件,用于展示报表
Table tb = CreateTable(columnNumber,columnNameFromSQL);
report.Body.ReportItems.Add(tb);
report = AddDataSetDataSource(report);
return report;
}
3. 添加数据源和数据集
private Table CreateTable(int NumOfColumn,String[] NameOfField)
{
Table table = new Table();
table.Name = "Table1";
TextBox[] tableTextBoxes = new TextBox;
TableCell[] tableCells = new TableCell;
TableRow[] tableRows = new TableRow;
TableColumn[] tableColumns = new TableColumn;
String[] textBoxValues = new String[] { };
textBoxValues = NameOfField;
//定义表格的列宽
String[] columnsWidth = new String[] { "3cm", "3cm", "3cm" };
String[] rowsHeight = new String[] { "1.5cm", "1.5cm" };
//Setting properties for the Textboxes to be placed in the TableCells
for (int i = 0; i < tableTextBoxes.Length; i++)
{
tableTextBoxes.SetValue(new TextBox(), i);
tableTextBoxes.Name = "textBox" + (i + 1);
tableTextBoxes.Value = ExpressionInfo.FromString(textBoxValues);
tableTextBoxes.Style.PaddingBottom = tableTextBoxes.Style.PaddingLeft = tableTextBoxes.Style.PaddingRight = tableTextBoxes.Style.PaddingTop = ExpressionInfo.FromString("2pt");
tableTextBoxes.Style.TextAlign = ExpressionInfo.FromString("Left");
tableCells.SetValue(new TableCell(), i);
tableCells.ReportItems.Add(tableTextBoxes);//Adding the TextBoxes to the TableCells
if (i < rowsHeight.Length)
{
tableRows.SetValue(new TableRow(), i);
tableRows.Height = "1.25cm";
table.Height += "1.25cm";
}
if (i < columnsWidth.Length)
{
tableColumns.SetValue(new TableColumn(), i);
tableColumns.Width = columnsWidth;
table.Width += columnsWidth;
table.TableColumns.Add(tableColumns);
tableCells.ReportItems.Style.BackgroundColor = ExpressionInfo.FromString("LightBlue");
tableRows.TableCells.Add(tableCells);
}
else
{
tableCells.ReportItems.Style.BackgroundColor = ExpressionInfo.FromString("=Choose((RowNumber(\"Table1\") +1) mod 2, \"PaleGreen\",)");
tableRows.TableCells.Add(tableCells);
}
}
table.Header.TableRows.Add(tableRows);
table.Details.TableRows.Add(tableRows);
table.Top = "1cm";
table.Left = "0.635cm";
return table;
}
4. 添加 AddDataSetDataSource 方法为报表绑定数据源
public static Report AddDataSetDataSource(Report report)
{
// 以 DataSet数据集为示例,通过输入SQL 语句获取到查询结果后后传递给报表即可
// create DataSource for the report
DataSource dataSource = new DataSource();
dataSource.Name = "Reels Database";
dataSource.ConnectionProperties.DataProvider = "OLEDB";
dataSource.ConnectionProperties.ConnectString = ExpressionInfo.FromString(@"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=Data\Reels.mdb");
//Create DataSet with specified query and load database fields to the DataSet
DataSet dataSet = new DataSet();
Query query = new Query();
dataSet.Name = "Sample DataSet";
query.DataSourceName = "Reels Database";
query.CommandType = QueryCommandType.Text;
query.CommandText = ExpressionInfo.FromString(@"SELECT Movie.MovieID, Movie.Title, Movie.YearReleased FROM Movie ORDER BY Movie.YearReleased");
dataSet.Query = query;
String[] fieldsList = new String[] { "MoviedID", "Title", "YearReleased" };
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.DataSources.Add(dataSource);
report.DataSets.Add(dataSet);
return report;
}
页:
[1]