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

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFDocument#getTables() . 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: BookMarks.java    From frpMgr with MIT License 6 votes vote down vote up
/** 
 * 构造函数,用以分析文档,解析出所有的标签
 * @param document  Word OOXML document instance. 
 */
public BookMarks(XWPFDocument document) {

	//初始化标签缓存
	this._bookmarks = new HashMap<String, BookMark>();

	// 首先解析文档普通段落中的标签 
	this.procParaList(document.getParagraphs());

	//利用繁琐的方法,从所有的表格中得到得到标签,处理比较原始和简单
	List<XWPFTable> tableList = document.getTables();

	for (XWPFTable table : tableList) {
		//得到表格的列信息
		List<XWPFTableRow> rowList = table.getRows();
		for (XWPFTableRow row : rowList) {
			//得到行中的列信息
			List<XWPFTableCell> cellList = row.getTableCells();
			for (XWPFTableCell cell : cellList) {
				//逐个解析标签信息
				//this.procParaList(cell.getParagraphs(), row);
				this.procParaList(cell);
			}
		}
	}
}
 
Example 2
Source File: WordXTableParser.java    From sun-wordtable-read with Apache License 2.0 6 votes vote down vote up
public List<WordTable> parse(InputStream inputStream) {
	List<WordTable> wordTables = Lists.newArrayList();

	try {
		XWPFDocument doc = new XWPFDocument(inputStream); // 载入文档  

		//获取文档中所有的表格  
		List<XWPFTable> tables = doc.getTables();
		for (XWPFTable table : tables) {
			ISingleWordTableParser parser = new SingleWordXTableParser(table, this.context);
			WordTable wordTable = parser.parse();
			wordTables.add(wordTable);
		}
	} catch (Exception e) {
		logger.error(e.getMessage(), e);
	} finally {
		IOUtils.closeQuietly(inputStream);
	}

	return wordTables;
}
 
Example 3
Source File: Issue313.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
public void excelRender(String temppath) throws Exception {
    TextRenderData[] arr = new TextRenderData[10];
    arr[1] = new TextRenderData("FFFFFF", "姓名");
    TableStyle headerStyle = new TableStyle();
    headerStyle.setBackgroundColor("80C687");// 表的标题背景
    RowRenderData header = RowRenderData.build(new TextRenderData("FFFFFF", "姓名"),
            new TextRenderData("FFFFFF", "学历"), new TextRenderData("FFFFFF", "aaf"));// 表头
    header.setRowStyle(headerStyle);
    RowRenderData row0 = RowRenderData.build("张三", "研究生", "");// 每列的数据
    RowRenderData row1 = RowRenderData.build("李四", "博士", "aa");
    RowRenderData row2 = RowRenderData.build("王五", "博士后", "");
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("excel_first", new MiniTableRenderData(header, Arrays.asList(row0, row1)));
    map.put("excel_second", new MiniTableRenderData(header, Arrays.asList(row2)));
    XWPFTemplate template = XWPFTemplate.compile(temppath).render(map);

    XWPFDocument document = XWPFTestSupport.readNewDocument(template);
    List<XWPFTable> tables = document.getTables();
    assertEquals(tables.size(), 2);

    assertEquals(tables.get(0).getRows().size(), 3);
    assertEquals(tables.get(1).getRows().size(), 2);

    document.close();
    new File(temppath).deleteOnExit();
}
 
Example 4
Source File: WordTest.java    From onetwo with Apache License 2.0 5 votes vote down vote up
@Test
	public void testRead() throws Exception {
		String path = ExcelUtils.class.getClassLoader().getResource("").getPath()+filePath;
		OPCPackage docPackage = ExcelUtils.readPackage(path);

		Map<String, String> headerMapper = new HashMap<String, String>(){{
			put("名称", "name");
			put("类型", "type");
			put("是否必须", "required");
			put("描述", "comment");
		}};
		BeanWpfRowMapper<FieldInfo> mapper = new BeanWpfRowMapper<>(headerMapper, FieldInfo.class);
		ListExcelDataExtractor<FieldInfo, XWPFTable> paramExtractor = new ListExcelDataExtractor<>(mapper);
		
		System.out.println("=================================================================");
		XWPFDocument xwpf = new XWPFDocument(docPackage);
		List<XWPFTable> tables = xwpf.getTables();
		int index = 0;
		for(XWPFTable table : tables){
			if(index==0){
				JavaClassInfo clazz = new JavaClassInfo("Test");
				List<FieldInfo> fields = paramExtractor.extractData(table);
				clazz.setFields(fields);
//				System.out.println(clazz.toJavaString());
				System.out.println(clazz.toEsMappingString());
			}
			index++;
		}
		
	}
 
Example 5
Source File: FormFieldTests.java    From kbase-doc with Apache License 2.0 4 votes vote down vote up
@Test
    public void testDocx() throws IOException {
        XWPFDocument document = new XWPFDocument(new FileInputStream("D:\\Xiaoi\\Items\\2019-07-02 合同智能分析工具\\04_现场数据\\4.25国网北京信通公司110kV半壁店站等63个站点通信蓄电池改造勘察设计合同.docx"));

        List<XWPFParagraph> paragraphs = document.getParagraphs();
        List<XWPFTable> tables = document.getTables();
        for (XWPFTable table : tables){
        }
        for (XWPFParagraph paragraph : paragraphs){
//            System.out.println(paragraph.getCTP().xmlText());
            printContentsOfTextBox(paragraph);
        }
    }