How to save report in HTML into ZIP

2016-04-13

When I was developing another PHP application, it was necessary in the financial statements. Previously I'd had great experience with FastReport.Net report generator. So I decided to use it for this purpose too. Unfortunately, the option with the Web-based reporting was irrelevant since we did not use ASP .Net in this project. The idea was born to use a REST web service that will build a report and give it to a php application. I will explain development of the service later. And now let's focus on the process of report building and preparation to be sent via REST. For simplicity, I'll demonstrate it on an example of a console application.

We need FastReport libraries:

1
2
3
using FastReport;
using FastReport.Export.Html;
using FastReport.Utils;

 I will pass the report title via the parameter:

1
2
3
4
5
6
7
 static void Main(string[] args)
 {
 if (args.Length > 0)
 DoExport(args[0]);
 else
 Console.WriteLine("Set the report file (*.frx) as parameter");
 }

Create the method of exporting report to HTML and archiving in ZIP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private static void DoExport(string reportFile)
 {
 if (File.Exists(reportFile))
 {
 Config.WebMode = true; // set WebReport mode for disable all progress and enable thread-safe code
 using (Report report = new Report()) // create new report object
 {
 report.Load(reportFile); // load report from file
 report.Prepare(); // prepare report
 using (HTMLExport html = new HTMLExport()) // create new export object
 {
 html.SaveStreams = true; // enable saving in streams
 report.Export(html, (Stream)null); // set target stream in null - we have multiple streams inside export object
 if (html.GeneratedFiles.Count > 0)
 {
 ZipArchive zip = new ZipArchive(); // create ZIP object
 for(int i = 0; i < html.GeneratedFiles.Count; i++)
 zip.AddStream(html.GeneratedFiles[i], html.GeneratedStreams[i]); // add streams with file names in zip
 zip.SaveToFile(Path.GetFileNameWithoutExtension(reportFile) + ".zip"); // write zip in file
 }
 } 
 }
 }
 else
 Console.WriteLine("File " + reportFile + " not found!");
 }

Here it should be noted about Config.WebMode property - it enables the "quiet" mode of reporting without issuing any dialogs and progress bars.

In this example I pack one report, but there's no reason why not to put a few pieces in archive.

Now start the application in the console with the parameter. The parameter specifies the path to the report. And get a zip-file in the folder with the application. The archive is a packed report in html format. Thus, using a web service we can pass into our web application an archive with one or more reports.

 

November 26, 2024

Installing FastReport on .NET 8.0 and Creating a Simple Report

The purpose of this article is to explain step by step how to install FastReport on .NET 8.0 and how to create a simple report. Taking the reporting process from the beginning, it will show how to connect, design and view reports.
November 20, 2024

Localization and Language Switching in FastReport VCL

FastReport VCL supports 40 languages for interface localization and allows you to change the language on the fly through menus or code, without recompilation.
November 01, 2024

New Features of the FastReport VCL Editor

We are considering new features of the report editor: extension lines, highlighting of intersecting objects, updated report and data trees.
Fast Reports
  • 800-985-8986 (English, US)
  • +4930568373928 (German)
  • +55 19 98147-8148 (Portuguese)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2024 Fast Reports Inc.