Dynamic tabular data report in ASP.Net

I am working on an MVC5 web application where I need to create a report from any database table. The user will select the table that he wants to print, then select which columns will be included in the report, and the system should simply display a table (table) with data.

Displaying data in an HTML table, of course, is not a problem, and I also managed to create Excel files with data that the user can download.

However, I also need to provide a printed report form, such as a PDF or some other format that does not require special software (for example, MS Office) to open and print. A.

At first it seemed to me that I should go for the ReportViewer control, but the problem is that my report is dynamic, because the user selects the columns he needs. I saw others trying to create a report definition file, but I was hoping for something less ugly.

Is there a way to take an HTML table that beautifully calculates cell sizes and creates a PDF file from it? I do not mind, of course, breaking the report horizontally across multiple pages if there are too many columns.

+8
c # asp.net-mvc-5 pdf-generation xlsx report
source share
2 answers

You can try Spartacus .

This is a relatively new .NET library written entirely in C #. It can connect to many different databases and generate reports in Excel and PDF.

I uploaded 3 files in Google Drive:

To use Spartacus, you need to refer to the System.Data and System.Xml packages, as well as to Spartacus.dll.

In the following example, I created report.pdf from template.xml in simple steps:

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 do not need to use Spartacus.Database objects. If you can get System.Data.DataTable in other ways, you can pass it to the Report object.

However, there is a catch. As you can see in the XML template, for each column you need to know:

  • Column Name (Obviously)
  • Title
  • Alignment (LEFT, RIGHT or CENTER)
  • Fill (percentage, 100 - total page width minus margin width)
  • Type (INTEGER, REAL, BOOLEAN, CHAR, DATE or STRING)

Filling and type are important, and you may need to store information about all of your columns. If this is too difficult to achieve, and you can only get the name and type of the column, you can calculate aproximation based only on the type, for example:

  • the product is of type STRING, by default fill = 30
  • the description is of type STRING, by default fill = 30
  • unit is of type STRING, default fill = 30
  • the quantity is of type REAL, by default fill = 15
  • total_cost is of type REAL, by default fill = 15
  • unit_cost is of type REAL, by default fill = 15

The sum of all filled by default is 30 + 30 + 30 + 15 + 15 + 15 = 135, more than 100. Thus, you can normalize to 100, thus:

  • Column fill = default column fill * (sum of default values ​​/ 100)

After that, you will need to generate dynamic template.xml files with information about the dynamic field.

DISCLAIMER : I am the creator and maintainer of Spartak.

+2
source share

SSRS has great support for automatic pagination processing, but is key according to its rules.

Understanding Pagination in Reporting Services (Report Builder 3.0 and SSRS)

If you have developed a report that is one page wide but displays on several pages, make sure that the width of the report body, including the margins, does not exceed the width of the size of the physical page. To prevent blank pages from being added to the report, you can reduce the size of the container by dragging the corner of the container to the left.

0
source share

All Articles