目录
根据word模板生成word文档
/        

根据word模板生成word文档

需求

需要给小学生出一些题库,由于小侄女这两种类型题型不太擅长,所以写个程序自动生成题库

目前程序不太灵活

项目运行

文档地址:http://deepoove.com/poi-tl/

maven依赖

        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>

模板文件

专项练习2.docx.zip

测试类

package cn.lacknb.test.wordtemplate;

import com.deepoove.poi.XWPFTemplate;
import org.junit.Test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author: gitsilence
 * @description: 根据word模板 生成word文档
 * @date: 2021/4/27 2:29 下午
 **/
public class WordGenerateTest {

    /**
     * 生成 start 和 end之间的数,包括start和end
     * [start, end]
     * @param start
     * @param end
     * @return
     */
    public int randInt (int start, int end) {
        return (int) (Math.random() * end) + start;
    }

    /**
     * 生成第一个数
     * @return
     */
    public int generateANum () {
        int a = randInt(1, 9);
        int b = randInt(0, 9);
        if (a == 0 && b == 0) {
            b = randInt(1, 9);
        }
        return a * 10 + b;
    }

    /**
     * 生成第二个数,【不涉及进位】
     * @param num
     * @param mark
     * @return
     */
    public int generateBNum (int num, char mark) {
        int ten = num / 10, unit = num % 10;
        int a = randInt(0, 9), b = randInt(0, 9);
        if (mark == '-' && (a > ten || b > unit)) {
            a = randInt(0, ten);
            b = randInt(0, unit);
        }
        if (mark == '+' && (b + unit > 9 || a + ten > 9)) {
            a = randInt(0, 9 - ten);
            b = randInt(0, 9 - unit);
        }
        return a * 10 + b;
    }

    @Test
    public void test01 () throws IOException {
        String path = "/Users/gitsilence/javaProject/java-study/src/main/resources/";
        HashMap<String, Integer> map = new HashMap<>();
        int oadd1 = generateANum();
        int oadd2 = generateBNum(oadd1, '+');
        int padd1 = generateANum();
        int padd2 = generateBNum(padd1, '+');
        int qadd1 = generateANum();
        int qadd2 = generateBNum(qadd1, '+');
        int osub1 = generateANum();
        int osub2 = generateBNum(osub1, '-');
        int psub1 = generateANum();
        int psub2 = generateBNum(psub1, '-');
        int qsub1 = generateANum();
        int qsub2 = generateBNum(qsub1, '-');
        int atower1 = generateANum();
        int atower2 = generateBNum(atower1, '+');
        int btower1 = generateANum();
        int btower2 = generateBNum(btower1, '+');
        int ctower1 = generateANum();
        int ctower2 = generateBNum(ctower1, '-');
        int dtower1 = generateANum();
        int dtower2 = generateBNum(dtower1, '-');
        map.put("oadd1", oadd1);
        map.put("oadd2", oadd2);
        map.put("padd1", padd1);
        map.put("padd2", padd2);
        map.put("qadd1", qadd1);
        map.put("qadd2", qadd2);
        map.put("osub1", osub1);
        map.put("osub2", osub2);
        map.put("psub1", psub1);
        map.put("psub2", psub2);
        map.put("qsub1", qsub1);
        map.put("qsub2", qsub2);
        map.put("atower1", atower1);
        map.put("atower2", atower2);
        map.put("btower1", btower1);
        map.put("btower2", btower2);
        map.put("ctower1", ctower1);
        map.put("ctower2", ctower2);
        map.put("dtower1", dtower1);
        map.put("dtower2", dtower2);

        System.out.println(map);
        XWPFTemplate template = XWPFTemplate.compile(path + "专项练习.docx").render(map);
        FileOutputStream fileOutputStream = new FileOutputStream(path + "专项练习-" + System.currentTimeMillis() + ".docx");
        template.writeAndClose(fileOutputStream);

    }

}

运行效果

image.png

用的wps,可能布局会乱,网上转pdf方法不太靠谱。

待优化...


标题:根据word模板生成word文档
作者:gitsilence
地址:http://blog.lacknb.cn/articles/2021/04/27/1619512010998.html