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

Dynamic tabular data report in ASP.Net

我正在开发 MVC5 Web 应用程序,我需要从任何数据库生成报告 table。用户将 select 他想要打印的 table,然后选择要包含在报告中的列,系统应该只显示一个包含数据的网格 (table)。

在 HTML table 中显示数据当然不是问题,我还设法用数据生成 Excel 文件,用户可以下载。

不过,我还需要提供一份主要table格式的报告,例如PDF或其他不需要特殊软件(例如MS Office)即可打开和打印的格式。

起初我认为我应该使用 ReportViewer 控件,但问题是我的报表是动态的,因为用户 selects 是他需要的列。我已经看到其他人尝试生成报告定义文件,但我希望能有一些不那么难看的东西。

有没有办法采用 HTML table,它可以很好地计算单元格的大小,并从中创建 PDF?如果列太多,我当然不介意将报告水平拆分为多个页面。

SSRS 对自动处理分页有很好的支持,但遵循它的规则是关键。

Understanding Pagination in Reporting Services (Report Builder 3.0 and 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.

你可以试试Spartacus.

这是一个相对较新的 .NET 库,完全用 C# 编写。它可以连接到许多不同的数据库并生成 Excel 和 PDF 格式的报告。

我在 Google 驱动器中上传了 3 个文件:

为了使用 Spartacus,您需要参考 System.Data 和 System.Xml 包,以及 Spartacus.dll.

在下面的示例中,我通过简单的步骤从 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");

请注意,您不需要使用 Spartacus.Database object。如果你可以通过其他方式得到一个System.Data.DataTable,那么你可以将它传递给Report object.

但是,有一个问题。正如您在 XML 模板中看到的,对于每一列,您需要知道:

  • 列名(显然)
  • 标题
  • 对齐(左、右或居中)
  • 填充(百分比,100 是页面的总宽度减去页边距的宽度)
  • 类型(整数、实数、布尔值、字符、日期或字符串)

填充和类型是必不可少的,您可能需要保留有关所有列的信息。 如果这太难实现,而且你只能得到列名和类型,你可以计算一个仅基于类型的近似值,如下所示:

  • product类型为STRING,默认fill = 30
  • 描述类型为STRING,默认填充=30
  • unit类型为STRING,默认fill = 30
  • 数量为REAL类型,默认填充=15
  • total_cost是REAL类型,默认fill = 15
  • unit_cost是REAL类型,默认fill = 15

所有默认填充的总和是30+30+30+15+15+15 = 135,大于100。所以你可以归一化为100,这样:

  • 列填充 = 列默认填充 *(默认填充总和 / 100)

之后,您将需要生成包含动态字段信息的动态 template.xml 文件。

免责声明:我是斯巴达克斯的创造者和维护者。