preamble
In the previous postGcExcel Template Tutorial Series IV - Grouping and ExtensionIn this article, I have shared how to use GcExcel to realize the grouping and expansion of templates, this article will mainly introduce how to make global settings in the template.
Global settings in GcExcel templates are settings defined for the entire template. Global settings can greatly simplify the workload when the same properties need to be applied to multiple fields. These settings can be applied to all template layouts and can even cover multiple sheets in a workbook.
GcExcel
The global settings provided by the GcExcel template are described below:
- KeepLineSize (maintains row height and column width)
- InsertMode (insert whole row or column)
- DebugMode
- PaginationMode
- EmbeddedFontForFormFields
In this section, I will mainly introduce the first three settings, for the other modes you can refer to the official GcExcel documentation for more details on the global settings.
Configuration method
Global settings, is configured through Excel's Formula Name Manager. It should be noted that the global settings need to be configured before the template is filled to take effect, you can use the following code for global configuration:
Workbook wb = new Workbook();
("");
// Configure the global settings
().add("", "true");
//Configure the data source, the ds object requires additional configuration
("ds", ds); //Configure the data source.
//Template filling
().
//Save the report
(""); //Save the report.
In addition to using the code, it can also be set up via Excel orSpreadJS, configure the global configuration directly in the template so that there is no need to show the settings in the code.
Excel:
SpreadJS:
global setting
1. KeepLineSize
Normally, GcExcel will not change the row height and column width of the cell when expanding the cell, but will directly follow the row height and column width of the existing cell, as shown in the following figure:
After exporting, you can see that only the height of the first line is larger, the rest of the new line height has not been modified, and the original line height in the template remains consistent.
But often, for the sake of a neat, aesthetically pleasing layout, the result we expect should look something like the following:
At this point we can use the KeepLineSize property to modify the row height and column width to be consistent when adding new rows and columns.
Use the following code to achieve this:
Workbook workbook = new Workbook();
("");
().add("","True");
("ds",CreateData2());
();
In addition to using code, you can also use a name manager, which is pre-configured in the template:
2. Insert an entire row or column (InsertMode)
GcExcel in the expansion of the cell, in order to try to avoid the impact of the layout of the template, the default is to insert the cell to expand the way, as shown in the following chart:
However, sometimes in real-world scenarios, this operation can break the layout, as in the image above where the green row of cells is cut. In this case, we can modify the insertion behavior by using the InsertMode property to make GcExcel insert the rows as they are when populating the template:
Workbook workbook = new Workbook();
("");
().add("","EntireRowColumn");
("ds",CreateData());
();
In addition to using the code, you can also use the Name Manager and add settings:
3. DebugMode
DebugMode, a mode designed to facilitate the comparison of templates and results. When DebugMode is turned on, the template worksheet will be retained when the template is populated and its name will be changed to {sheetname}_template, e.g., if the original worksheet name is sales, the template worksheet will be sales_template.
As shown in the figure below:
(architecture) formwork
forms for reporting statistics
As with the previous setup, you can enable debug mode via the code or name manager.
Workbook workbook = new Workbook();
("");
().add("","True");
("ds",CreateData());
();
summarize
GcExcel's Global Settings are designed to efficiently address configuration issues that are shared across different worksheets or templates. Each of the different configuration methods has its own advantages. Configuration by code avoids the need to focus on whether the template itself has global properties set, while configuration using the Name Manager better decouples the code from the template and improves the maintainability and flexibility of the code. This flexible configuration method provides a variety of options for handling global settings, enabling better application and management of global settings under different requirements.
Extended Links:
Easily build low-code workflows: a great tool for simplifying tedious tasks
Data-Driven Innovation: Customizing Pivot Tables Programmatically in Java
Java batch operation Excel file practice