org.apache.poi.xwpf.usermodel.XWPFTable Java Examples

The following examples show how to use org.apache.poi.xwpf.usermodel.XWPFTable. 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: ExcelEntityParse.java    From autopoi with Apache License 2.0 7 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
	if (index < headRows) {
		throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
	}
	Map<String, Integer> map = new HashMap<String, Integer>();
	String text;
	for (int j = 0; j < headRows; j++) {
		List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
		for (int i = 0; i < cells.size(); i++) {
			text = cells.get(i).getText();
			if (StringUtils.isEmpty(text)) {
				throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
			}
			map.put(text, i);
		}
	}
	return map;
}
 
Example #2
Source File: TableTools.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
/**
 * 合并列单元格
 * 
 * @param table
 *            表格对象
 * @param col
 *            列 从0开始
 * @param fromRow
 *            起始行
 * @param toRow
 *            结束行
 */
public static void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
    if (toRow <= fromRow) return;
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
        CTTcPr tcPr = getTcPr(cell);
        CTVMerge vMerge = tcPr.addNewVMerge();
        if (rowIndex == fromRow) {
            // The first merged cell is set with RESTART merge value
            vMerge.setVal(STMerge.RESTART);
        } else {
            // Cells which join (merge) the first one, are set with CONTINUE
            vMerge.setVal(STMerge.CONTINUE);
        }
    }
}
 
Example #3
Source File: TableTools.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
/**
 * 设置表格边框
 * 
 * @param table
 * @param size
 */
public static void borderTable(XWPFTable table, int size) {
    CTTblPr tblPr = getTblPr(table);
    CTTblBorders tblBorders = tblPr.getTblBorders();
    if (null == tblBorders) {
        tblBorders = tblPr.addNewTblBorders();
    }
    BigInteger borderSize = BigInteger.valueOf(size);
    if (!tblBorders.isSetBottom()) tblBorders.addNewBottom();
    if (!tblBorders.isSetLeft()) tblBorders.addNewLeft();
    if (!tblBorders.isSetTop()) tblBorders.addNewTop();
    if (!tblBorders.isSetRight()) tblBorders.addNewRight();
    if (!tblBorders.isSetInsideH()) tblBorders.addNewInsideH();
    if (!tblBorders.isSetInsideV()) tblBorders.addNewInsideV();
    tblBorders.getBottom().setSz(borderSize);
    tblBorders.getLeft().setSz(borderSize);
    tblBorders.getTop().setSz(borderSize);
    tblBorders.getRight().setSz(borderSize);
    tblBorders.getInsideH().setSz(borderSize);
    tblBorders.getInsideV().setSz(borderSize);
}
 
Example #4
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 #5
Source File: ParseWord07.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JueYue
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
	XWPFTableRow row;
	List<XWPFTableCell> cells;
	Object listobj;
	ExcelEntityParse excelEntityParse = new ExcelEntityParse();
	for (int i = 0; i < table.getNumberOfRows(); i++) {
		row = table.getRow(i);
		cells = row.getTableCells();
		if (cells.size() == 1) {
			listobj = checkThisTableIsNeedIterator(cells.get(0), map);
			if (listobj == null) {
				parseThisRow(cells, map);
			} else if (listobj instanceof ExcelListEntity) {
				table.removeRow(i);// 删除这一行
				excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
			} else {
				table.removeRow(i);// 删除这一行
				ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
			}
		} else {
			parseThisRow(cells, map);
		}
	}
}
 
Example #6
Source File: ExcelEntityParse.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
	if (index < headRows) {
		throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
	}
	Map<String, Integer> map = new HashMap<String, Integer>();
	String text;
	for (int j = 0; j < headRows; j++) {
		List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
		for (int i = 0; i < cells.size(); i++) {
			text = cells.get(i).getText();
			if (StringUtils.isEmpty(text)) {
				throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
			}
			map.put(text, i);
		}
	}
	return map;
}
 
Example #7
Source File: ExcelMapParse.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析下一行,并且生成更多的行
 * 
 * @Author JEECG
 * @date 2013-11-18
 * @param table
 * @param listobj2
 */
public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception {
	XWPFTableRow currentRow = table.getRow(index);
	String[] params = parseCurrentRowGetParams(currentRow);
	table.removeRow(index);// 移除这一行
	int cellIndex = 0;// 创建完成对象一行好像多了一个cell
	for (Object obj : list) {
		currentRow = table.createRow();
		for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) {
			currentRow.getTableCells().get(cellIndex).setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString());
		}
		for (; cellIndex < params.length; cellIndex++) {
			currentRow.createCell().setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString());
		}
	}

}
 
Example #8
Source File: ExcelMapParse.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析下一行,并且生成更多的行
 * 
 * @Author JueYue
 * @date 2013-11-18
 * @param table
 * @param listobj2
 */
public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list) throws Exception {
	XWPFTableRow currentRow = table.getRow(index);
	String[] params = parseCurrentRowGetParams(currentRow);
	table.removeRow(index);// 移除这一行
	int cellIndex = 0;// 创建完成对象一行好像多了一个cell
	for (Object obj : list) {
		currentRow = table.createRow();
		for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) {
			currentRow.getTableCells().get(cellIndex).setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString());
		}
		for (; cellIndex < params.length; cellIndex++) {
			currentRow.createCell().setText(PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0).toString());
		}
	}

}
 
Example #9
Source File: ParseWord07.java    From autopoi with Apache License 2.0 6 votes vote down vote up
private void parseWordSetValue(MyXWPFDocument doc, Map<String, Object> map) throws Exception {
	// 第一步解析文档
	parseAllParagraphic(doc.getParagraphs(), map);
	// 第二步解析页眉,页脚
	parseHeaderAndFoot(doc, map);
	// 第三步解析所有表格
	XWPFTable table;
	Iterator<XWPFTable> itTable = doc.getTablesIterator();
	while (itTable.hasNext()) {
		table = itTable.next();
		if (table.getText().indexOf("{{") != -1) {
			parseThisTable(table, map);
		}
	}

}
 
Example #10
Source File: ParseWord07.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JEECG
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
	XWPFTableRow row;
	List<XWPFTableCell> cells;
	Object listobj;
	ExcelEntityParse excelEntityParse = new ExcelEntityParse();
	for (int i = 0; i < table.getNumberOfRows(); i++) {
		row = table.getRow(i);
		cells = row.getTableCells();
		if (cells.size() == 1) {
			listobj = checkThisTableIsNeedIterator(cells.get(0), map);
			if (listobj == null) {
				parseThisRow(cells, map);
			} else if (listobj instanceof ExcelListEntity) {
				table.removeRow(i);// 删除这一行
				excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
			} else {
				table.removeRow(i);// 删除这一行
				ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
			}
		} else {
			parseThisRow(cells, map);
		}
	}
}
 
Example #11
Source File: ExcelEntityParse.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table) throws Exception {
	ExcelExportEntity entity;
	XWPFTableRow row;
	if (table.getRow(index) == null) {
		row = table.createRow();
		row.setHeight(getRowHeight(excelParams));
	} else {
		row = table.getRow(index);
	}
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		Object value = getCellValue(entity, obj);
		if (entity.getType() == 1) {
			setCellValue(row, value, cellNum++);
		}
	}
}
 
Example #12
Source File: ExcelEntityParse.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
public void createListCells(int index, int cellNum, Object obj, List<ExcelExportEntity> excelParams, XWPFTable table) throws Exception {
	ExcelExportEntity entity;
	XWPFTableRow row;
	if (table.getRow(index) == null) {
		row = table.createRow();
		row.setHeight(getRowHeight(excelParams));
	} else {
		row = table.getRow(index);
	}
	for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
		entity = excelParams.get(k);
		Object value = getCellValue(entity, obj);
		if (entity.getType() == 1) {
			setCellValue(row, value, cellNum++);
		}
	}
}
 
Example #13
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 #14
Source File: ReferencePolicyTest.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Test
public void testOptionalTextTable() throws Exception {
    Configure configure = Configure.newBuilder().referencePolicy(new OptionalTextTableRefRenderPolicy() {

        @Override
        public void doRender(XWPFTable t, XWPFTemplate template) {
            System.out.println(t);
            assertNotNull(t);
        }

        @Override
        public String optionalText() {
            return "let's table";
        }
    }).build();
    XWPFTemplate.compile(template_file, configure).render(new HashMap<>()).close();

}
 
Example #15
Source File: CellBodyContainer.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public XWPFTable insertNewTable(XWPFRun run, int row, int col) {
    XmlCursor cursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor();
    XWPFTable table = insertNewTbl(cursor);

    // hack for cursor.removeXmlContents(); in XWPFTableCell
    List<XWPFTableRow> rows = table.getRows();
    for (int i = 0; i < rows.size(); i++) {
        table.removeRow(i);
    }
    for (int i = 0; i < row; i++) {
        XWPFTableRow tabRow = (table.getRow(i) == null) ? table.createRow() : table.getRow(i);
        for (int k = 0; k < col; k++) {
            if (tabRow.getCell(k) == null) {
                tabRow.createCell();
            }
        }
    }
    return table;
}
 
Example #16
Source File: MiniTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
public static void renderTable(XWPFRun run, MiniTableRenderData tableData) {
    // 1.计算行和列
    int row = tableData.getRows().size(), col = 0;
    if (!tableData.isSetHeader()) {
        col = getMaxColumFromData(tableData.getRows());
    } else {
        row++;
        col = tableData.getHeader().size();
    }

    // 2.创建表格
    BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
    XWPFTable table = bodyContainer.insertNewTable(run, row, col);
    TableTools.initBasicTable(table, col, tableData.getWidth(), tableData.getStyle());

    // 3.渲染数据
    int startRow = 0;
    if (tableData.isSetHeader()) Helper.renderRow(table, startRow++, tableData.getHeader());
    for (RowRenderData data : tableData.getRows()) {
        Helper.renderRow(table, startRow++, data);
    }

}
 
Example #17
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 #18
Source File: DynamicTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
    RunTemplate runTemplate = (RunTemplate) eleTemplate;
    XWPFRun run = runTemplate.getRun();
    run.setText("", 0);
    try {
        if (!TableTools.isInsideTable(run)) {
            throw new IllegalStateException(
                    "The template tag " + runTemplate.getSource() + " must be inside a table");
        }
        XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
        XWPFTable table = cell.getTableRow().getTable();
        render(table, data);
    } catch (Exception e) {
        throw new RenderException("dynamic table error:" + e.getMessage(), e);
    }
}
 
Example #19
Source File: IndexRefRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
protected T locate(XWPFTemplate template) {
    int positon = index();
    logger.info("Try locate the {} object at position of {}...",
            ClassUtils.getSimpleName(genericType), positon);
    Object t = null;
    NiceXWPFDocument doc = template.getXWPFDocument();
    if (XWPFChart.class.equals(genericType)) {
        t = doc.getCharts().get(positon);
    } else if (XWPFTable.class.equals(genericType)) {
        t = doc.getTables().get(positon);
    } else if (XWPFPictureData.class.equals(genericType)) {
        t = doc.getAllPictures().get(positon);
    } else if (XWPFPicture.class.equals(genericType)) {
        t = doc.getAllEmbeddedPictures().get(positon);
    } else if (XWPFParagraph.class.equals(genericType)) {
        t = doc.getParagraphs().get(positon);
    }
    return (T) t;
}
 
Example #20
Source File: M2DocEvaluator.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public XWPFParagraph caseTable(Table table) {
    // Create the table structure in the destination document.

    CTTbl copy = (CTTbl) table.getTable().getCTTbl().copy();
    copy.getTrList().clear();
    final XWPFTable saveTable = currentGeneratedTable;
    try {
        final XWPFTable newTable = POIServices.getInstance().createTable(generatedDocument);
        newTable.getCTTbl().set(copy);
        currentGeneratedTable = newTable;
        // iterate on the row
        for (Row row : table.getRows()) {
            doSwitch(row);
        }
    } finally {
        currentGeneratedTable = saveTable;
    }

    currentTemplateParagraph = null;

    return null;
}
 
Example #21
Source File: OptionalTextTableRefRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
protected XWPFTable locate(XWPFTemplate template) {
    logger.info("Try locate the XWPFTable object which mathing optional text [{}]...",
            optionalText());
    NiceXWPFDocument xwpfDocument = template.getXWPFDocument();
    List<XWPFTable> tables = xwpfDocument.getAllTables();
    for (XWPFTable table : tables) {
        CTTbl ctTbl = table.getCTTbl();
        CTTblPr tblPr = ctTbl.getTblPr();
        CTTblPrImpl cTTblPrImpl = (CTTblPrImpl) tblPr;
        String caption = getTblStringVal(cTTblPrImpl, TBLCAPTION);
        if (Objects.equals(optionalText(), caption)) return table;
        String description = getTblStringVal(cTTblPrImpl, TBLDESCRIPTION);
        if (Objects.equals(optionalText(), description)) return table;
    }

    return null;
}
 
Example #22
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Copy Table Style.
 * 
 * @param inputTable
 *            input Table
 * @param outputDoc
 *            outputDoc where copy style
 * @throws IOException
 *             if the copy fails
 */
private static void copyTableStyle(XWPFTable inputTable, XWPFDocument outputDoc) throws IOException {
    @SuppressWarnings("resource")
    final XWPFDocument inputDoc = inputTable.getBody().getXWPFDocument();
    final XWPFStyle style = inputDoc.getStyles().getStyle(inputTable.getStyleID());
    if (outputDoc != null && style != null) {
        if (outputDoc.getStyles() == null) {
            outputDoc.createStyles();
        }

        List<XWPFStyle> usedStyleList = inputDoc.getStyles().getUsedStyleList(style);
        for (XWPFStyle xwpfStyle : usedStyleList) {
            outputDoc.getStyles().addStyle(xwpfStyle);
        }
    }
}
 
Example #23
Source File: RawCopier.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Updates bookmarks for the given output {@link XWPFTable} according to its input {@link XWPFTable}.
 * 
 * @param bookmarkManager
 *            the {@link BookmarkManager}
 * @param outputTable
 *            the output {@link XWPFTable}
 * @param inputTable
 *            the input {@link XWPFTable}
 */
private void updateBookmarks(BookmarkManager bookmarkManager, final XWPFTable outputTable,
        final XWPFTable inputTable) {
    final List<XWPFTableRow> inputRows = inputTable.getRows();
    final List<XWPFTableRow> outputRows = outputTable.getRows();
    for (int rowIndex = 0; rowIndex < inputRows.size(); rowIndex++) {
        final XWPFTableRow inputRow = inputRows.get(rowIndex);
        final XWPFTableRow outputRow = outputRows.get(rowIndex);
        final List<XWPFTableCell> inputCells = inputRow.getTableCells();
        final List<XWPFTableCell> outputCells = outputRow.getTableCells();
        for (int cellIndex = 0; cellIndex < inputCells.size(); cellIndex++) {
            final XWPFTableCell inputCell = inputCells.get(cellIndex);
            final XWPFTableCell outputCell = outputCells.get(cellIndex);
            final List<IBodyElement> inputBodyElements = inputCell.getBodyElements();
            final List<IBodyElement> outputBodyElements = outputCell.getBodyElements();
            for (int bodyElementIndex = 0; bodyElementIndex < inputBodyElements.size(); bodyElementIndex++) {
                final IBodyElement inputBodyElement = inputBodyElements.get(bodyElementIndex);
                if (inputBodyElement instanceof XWPFParagraph) {
                    final IBodyElement outputBodyElement = outputBodyElements.get(bodyElementIndex);
                    updateBookmarks(bookmarkManager, ((XWPFParagraph) outputBodyElement).getCTP(),
                            ((XWPFParagraph) inputBodyElement).getCTP());
                }
            }
        }
    }
}
 
Example #24
Source File: CustomTableRenderPolicy.java    From poi-tl with Apache License 2.0 6 votes vote down vote up
@Override
public void doRender(RenderContext<Object> context) throws Exception {

    XWPFRun run = context.getRun();
    BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);
    // 定义行列
    int row = 10, col = 8;
    // 插入表格
    XWPFTable table = bodyContainer.insertNewTable(run, row, col);

    // 定义表格宽度、边框和样式
    TableTools.widthTable(table, MiniTableRenderData.WIDTH_A4_FULL, col);
    TableTools.borderTable(table, 4);

    // TODO 调用XWPFTable API操作表格:data对象可以包含任意你想要的数据,包括图片文本等
    // TODO 调用MiniTableRenderPolicy.Helper.renderRow方法快速方便的渲染一行数据
    // TODO 调用TableTools类方法操作表格,比如合并单元格
    // ......
    TableTools.mergeCellsHorizonal(table, 0, 0, 7);
    TableTools.mergeCellsVertically(table, 0, 1, 9);

}
 
Example #25
Source File: AbstractBodyParser.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Parses a {@link Table}.
 * 
 * @param wtable
 *            the table to parse
 * @return the created object
 * @throws DocumentParserException
 *             if a problem occurs while parsing.
 */
protected Table parseTable(XWPFTable wtable) throws DocumentParserException {
    if (wtable == null) {
        throw new IllegalArgumentException("parseTable can't be called on a null argument.");
    }
    Table table = (Table) EcoreUtil.create(TemplatePackage.Literals.TABLE);
    table.setTable(wtable);
    for (XWPFTableRow tablerow : wtable.getRows()) {
        Row row = (Row) EcoreUtil.create(TemplatePackage.Literals.ROW);
        table.getRows().add(row);
        row.setTableRow(tablerow);
        for (XWPFTableCell tableCell : tablerow.getTableCells()) {
            Cell cell = (Cell) EcoreUtil.create(TemplatePackage.Literals.CELL);
            row.getCells().add(cell);
            cell.setTableCell(tableCell);
            AbstractBodyParser parser = getNewParser(tableCell);
            cell.setBody(parser.parseBlock(null, TokenType.EOF));
        }
    }
    return table;
}
 
Example #26
Source File: ExcelMapParse.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析下一行,并且生成更多的行
 * 
 * @Author JueYue
 * @date 2013-11-18
 * @param table
 * @param listobj2
 */
public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list)
                                                                                       throws Exception {
    XWPFTableRow currentRow = table.getRow(index);
    String[] params = parseCurrentRowGetParams(currentRow);
    table.removeRow(index);// 移除这一行
    int cellIndex = 0;// 创建完成对象一行好像多了一个cell
    for (Object obj : list) {
        currentRow = table.createRow();
        for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) {
            currentRow
                .getTableCells()
                .get(cellIndex)
                .setText(
                    PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0)
                        .toString());
        }
        for (; cellIndex < params.length; cellIndex++) {
            currentRow.createCell().setText(
                PoiPublicUtil.getValueDoWhile(obj, params[cellIndex].split("\\."), 0)
                    .toString());
        }
    }

}
 
Example #27
Source File: ExcelEntityParse.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 创建List之后的各个Cells
 * 
 * @param styles
 */
public void createListCells(int index, int cellNum, Object obj,
                            List<ExcelExportEntity> excelParams, XWPFTable table)
                                                                                 throws Exception {
    ExcelExportEntity entity;
    XWPFTableRow row;
    if (table.getRow(index) == null) {
        row = table.createRow();
        row.setHeight(getRowHeight(excelParams));
    } else {
        row = table.getRow(index);
    }
    for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
        entity = excelParams.get(k);
        Object value = getCellValue(entity, obj);
        if (entity.getType() == 1) {
            setCellValue(row, value, cellNum++);
        }
    }
}
 
Example #28
Source File: ExcelEntityParse.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取表头数据
 * 
 * @param table
 * @param index
 * @return
 */
private Map<String, Integer> getTitleMap(XWPFTable table, int index, int headRows) {
    if (index < headRows) {
        throw new WordExportException(WordExportEnum.EXCEL_NO_HEAD);
    }
    Map<String, Integer> map = new HashMap<String, Integer>();
    String text;
    for (int j = 0; j < headRows; j++) {
        List<XWPFTableCell> cells = table.getRow(index - j - 1).getTableCells();
        for (int i = 0; i < cells.size(); i++) {
            text = cells.get(i).getText();
            if (StringUtils.isEmpty(text)) {
                throw new WordExportException(WordExportEnum.EXCEL_HEAD_HAVA_NULL);
            }
            map.put(text, i);
        }
    }
    return map;
}
 
Example #29
Source File: ParseWord07.java    From easypoi with Apache License 2.0 6 votes vote down vote up
private void parseWordSetValue(MyXWPFDocument doc, Map<String, Object> map) throws Exception {
    // 第一步解析文档
    parseAllParagraphic(doc.getParagraphs(), map);
    // 第二步解析页眉,页脚
    parseHeaderAndFoot(doc, map);
    // 第三步解析所有表格
    XWPFTable table;
    Iterator<XWPFTable> itTable = doc.getTablesIterator();
    while (itTable.hasNext()) {
        table = itTable.next();
        if (table.getText().indexOf("{{") != -1) {
            parseThisTable(table, map);
        }
    }

}
 
Example #30
Source File: ParseWord07.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 解析这个表格
 * 
 * @Author JueYue
 * @date 2013-11-17
 * @param table
 * @param map
 */
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
    XWPFTableRow row;
    List<XWPFTableCell> cells;
    Object listobj;
    ExcelEntityParse excelEntityParse = new ExcelEntityParse();
    for (int i = 0; i < table.getNumberOfRows(); i++) {
        row = table.getRow(i);
        cells = row.getTableCells();
        if (cells.size() == 1) {
            listobj = checkThisTableIsNeedIterator(cells.get(0), map);
            if (listobj == null) {
                parseThisRow(cells, map);
            } else if (listobj instanceof ExcelListEntity) {
                table.removeRow(i);// 删除这一行
                excelEntityParse.parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
            } else {
                table.removeRow(i);// 删除这一行
                ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj);
            }
        } else {
            parseThisRow(cells, map);
        }
    }
}