简体   繁体   English

ASP.Net中的动态表格数据报告

[英]Dynamic tabular data report in ASP.Net

I am working on an MVC5 web application where I need to generate a report from any database table. 我正在使用MVC5 Web应用程序,需要从任何数据库表生成报告。 The user will select the table he wants to print, then choose which columns to include in the report and the system should simply display a grid (table) with the data. 用户将选择他要打印的表,然后选择要包括在报告中的列,并且系统应仅显示包含数据的网格(表)。

Displaying the data in a HTML table is of course not a problem and I have also managed to generate Excel files with the data, that the user can download. 在HTML表中显示数据当然不是问题,而且我还设法用用户可以下载的数据生成Excel文件。

However I also need to provide a printable form of the report such as PDF or some other format that doesn't need special software (such as MS Office) to open and print. 但是,我还需要提供报告的可打印形式,例如PDF或不需要特殊软件(例如MS Office)即可打开和打印的其他格式。

At first I thought I should go for the ReportViewer control, but the problem is that my report is dynamic since the user selects the columns he needs. 起初我以为我应该使用ReportViewer控件,但是问题在于我的报表是动态的,因为用户选择了他需要的列。 I have seen attempts from others to generate the report definition file, but I was hoping for something less ugly. 我已经看到了其他人尝试生成报告定义文件的尝试,但是我希望它不那么难看。

Is there a way to take a HTML table, which calculates the sizing of the cells nicely, and create a PDF from it? 有没有办法获取一个HTML表格,该表格可以很好地计算单元格的大小,并从中创建PDF? I don't mind of course splitting the report horizontally in multiple pages if the columns are too many. 如果列太多,我当然不介意将报告水平分成多页。

You could try Spartacus . 您可以尝试斯巴达克斯

It's a relatively new .NET library, totally written in C#. 这是一个相对较新的.NET库,完全使用C#编写。 It can connect to many different databases and generate reports in Excel and PDF. 它可以连接到许多不同的数据库并以Excel和PDF生成报告。

I uploaded 3 files in Google Drive: 我在Google云端硬盘中上传了3个文件:

In order to use Spartacus, you need to reference System.Data and System.Xml packages, as well as Spartacus.dll. 为了使用Spartacus,您需要引用System.Data和System.Xml程序包以及Spartacus.dll。

In the following example, I created report.pdf from template.xml in simple steps: 在以下示例中,我通过简单的步骤从template.xml创建report.pdf:

Spartacus.Database.Generic v_database;
Spartacus.Reporting.Report v_report;
System.Data.DataTable v_table;

v_database = new Spartacus.Database.Postgresql("127.0.0.1", "database", "postgres", "password");

v_table = v_database.Query(
    "select 'Example of Report made with Spartacus' as title, " +
    "       product, " +
    "       description, " +
    "       unit, " +
    "       quantity, " +
    "       total_cost, " +
    "       unit_cost " +
    "from table", "REPORT");

v_report = new Spartacus.Reporting.Report(1, "template.xml", v_table);
v_report.Execute();
v_report.Save("report.pdf");

Note that you don't need to use Spartacus.Database objects. 请注意,您不需要使用Spartacus.Database对象。 If you can get a System.Data.DataTable by other means, then you can pass it to the Report object. 如果可以通过其他方式获得System.Data.DataTable,则可以将其传递给Report对象。

However, there is a catch. 但是,有一个陷阱。 As you can see in the XML template, for each column, you need to know: 正如您在XML模板中看到的那样,对于每一列,您需要了解:

  • Column name (obviously) 列名(显然)
  • Title 标题
  • Align (LEFT, RIGHT or CENTER) 对齐(向左,向右或居中)
  • Fill (percent, 100 being the total width of the page minus width of the margins) 填充(百分比,100为页面的总宽度减去页边距的宽度)
  • Type (INTEGER, REAL, BOOLEAN, CHAR, DATE or STRING) 类型(INTEGER,REAL,BOOLEAN,CHAR,DATE或STRING)

Fill and Type are essential, and you may need to keep information about all your columns. 填充和类型是必不可少的,您可能需要保留有关所有列的信息。 If this is too hard to achieve, and you can only get column name and type, you can compute a aproximation based only on type, like the following: 如果这很难实现,并且您只能获取列名和类型,则可以仅基于类型来计算近似值,如下所示:

  • product is of type STRING, default fill = 30 产品类型为STRING,默认填充= 30
  • description is of type STRING, default fill = 30 说明的类型为STRING,默认填充= 30
  • unit is of type STRING, default fill = 30 单位为STRING类型,默认填充= 30
  • quantity is of type REAL, default fill = 15 数量为REAL类型,默认填充= 15
  • total_cost is of type REAL, default fill = 15 total_cost类型为REAL,默认填充= 15
  • unit_cost is of type REAL, default fill = 15 unit_cost的类型为REAL,默认填充= 15

The sum of all default fill are 30+30+30+15+15+15 = 135, greater than 100. So you can normalize to 100, this way: 所有默认填充的总和为30 + 30 + 30 + 15 + 15 + 15 = 135,大于100。因此,您可以通过以下方式将其标准化为100:

  • Column fill = Column default fill * (sum of default fills / 100) 列填充=列默认填充*(默认填充总和/ 100)

After that, you will need to generate dynamic template.xml files with dynamic field information. 之后,您将需要生成带有动态字段信息的动态template.xml文件。

DISCLAIMER : I'm Spartacus' creator and maintainer. 免责声明 :我是Spartacus的创建者和维护者。

SSRS has great support for automatically handling pagination, but following its rules are key. SSRS支持自动处理分页,但是遵循其规则是关键。

Understanding Pagination in Reporting Services (Report Builder 3.0 and SSRS) 了解Reporting Services中的分页(Report Builder 3.0和SSRS)

If you have designed a report to be one page wide, but it renders across multiple pages, check that the width of the report body, including margins, is not larger than the physical page size width. 如果您将报表设计为一页宽,但是可以跨多页呈现,请检查报表正文的宽度(包括边距)是否不大于物理页面大小的宽度。 To prevent empty pages from being added to your report, you can reduce the container size by dragging the container corner to the left. 为了防止将空白页添加到报表中,可以通过将容器角向左拖动来减小容器的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM