[Java] [POI] Create a table in Word and start a new line in one cell

Thing you want to do

Create MS Office Word file in Java Insert table by matrix specification Insert characters with line breaks in a specific cell

What to use

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.

Various preparations

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.

Library added with Maven

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>

html creation

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>

Controller creation

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();
        }
    }
}

Verification

Launch Spring Boot application

Access loalhost: 8080 / demo

[Download Ooooooooooooooooooo] Press

Save the file to a suitable location and open it

Recommended Posts

[Java] [POI] Create a table in Word and start a new line in one cell
Java creates a table in a Word document
Java joins and splitting table cells in Word documents
[Java] How to start a new line with StringBuilder
Create a new app in Rails
Sort List in descending order in Java and generate a new List non-destructively
How to create a new Gradle + Java + Jar project in Intellij 2016.03
Install Rails in the development environment and create a new application
[Java] Create and apply a slide master
Create a TODO app in Java 7 Create Header
Have a tool to create and open a new canvas in Mac preview
Create barcodes and QR codes in Java PDF
Two ways to start a thread in Java + @
Create a CSR with extended information in Java
Try to create a bulletin board in Java
[Java] Create a collection with only one element
Let's create a super-simple web framework in Java
Java adds and removes watermarks in word documents
[Personal application work memo] How to display a bar graph and a line graph in one graph
Create a Java Servlet and JSP WAR file to deploy to Apache Tomcat 9 in Gradle
Create a portfolio app using Java and Spring Boot
How to create a Java environment in just 3 seconds
Difference between new and create in Rais action controller
Write a class in Kotlin and call it in Java
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
How to convert A to a and a to A using AND and OR in Java
Create a JAVA WEB application and try OMC APM
[Java] A technique for writing constructors, getters, and setters in one shot with IntelliJ IDEA.
[Java] Create a filter
Create JSON in Java
What's new in Java 8
What's new in Java 9,10,11
Activate Excel file A1 cell of each sheet in Java
Create a SlackBot with AWS lambda & API Gateway in Java
Create a method to return the tax rate in Java
Create a tomcat project using Eclipse Pleiades All in One
How to develop and register a Sota app in Java
Create a Java and JavaScript team development environment (gradle environment construction)
Remove consecutive line breaks and tabs before and after in Java