Java background uses Apache POI to generate EXCEL documents, and provides foreground download examples

Previously, in the project, the template of filling data into word documents in Java in the background was used to provide foreground download. In order to view the implementation scheme at that time and facilitate others' learning, I wrote this blog, and the number of visits has been the first in my blog. So after I learned to use java to generate EXCEL documents using Apache POI in the background and provide foreground download, I thought of writing a sister article.

When generating EXCEL documents, I use a different method from that when generating word, Apache poi. It is a free, open source, cross platform Java API written in Java. It provides Java programs with the ability to read and write files in Microsoft Office format. To realize this function, follow the following steps. For convenience, I directly take the examples encountered in the project as examples. Yes, I am also completing my project when I write this blog.

Step 1: create a template in XLS format

If the header contains my party a information, you can see that I have made an empty template file. Now there are many things to fill in in the background

Step 2: foreground trigger event

Make a button and use JavaScript window when the user clicks location. Href redirect the page to the URL where you process the download

For example, this is the front desk of my project. Do you see the surface quality button? Take a look at the function called when it is clicked

Some friends may want to use ajax to send requests. Anyway, I didn't get it out. It's very troublesome. The relevant solutions found online are also painful, so they don't pass any complex and sensitive parameters. Just write it like this.

Step3: background processing

First of all, of course, you should introduce Apache POI into your project. My project is Maven project. It's easy to add dependencies

Then, in order to facilitate the export of Excel, an excelutils tool class is built in the project, and the source code is given later. In this way, it will be easier to export excel. In addition to some established methods in excelutils, there are also specific methods for you to operate the template. Of course, if you use less, you can not use my tool class, but import relevant classes when you need them, and then write the logic of the operation template when you handle them. However, I use export excel many times in this project, so it is necessary to abstract a tool class, which conforms to the design pattern.

My project is based on spring MVC. Let's see what I did after receiving the request in the background

Controller:

Finally, calling the related export method in ExcelUtils, this method is customized, which defines how to operate templates.

Custom methods:

ExcelUtils:

Step4: start the project and test it. Look! Perfectly derived... There are pictures as evidence

HMM, the article ends here. Apache POI also provides many APIs to be shown in this example, such as being able to specify styles and so on.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>