📜 ⬆️ ⬇️

Easy report creation in C / C ++

If you develop any software for operators (administrators) of hospitals, stores, rental services of gyroscooters, cell phone repair services, then you probably faced the task of creating reports to print them on a printer, or at least in PDF. There are many third-party packages for Embarcadero RAD Studio that allow you to do this. Such as FastReport, QuickReport, Crystal Reports, etc. But in my opinion all these packages require a deep understanding of their mechanism and interface. It takes a lot of time to study them, and most importantly, the code to generate even the simplest report will consist of a huge number of lines. When I first encountered the task of generating reports under C / C ++, I started with FastReport and realized that I absolutely do not like this tool.

At that very moment, a wonderful thought came to mind: how cool it would be to take a simple EXCEL file, add static information to it and format it to fit your needs. The program will only open this file, fill it with dynamic data and save or send to print! This was the starting point for my study of the OLE mechanism for working with MS Office files from the programs developed in Embarcadero RAD Studio.

In the course of my activity, I managed to penetrate deep enough into the topic and now I can say with confidence that all the tools provided by MS Office and OLE cover all my reporting needs. Well, if so, then probably for other developers, it would be a self-contained, simple and convenient tool. Therefore, it was decided to write a DLL and package it with a list of all commonly used MS Excel functions that you have to face during the creation of Excel documents. This is very convenient, because in order to create a report, you do not need to study a bunch of manuals or OLE. All that is required is to load the DLL and the functions included in it. Well, many people can work with DLL.

Here is the site of the project itself .
')
The project site describes in some detail the mechanism for working with DLLs, there is an example and a description of all functions.

The DLL comes without a header file and without a static LIB library. Thus, it is necessary to connect DLLs dynamically using the LoadLibrary function. ZIP archive with the library includes the following files:


The header file for the example of using the library " LPDLL.h " contains the following declarations:


The file "LPDLL.h" already contains everything you need to work with the DLL and its functions. But you can also edit it or take only the most necessary for your project.

Imagine that you simply connected the header file “LPDLL.h” to your project.

Then in the project itself you just need to:

1) Declare a variable descriptor of your report:

Variant report; 

2) Dynamically load a DLL and all its functions:

 if(!LoadLightReportDLL("C:\\LightReport\\light_report.dll"))return; 

3) Next, in the try-catch structure, open a previously prepared report template (MS Excel file):

 report=OpenReport("C:\\LightReport\\Report.xlsx",0); 

4) Add some data to the report:

 WriteCell(report, "Sheet1", 9, 1, "Hello world!"); 

...
5) Save file or print:

 Save(report); SaveAs(report, "C:\\LightReport\\Report copy.xlsx"); ExportToPDF(report, "C:\\LightReport\\Report.pdf", false); PrintOut(report); 

6) Close the file:

 CloseReport(report); 

7) Unload DLL library:

 FreeLightReportDLL(); 

And that's it! Absolutely nothing extra! There is only one nuance, but also that, it is shown only at debugging. In a running application, everything will work fine. I mean a situation in which there is any error when working with a report. The fact is that the OLE mechanism for working with documents involves only throwing exceptions when errors occur. That is why when working with DLL functions it is necessary to use the try-catch construct. In the catch block, close the report without saving:

 catch(...){CloseReport(report);} 
.
When debugging, when an error occurs, you can stop the program. At the same time, the MS Excel process will remain running, and it will be possible to close it only through the task manager. Therefore, when debugging an application several times, several instances of the MS Excel process can be started, depending on how often you suspend the program when an error occurs, without waiting for the code to run in catch. This must be monitored.

In the release, if an error occurs, the code specified in the catch block will surely work, the report will close and the MS Excel process will be completed. No MS Excel processes hanging in the system will be observed. But still trying to write an error-free code, so I hope you will not have such a situation in a running application.

Finally, add:

  1. The DLL uses platform-specific data types, such as unsigned short , unsigned long, and char . This is understandable why.
  2. The example is written in the Embarcadero Builder C ++ 10 environment. Accordingly, all the code corresponds to this environment and you may need to make some changes to the example code in order for it to work in your environment.
  3. The bottleneck of this DLL is the use of the Variant report file descriptor. This is a rather specific format, and I suspect that it may be difficult to use the library outside Embarcadero RAD Studio. Honestly, did not check.

Therefore, for the future, it is planned to pack all the functionality into a class and hide this Variant format inside the class, so that only the generally accepted C / C ++ formats are provided to the user. I have never tried to package classes in a DLL, I read that with this there will definitely be difficulties. Nevertheless, we will understand! In the meantime, thank you for your attention, I will be very happy if this article and the DLL help someone.

Link to the project .

Source: https://habr.com/ru/post/456102/


All Articles