Java realizes the writing of word documents

For the generation of word documents, poi generation is mainly used.
import dependencies

<dependency>
   <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.17</version>
</dependency>

1. The first way
Write an alternative way to write documentation in the main class

public static void main(String[] args) throws Exception {
    
    
    Map<String, String> map = new HashMap<>();
    map.put("${reportContent}", "这是一个难忘的季节");
    map.put("${date}", "2021-9-10");
    map.put("${author}", "cyz");
    map.put("${address}", "中国的一个小城市");
    XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\word\\template.docx"));
    Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
    while (itPara.hasNext()) {
    
    
        XWPFParagraph paragraph = (XWPFParagraph) itPara.next();
        List<XWPFRun> runs = paragraph.getRuns();
        for (int i = 0; i < runs.size(); i++) {
    
    
            String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition()).trim();
            for (Map.Entry<String, String> entry : map.entrySet()) {
    
    
                if (oneparaString.equals(entry.getKey())) {
    
    
                    oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
                }
            }
            runs.get(i).setText(oneparaString, 0);
        }
    }
    FileOutputStream outStream = null;
    outStream = new FileOutputStream("D:\\word\\write.docx");
    document.write(outStream);
    outStream.close();
}

First, we write the replaced content in the template.docx document.
Secondly, you can obtain the corresponding value from the database, use the replacement content as the key, and use the corresponding value obtained in the database as the key value. Finally, replace the content and write it out to the write.docx document.
Second, the second way

package com.zhsh.thymeleaftest.controller;

import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;

/**
 * @description:
 * @author:cyz
 * @time:2021/9/11 0011
 */
@Controller
@RequestMapping(value="/expReport")
public class ExpReportController {
    
    

    @RequestMapping(value="word")
    @ResponseBody
    public static void ExpReport(HttpServletRequest request, HttpServletResponse response)throws Exception {
    
    

        response.setContentType("application/msword");
        response.setCharacterEncoding("utf-8");
        String fileName=URLEncoder.encode("生成报告文档","utf-8").replaceAll("\\+","%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''"+fileName+".doc");

        //创建word文档对象
        XWPFDocument document=new XWPFDocument();
        //创建标题
        XWPFParagraph title=document.createParagraph();
        //设置段落居中
        title.setAlignment(ParagraphAlignment.CENTER);

        XWPFRun titleRun=title.createRun();
        titleRun.setColor("000000");
        titleRun.setFontSize(25);
        titleRun.setFontFamily("仿宋");
        titleRun.setBold(true);
        titleRun.setText("生成报告文档信息");
        titleRun.addBreak();//换行

        //添加第一段落
        XWPFParagraph firstParagraph=document.createParagraph();
        //设置左对齐
        firstParagraph.setAlignment(ParagraphAlignment.LEFT);
        //---------------------以上设置整段的树形,以下设置内容
        XWPFRun firstRun=firstParagraph.createRun();
        firstRun.setColor("000000");
        firstRun.setFontSize(13);
        firstRun.addTab();//缩进
        firstRun.setFontFamily("楷体");
        firstRun.setBold(true);
        firstRun.setText("这是一份有关于经济方面的报告,如何增长,如何促进本市的经济发展,促进消费,拉动经济。");
        firstRun.addBreak();//换行
        firstRun.addTab();//缩进
        firstRun.setText("虽然目前经济来说,由于疫情的影响,消费拉动经济确实有点困难,但线上的消费同样也可以促进经济发展,所以应该大力发展。");
        firstRun.addBreak();//换行
        //添加第二段落
        XWPFParagraph secondParagraph=document.createParagraph();
        secondParagraph.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun secondRun=firstParagraph.createRun();
        secondRun.setText("这是第二段落");

        document.write(response.getOutputStream());
        document.close();

    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324353831&siteId=291194637