やりたいこと
JavaでMS OfficeのWordファイルを作成
表(table)を行列指定で挿入
特定のセル内で改行を入れて文字を挿入
使用するもの
言語 : Java 1.8
IDE : eclipse Version: Neon.3 Release (4.6.3)
※STS Pluginが使えれば何でも大丈夫
FW : Spring boot 1.5.7
※POIが使えれば何でも大丈夫(多分)
プロジェクト管理ツール : Maven 4.0.0
MS Officeが入ってない人
MS Office Word Viewer を導入しましょう。
諸々の準備
IDEのDL方法やSTS Pluginの導入はググれば出てくるから割愛
検索ワード
Eclipse → eclipse pleiades
STS Plugin → eclipse spring bot plugin
新規プロジェクトの作成もググれば出てくるから割愛。
Mavenでライブラリ追加
pom.xmlの <dependencies> ~ </dependencies>
内に以下を追加
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作成
index.html
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<a href="demo/download">ダウンロードおおおおおおおおおおおおおおおおお</a>
</body>
</html>
Controller作成
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ファイル作成ここから
XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph;
XWPFRun run;
XWPFTable table;
// 10 x 1 の表を挿入 第1引数:行数 第2引数:1行あたりのセル数
table = document.createTable(10, 1);
paragraph = table.getRow(0).getCell(0).getParagraphs().get(0);
run = paragraph.createRun();
run.setText("1行目");
// メイン:Paragraph(段落)追加
// run.addCarriageReturn();では改行してくれない。
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("2行目");
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("3行目");
paragraph = table.getRow(0).getCell(0).addParagraph();
run = paragraph.createRun();
run.setText("4行目");
// Wordファイル作成ここまで
// HttpServletResponseにダウンロードさせるファイルの形式とファイル名を設定
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename=\"" + "fileName" + ".docx\"");
// HttpServletResponseに作成したWordファイルをセット
document.write(response.getOutputStream());
if (document != null) {
document.close();
}
}
}
確認
Spring Bootアプリケーションを起動
loalhost:8080/demo にアクセス
[ダウンロードおおおおおおおおおおおおおおおおお] 押下
適当な場所にファイル保存して開く