部分代码..................
.
..
..
/**
* 处理动态表格
* 默认取第二行第一列的标识
* @param table
* @param ctRow
* @param values 需要添加的数据
*/
public static void processDynamicTable(XWPFTable table, CTRow ctRow, List values) {
if(values==null || values.size()==0) return;
for (Iterator iterator = values.iterator(); iterator.hasNext();) {
String[] value = iterator.next();
XWPFTableRow newRow = new XWPFTableRow(ctRow,table);
List cells = newRow.getTableCells();
if(cells==null || cells.size()==0) return;
for (int j = 0; j < cells.size(); j++) {
XWPFTableCell cell = cells.get(j);
cell.removeParagraph(0);
if(j<=value.length) cell.setText(value[j]);
}
table.addRow(newRow);
}
}
/**
* 处理表格
* @param doc
* @param dataMap
*/
public static void processTable(XWPFDocument doc, Map dataMap) {
Iterator iter = doc.getTablesIterator();
while(iter.hasNext()) {
XWPFTable table = iter.next();
List rows = table.getRows();
if(rows==null || rows.size()==0) continue;
CTRow ctRow = null;
for (int i = 0; i < rows.size(); i++) {
XWPFTableRow row = rows.get(i);
ctRow = (CTRow)row.getCtRow().copy();
List cells = row.getTableCells();
if(cells==null || cells.size()==0) continue;
//处理标致
boolean sign = false;
for (int j = 0; j < cells.size(); j++) {
XWPFTableCell cell = cells.get(j);
if(cell.getText()==null) continue;
if(i==1 && j==0) {
//取第二行第一列的值,再添加table.关键字之后与dataMap匹配,有则当动态表格处理
String key = "table."+ cell.getText();
if(dataMap.containsKey(key)) {
List values = (List)dataMap.get(key);
table.removeRow(1);
processDynamicTable(table, ctRow, values);
//更新处理标致
sign = true;
break;
}
}
if(!cell.getText().startsWith("\\$") && cell.getText().endsWith("\\$")) continue;
String key = cell.getText().replaceAll("\\$", "");
if(dataMap.containsKey(key)) {
1