Create MS Office Word file in Java Insert table by matrix specification Insert characters with line breaks in a specific cell
Language: Java 1.8
IDE : eclipse Version: Neon.3 Release (4.6.3)
FW : Spring boot 1.5.7
Project management tool: Maven 4.0.0
** People without MS Office ** Let's install MS Office Word Viewer.
The DL method of the IDE and the introduction of the STS Plugin will come out if you google, so I will omit it. Search word Eclipse → eclipse pleiades STS Plugin → eclipse spring bot plugin
I will omit the creation of a new project because it will come out if I google.
Add the following in <dependencies> ~ </ dependencies>
of 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">Download Ooooooooooooooooooooooooooooooooooooooooooooo</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 {
//Word file creation from here
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
XWPFTable table;
//Insert a 10 x 1 table 1st argument: number of rows 2nd argument: number of cells per row
table = document.createTable(10, 1);
paragraph = table.getRow(0).getCell(0).getParagraphs().get(0);
run = paragraph.createRun();
run.setText("The first line");
//Main: Paragraph(Paragraph)add to
// run.addCarriageReturn();Then it won't break.
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("2nd line");
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("3rd line");
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("4th line");
//Word file creation up to here
//Set the format and file name of the file to be downloaded by HttpServletResponse
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=\"" + "fileName" + ".docx\"");
//Set the created Word file in HttpServletResponse
document.write(response.getOutputStream());
if (document != null) {
document.close();
}
}
}
Launch Spring Boot application
Access loalhost: 8080 / demo
[Download Ooooooooooooooooooo] Press
Save the file to a suitable location and open it
Recommended Posts