Créer un fichier MS Office Word en Java Insérer un tableau par spécification de matrice Insérer des caractères avec des sauts de ligne dans une cellule spécifique
Langue: Java 1.8
IDE : eclipse Version: Neon.3 Release (4.6.3)
FW : Spring boot 1.5.7
Outil de gestion de projet: Maven 4.0.0
** Personnes sans MS Office ** Installons MS Office Word Viewer.
La méthode DL de l'IDE et l'introduction de STS Plugin sortiront si vous google, donc je vais l'omettre Rechercher un mot Eclipse → eclipse pleiades STS Plugin → eclipse spring bot plugin
J'omettrai la création d'un nouveau projet car il sortira si je google.
Ajoutez ce qui suit dans <dependencies> ~ </ dependencies> de pom.xml
pom.xml
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml-schemas</artifactId>
	<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi</artifactId>
	<version>3.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans -->
<dependency>
	<groupId>org.apache.xmlbeans</groupId>
	<artifactId>xmlbeans</artifactId>
	<version>2.6.0</version>
</dependency>
index.html
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
      <head></head>
      <body>
	      <a href="demo/download">Télécharger Ooooooooooooooooooooooooooooooooooooooooooooooooo</a>
      </body>
</html>
DemoController.java
package com.example.demo.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/demo")
public class DemoController {
	@GetMapping
	public ModelAndView demo(ModelAndView mv) {
		mv.setViewName("index");
		return mv;
	}
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        //Création de fichier Word à partir d'ici
        XWPFDocument document = new XWPFDocument();
        XWPFParagraph paragraph;
        XWPFRun run;
        XWPFTable table;
        //Insérer un tableau 10 x 1 1er argument: nombre de lignes 2ème argument: nombre de cellules par ligne
        table = document.createTable(10, 1);
        paragraph = table.getRow(0).getCell(0).getParagraphs().get(0);
        run = paragraph.createRun();
        run.setText("La première ligne");
        //Principal: Paragraphe(Paragraphe)ajouter à
        // run.addCarriageReturn();Alors ça ne casse pas.
        paragraph = table.getRow(0).getCell(0).addParagraph();
        run = paragraph.createRun();
        run.setText("2e ligne");
        paragraph = table.getRow(0).getCell(0).addParagraph();
        run = paragraph.createRun();
        run.setText("3e ligne");
        paragraph = table.getRow(0).getCell(0).addParagraph();
        run = paragraph.createRun();
        run.setText("4ème ligne");
        //Création de fichier Word jusqu'à ici
        //Définissez le format et le nom de fichier du fichier à télécharger par HttpServletResponse
        response.setContentType("application/msword");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + "fileName" + ".docx\"");
        //Définissez le fichier Word créé dans HttpServletResponse
        document.write(response.getOutputStream());
        if (document != null) {
            document.close();
        }
    }
}
Lancer l'application Spring Boot
Accéder à loalhost: 8080 / démo
[Télécharger Ooooooooooooooooooo] Appuyez sur
Enregistrez le fichier dans un emplacement approprié et ouvrez-le
Recommended Posts