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.

 

.NET .NET FastReport FastReport HTML HTML
April 08, 2025

Converter from Microsoft Word (.docx) format to FastReport .NET (.frx) file

A converter from Microsoft Word (.docx) format to a file FastReport .NET (.frx): description and instructions for using the tool.
April 08, 2025

How to Set Up a Connection to Apache Ignite in FastReport .NET

In this article, we will explore how to configure a connection to Apache Ignite in FastReport .NET. You will learn the necessary steps to connect the plugin via code and the report designer.
March 25, 2025

How to Merge Multiple Reports into One in FastReport .NET

FastReport .NET is a powerful tool for creating and managing reports. In this article, we will look at how to combine multiple reports into one in FastReport .NET.
Fast Reports
  • 800-985-8986 (English, US)
  • +31 97 01025-8466 (English, EU)
  • +49 30 56837-3928 (German, DE)
  • +55 19 98147-8148 (Portuguese, BR)
  • info@fast-report.com
  • 66 Canal Center Plaza, Ste 505, Alexandria, VA 22314

© 1998-2025 Fast Reports Inc.