A Fast and Flexible Import and Export Tool For Excel: Why Autoexcel?
A Fast and Flexible Import and Export Tool For Excel: Why Autoexcel?
With AutoExcel, you can quickly import and export excel without hard coding, and embrace changes.
AutoExcel is very simple and only requires a small amount of code to complete complex import and export. When using it, programmers have no sense of import and
export, that is, there is no need to directly manipulate POI.
Why AutoExcel?
Excel import and export is very common in software development, as long as you are a programmer, you have met. I believe that many people will choose to use
Apache POI to complete this work like me. While feeling the power of POI, my team also encountered the following problems:
1. Directly use POI to operate Excel will generate a lot of hard code, you will hardly write row index and column index in the code.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
2. A large number of non-reusable format control codes, such as background color, alignment, cell style, etc.
3. The implementation consultant clearly provided a ready-made template, but had to develop the code to implement it again, resulting in low development
efficiency.
4. Development resources have to be used when the template is adjusted.
5. Simple export also requires specific code.
AutoExcel solves the above problems. It is very simple and only requires a small amount of code to complete complex import and export. When using it,
programmers have no sense of import and export, that is, there is no need to directly manipulate POI. At the same time, the implementation consultant provides Excel
is the import and export template, unless new data sources or fields are added, the template update does not need to use development resources.
AutoExcel does not over-encapsulate the POI, but makes full use of Excel's own feature-the name manager, through some tricks, the cell and the data source are
mapped, thereby decoupling the programmer and the POI, and avoid hard code, so that import and export work becomes enjoyable and no longer boring.
Function preview
Before export
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Before export
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Before export
After export
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
After export
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
After export
To achieve the above export, you only need to write the following small amount of code (you need additional code to prepare the data source, for example, from the
database)
AutoExcel.save(this.getClass().getResource("/template/Common.xlsx").getPath(),
this.getClass().getResource("/").getPath() + "ExportWithTemplate.xlsx",
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
paras,
excelSetting);
Name manager
The name manager in Excel, a feature that is ignored by most people, has become a bridge between data sources and cells in AutoExcel. You can open the name
manager by clicking the menu Formula->Name Manager. Each name corresponds to a specific location in Excel. It can be a region or a cell. Of course, here, the
names we defined all point to cells. So it can be understood that the name manager is used to name cells. It is precisely because the cell has a name that we can
automatically assign a value to the cell without the need for personalized code.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
After defining the name for the cell, when you click on the cell again, you will find the name you just defined is displayed in the upper left corner.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
In addition to adding new names in the name manager, there is another way that is more intuitive and faster. Click on the cell you want to name, then directly enter
the name in the upper left corner, and finally press the Entry button. It is recommended to create names in this way.
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Name rule
Because the cell name determines what kind of data and how to fill in, it must be named according to the following rules:
1. DataSourceName.FieldName[.AggregateType], used to fill common fields or aggregate of common fields, e.g. product.SaleArea.sum
2. DataSourceName.Formula.xxxx, used to fill the formula, e.g. product.Formula.1
3. DataSourceName.RowNo, used to fill the row number, e.g. product.RowNo
All names are not case sensitive, the following will be introduced according to specific scenarios.
Export
Basic object
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
As shown in the figure, the name of each cell is indicated in the remarks, written in accordance with the rules of DataSourceName.FieldName
java code :
Hide Copy Code
Single table
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
If you want to export a list of data, you only need to name it according to the writing rules of the base object. Of course, the export of list data is often more
complicated than the basic object. For example, you may need a column of row numbers, but you don’t want to do special processing in the code. At this time, you
can use DataSourceName.RowNo to hand over the work to AutoExcel to process. Note that RowNo is a built-in field. If this field is included in the data source, it
will be overwritten.
There is also a very common situation, you have a cell with a formula in the table, such as: =E6+F6, you want the cell in the next row to be assigned the value
=E7+F7. At this time, you should use DataSourceName.Formula.xxxx, you can use any formula you like, and AutoExcel will automatically fill it for you
eventually. You can write whatever you want at the part of xxxx, as long as the name is unique. Formula is also a built-in field.
java code :
Hide Copy Code
Multi-table
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Export multiple tables in one Sheet. If you have such a requirement, please set the export parameter of the table that is not at the bottom in the background code to:
setInserted(true). As shown in the figure above, the export parameter para corresponding to products should be set as follows:
para.setInserted(true). You know, AutoExcel does not care about whether there is enough space for data export, it will only output continuously. So when
your template space is not enough, you need to tell AutoExcel, and then AutoExcel will make enough space to hold your data before exporting.
A new naming rule is introduced here: DataSourceName.FieldName.AggregateType, used to total the specified fields. Currently, two aggregate types are
supported: Sum and Avg.
java code :
Hide Copy Code
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Fill data to the right
If you need to fill the data to the right instead of down, you just need to use setDataDirection(DataDirection.Right).
java code :
Hide Copy Code
Export directly
Export directly, that is, the export process does not require the use of templates, and is suitable for integration into the general export function of the back-end system.
The code is very simple.
:
effect
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Of course, you don't like this kind of title and title order, so you need to use FieldSetting to make your title readable and display in the order you like.
final effect:
Custom action
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
AutoExcel is committed to dealing with general scenarios of import and export. If there is a personalized demand, you should take back the control of Workbook and
perform personalized processing according to your needs. The save method provides two Consumers, of which actionAhead will be called before the export
operation starts, and actionBehind will be called after the export is completed. You can use these two Consumers to add the functions you want.
Import
Compared with export, import has the following characteristics:
java code :
Hide Copy Code
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Run the sample code
Please go to the unit test to get the complete sample code.
GitHub
License
This article, along with any associated source code and files, is licensed under The MIT License
Share
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
About the Author
IvanFeng
Team Leader
China
No Biography provided
Search Comments
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Permalink Layout: fixed | fluid Article Copyright 2020 by IvanFeng
Advertise Everything else Copyright © CodeProject, 1999-2020
Privacy
Cookies Web04 2.8.20201008.1
Terms of Use
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD