Java Code Examples for org.apache.poi.xwpf.usermodel.XWPFDocument#getParagraphArray()

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFDocument#getParagraphArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ELModeTest.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Test
public void testPoitlELMode() throws Exception {
    model.getDetail().setDesc(null);
    // poi_tl_mode 当变量不存在时,会友好的认为变量是null,不会抛出异常
    XWPFTemplate template = XWPFTemplate.compile(resource).render(model);
    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Sayi");
    paragraph = document.getParagraphArray(1);
    assertEquals(paragraph.getText(), "卅一");
    paragraph = document.getParagraphArray(3);
    assertEquals(paragraph.getText(), "");
    paragraph = document.getParagraphArray(4);
    assertEquals(paragraph.getText(), "");

    assertEquals(document.getAllPictures().size(), 1);
    assertEquals(document.getTables().size(), 1);
    document.close();
}
 
Example 2
Source File: ELModeTest.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Test
public void testSpringELMode() throws Exception {
    // Spring EL 无法容忍变量不存在,直接抛出异常,表达式计算引擎为Spring Expression Language
    Configure config = Configure.newBuilder().setElMode(ELMode.SPEL_MODE).build();
    XWPFTemplate template = XWPFTemplate.compile(resource, config).render(model);

    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Sayi");
    paragraph = document.getParagraphArray(1);
    assertEquals(paragraph.getText(), "卅一");
    paragraph = document.getParagraphArray(3);
    assertEquals(paragraph.getText(), "2018-10-01");
    paragraph = document.getParagraphArray(4);
    assertEquals(paragraph.getText(), "http://www.deepoove.com");

    assertEquals(document.getAllPictures().size(), 1);
    assertEquals(document.getTables().size(), 1);
    document.close();

}
 
Example 3
Source File: IfTemplateRenderTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testIfFalse() throws Exception {
    Map<String, Object> datas = new HashMap<String, Object>() {
        {
            put("title", "poi-tl");
            put("isShowTitle", true);
            put("showUser", false);
            put("showDate", false);
        }
    };

    XWPFTemplate template = XWPFTemplate.compile("src/test/resources/template/iterable_if1.docx");
    template.render(datas);

    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Hi, poi-tl");

    XWPFTable table = document.getTableArray(0);
    XWPFTableCell cell = table.getRow(1).getCell(0);
    assertEquals(cell.getText(), "Hi, poi-tl");

    XWPFHeader header = document.getHeaderArray(0);
    paragraph = header.getParagraphArray(0);
    assertEquals(paragraph.getText(), "Hi, poi-tl");
}
 
Example 4
Source File: Issue331.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
public void testinsertNewRunRun() throws FileNotFoundException, IOException {
    XWPFDocument doc = new XWPFDocument(new FileInputStream("src/test/resources/issue/331_hyper.docx"));
    XWPFParagraph createParagraph = doc.getParagraphArray(0);
    XWPFRun insertNewRun = createParagraph.insertNewRun(0);
    insertNewRun.setText("Hi");

    // FileOutputStream out = new FileOutputStream("out_tem.docx");
    // doc.write(out);
    // doc.close();
    // out.close();

}
 
Example 5
Source File: SpELTest.java    From poi-tl with Apache License 2.0 5 votes vote down vote up
@Test
  public void testSpELTemplate() throws IOException {
      Configure config = Configure.newBuilder().setElMode(ELMode.SPEL_MODE).setSpELFunction(spELFunction).build();
      XWPFTemplate template = XWPFTemplate.compile("src/test/resources/template/config_spel.docx", config).render(data);

      XWPFDocument document = XWPFTestSupport.readNewDocument(template);
      XWPFParagraph paragraph = document.getParagraphArray(0);
      assertEquals(paragraph.getText(), "poi-tl");
      paragraph = document.getParagraphArray(1);
      assertEquals(paragraph.getText(), "lowCase:poi-tlUpcase:POI-TL");
      paragraph = document.getParagraphArray(2);
      assertEquals(paragraph.getText(), "这个字段为空");
      paragraph = document.getParagraphArray(3);
      assertEquals(paragraph.getText(), "男");
      paragraph = document.getParagraphArray(4);
      assertEquals(paragraph.getText(), "2019-05-20 22:14:10");
      paragraph = document.getParagraphArray(5);
      assertEquals(paragraph.getText(), "2019-05-20 10:14");
      paragraph = document.getParagraphArray(6);
      assertEquals(paragraph.getText(), "88880000");
      paragraph = document.getParagraphArray(7);
      assertEquals(paragraph.getText(), "8888万元");
      paragraph = document.getParagraphArray(8);
      assertEquals(paragraph.getText(), "阿黄");
      paragraph = document.getParagraphArray(9);
      assertEquals(paragraph.getText(), "6");
      paragraph = document.getParagraphArray(10);
      assertEquals(paragraph.getText(), "阿蓝");
paragraph = document.getParagraphArray(11);
assertEquals(paragraph.getText(), "ElFunction");

      document.close();

  }
 
Example 6
Source File: Issue157.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@Test
public void testNullMerge() throws Exception {

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("title", "testTitle");

    // 不加入这一段, 虽然 {{+teachers}} 设置值了,但是仍然不会渲染
    /*
     * List<Teacher> stuList = new ArrayList<Teacher>(); Teacher p1 = new Teacher();
     * p1.setName("test1"); stuList.add(p1);
     * 
     * p1 = new Teacher(); p1.setName("test2"); stuList.add(p1);
     * 
     * params.put("students", new DocxRenderData(new
     * File("src/test/resources/issue/test_teacher.docx"), stuList));
     */

    List<Teacher> teacherList = new ArrayList<Teacher>();
    Teacher t1 = new Teacher();
    t1.setName("t1");
    t1.setAge(18);
    teacherList.add(t1);

    t1 = new Teacher();
    t1.setName("t2");
    t1.setAge(36);
    teacherList.add(t1);

    params.put("teachers", new DocxRenderData(new File("src/test/resources/issue/157_MERGE.docx"), teacherList));

    XWPFTemplate doc = XWPFTemplate.compile("src/test/resources/issue/157.docx");
    doc.render(params);

    XWPFDocument document = XWPFTestSupport.readNewDocument(doc);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "testTitle");
    paragraph = document.getParagraphArray(1);
    assertEquals(paragraph.getText(), "");
    paragraph = document.getParagraphArray(2);
    assertEquals(paragraph.getText(), "老师:t1\n" + "年龄:18");
    paragraph = document.getParagraphArray(3);
    assertEquals(paragraph.getText(), "老师:t2\n" + "年龄:36");

    document.close();

}
 
Example 7
Source File: Issue111.java    From poi-tl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testCRBR() throws Exception {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < 5; i++) {
        String info = "姓名{{name" + i + "}},年龄:{{age" + i + "}}。";
        sb.append(info).append("\n");
    }
    final String Info = sb.toString();
    XWPFTemplate templateRule = XWPFTemplate.compile("src/test/resources/issue/111.docx")
            .render(new HashMap<String, Object>() {
                {
                    put("title", Info);
                }
            });

    templateRule.writeToFile("out_111temp.docx");
    XWPFTemplate template = XWPFTemplate.compile("out_111temp.docx").render(new HashMap<String, Object>() {
        {
            for (int j = 0; j < 5; j++) {
                put("name" + j, "测试姓名" + j);
                put("age" + j, "测试年龄" + j);
            }
        }
    });

    // template.writeToFile("out_issue_111.docx");
    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    XWPFParagraph paragraph = document.getParagraphArray(0);
    assertEquals(paragraph.getText(), "姓名测试姓名0,年龄:测试年龄0。\n" + 
            "姓名测试姓名1,年龄:测试年龄1。\n" + 
            "姓名测试姓名2,年龄:测试年龄2。\n" + 
            "姓名测试姓名3,年龄:测试年龄3。\n" + 
            "姓名测试姓名4,年龄:测试年龄4。\n" + 
            "姓名测试姓名0,年龄:测试年龄0。\n" + 
            "姓名测试姓名1,年龄:测试年龄1。\n" + 
            "姓名测试姓名2,年龄:测试年龄2。\n" + 
            "姓名测试姓名3,年龄:测试年龄3。\n" + 
            "姓名测试姓名4,年龄:测试年龄4。\n");
    
    document.close();

    new File("out_111temp.docx").deleteOnExit();
}