“This is the 22nd day of my participation in the First Challenge 2022. For details: First Challenge 2022”

πŸ‘¨πŸŽ“ Author: Bug Bacteria

✏️ blog: CSDN, Nuggets, etc

πŸ’Œ public account: Magic House of the Circle of the Apes

🚫 special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.

πŸ™ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.

Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!

In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles πŸ‘. Your likes (collect ⭐️+ pay attention to πŸ‘¨ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon πŸƒπŸ»β™€οΈ, creation stopped πŸ’•, refueling 🏻

One, foreword

In the last few sessions, we mainly talked about how to integrate EasyPOI to achieve import and export function of Excel, import and export of Word, right? I don’t know how you have mastered it. If you still have questions about any of the following articles, please ask more questions. Ha ha ha, although I am not a big guy, I will try my best to teach each other and learn to fill in the gaps together.

  • How does Springboot integrate with EasyPOI

  • Integrate EasyPOI to realize Excel import function

  • Integrated EasyPOI to achieve Excel export function

  • Integrated easyPOI to realize the function of exporting multi-page data from single Word template

  • Integrated EasyPOI to realize word template traversal display all data functions

Now that word exports are diversified, what if you carry images on your data?

For example, to export the basic information of all users, and the user table, there is a self-portrait of each user, such as an inch of what, like this situation, then how to export word should be implemented? Is it the same as before?

Next I will start, the students may have to listen to oh ~ I will take you step by step to achieve it, as for how to achieve, then look down.

2. Introduce POM dependencies

For example, if you want to implement word portable image export, it will be impossible to introduce the previous easyPOI version. For example, although easyPOi 3.2.0 provides extended image export, it is true that the bug bacteria have tried and failed to export. It may be that the author has failed to export the image in these versions. There is no code to parse the image at all. Please look at:

Then I would have encapsulated a method for setting images directly, but any reference to the entity is underlined and should be deprecated.

So in the query of information found that easypoi4.3 version and above but support word image export, is very perfect, this is the Gospel for students who have the need to export pictures in word ah. That is, we will take easypoi4.3 as an example to show you how to achieve word image export, ok?

<! - easypoi dependence, Afterturn </groupId> <artifactId>easypoi-spring-boot-starter</artifactId> The < version > 4.3.0 < / version > < / dependency >Copy the code

Remember that! Note that multiple image export loops are supported only in version 4.3.0+.

Three, to achieve word image export

1. Define a Word template file

We respectively to single Word template export multi-page data and Word template export multi-data two modes of demonstration, so that we encounter this demand in the future, do not need to refer to the relevant information, directly start is open masturbating (code).

A model:

Model 2:

The above are two different Word templates. Next, we will show you how to export the portrait picture according to these two different templates.

2. Add word export method for Controller

Let’s first define a Word document export method, the purpose is to provide a port, easy to test through the browser access.

@getMapping ("export-word-all") @apiOperation (value = "Export word to a template ", Notes = "Export word document to a template ") public void exportUsersToWordAll(HttpServletResponse Response) throws Exception { userService.exportUsersToWord(response); } @getMapping ("export-word") @apiOperation (value = "export-word", Notes = "Word document export ") public void exportUsersToWord(HttpServletResponse Response) throws Exception { userService.exportUsersToWord(response); }Copy the code

You don’t need any return values or arguments, you just need to carry the HttpServletResponse that you requested when you called the interface.

3. Define the import interface

/** * Word document export to a template */ void exportUsersToWordAll(HttpServletResponse Response) throws Exception; /** * word document export */ void exportUsersToWord(HttpServletResponse Response) throws Exception;Copy the code

4. Implement the export method (core)

Easypoi exportWord07() : easyPOi exportWord07() : easyPOI exportWord07() : EasyPOI

The specific code Settings are as follows:

Put ("image", this.imgformatting ("static/image/ img.jpg ", 100, 100));Copy the code

I will not directly post all the code ah, after all, the rest of the code in the last few teaching content are all there, and I am only here as a demonstration, so the picture address is fixed, if the actual environment, must be directly passed in the user’s picture address.

Attached is the method of setting up picture packaging for your reference only:

/** * * * @param imgPath Image path */ private ImageEntity imgFormatting(String imgPath, int width, Int height) {// Set ImageEntity image = new ImageEntity(imgPath, width, height); // add a simple image image.setType(imageEntity.url); return image; }Copy the code

5. Browser test interface

We open the browser, enter the address of the interface we just exposed in Controller in the address bar:

Like my: http://localhost:8080//user/export-word-all and http://localhost:8080//user/export-word you visit at your interface address.

Let me show you the screenshot of the two modes to show what the image looks like:

Mode 1: Export data in a single template with multiple pages

The above is only a screenshot of the template content, anyway, the picture is the same, your national goddess.

Mode 2: Export word template to display multiple contents

The difference between these two export modes is still a little big, right? But the main point of this episode is not the two export modes, but the pictures in both modes are displayed successfully, I thought they would flip over. Hey, the content teach you, the rest is up to you to explore.

. .

Well, that’s all for this episode, and if you want to learn more, you can check out my top tips on how to accumulate a little weird knowledge every day, and over time, you can become a person you respect. Well, I’ll see you next time

Four, the past popular recommendation

  • Springboot series (16) : Integrated easyPOI implementation of Excel import and export (preparation)

  • Springboot series (16) : Integrated EasyPOI to achieve Excel import function

  • Springboot series (16) : Integrated easyPOI to achieve Excel export function

  • Springboot series (16) : integrated easyPOI to achieve single Word template export multi-page function

  • Springboot series (16) : Integrated easyPOI to realize word template multi-data export function

  • Springboot series (15) : AOP implements custom annotations for business logging! Have you ever played?

  • Springboot series (14) : Redis Zero-based teaching, you deserve it!

  • Springboot Series (thirteen) : How to project integrated Swagger online interface documentation, will you?

  • Springboot series (12) : How to code to send email reminders, have you written?

  • . .

If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

☘️ Be who you want to be, there is no time limit, you can start whenever you want,

πŸ€ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

​

πŸ’Œ If this article is helpful to you, please leave a like! (# ^. ^ #);

πŸ’ if you like the article shared by bug fungus, please give bug fungus a point of concern! The danjun ‘α΄—, you guys will have a cameo appearance with you.

πŸ’— if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];

πŸ’ž In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);

πŸ’• copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you