easypoi4.3.0 word模板導出第一列非循環語句時導出為空解決方法


參考

https://blog.csdn.net/cheng137666/article/details/111279476

源碼測試

 輸出后

 數據無法循環

如何修改源碼看自己怎么方便怎么來

比如1.下載源碼自己打包再引入; 2.放到自己的私有服; 3.替換(最簡單)。

 

 第一列非循環語句時導出為空解決方法

修改2個文件

debug查看到ParseWord07類中的parseThisTable方法,源碼如下:

 

 

 需要改的地方就是第五行,之后替換表格內容的代碼最重要的就是listobj參數。這源代碼中第五行很明顯,他只是根據第一列的內容來獲取listobj,而我們的模板循環語句並未在第一列上,因此導致word導出時,無法替換內容。原因講完了,下面都是動手節了。

替換源碼文件,在最上面找到包,這邊需要替換2個文件

 

 在本地新建包,然后復制這2個文件到各自的包里面

如下:

 然后再修改需要更改的代碼

ParseWord07類中 第 199行 方法 替換為下面的

/**
* 解析這個表格
*
* @param table
* @param map
* @author JueYue
* 2013-11-17
*/
private void parseThisTable(XWPFTable table, Map<String, Object> map) throws Exception {
XWPFTableRow row;
List<XWPFTableCell> cells;
Object listobj;
for (int i = 0; i < table.getNumberOfRows(); i++) {
row = table.getRow(i);
cells = row.getTableCells();
listobj = null;
int col = 0;
for (XWPFTableCell cell : cells) {
listobj = this.checkThisTableIsNeedIterator(cell, map);
if (listobj != null) {
break;
}
col++;
}
if (listobj == null) {
this.parseThisRow(cells, map);
} else if (listobj instanceof ExcelListEntity) {
(new ExcelEntityParse()).parseNextRowAndAddRow(table, i, (ExcelListEntity) listobj);
i = i + ((ExcelListEntity) listobj).getList().size() - 1;
} else {
ExcelMapParse.parseNextRowAndAddRow(table, i, (List) listobj, col);
i = i + ((List) listobj).size() - 1;
}

}
}

 ExcelMapParse 類中 第144 方法替換為下面的

 /**
* 解析下一行,並且生成更多的行
*
* @param table
* @param index
* @param list
*/
public static void parseNextRowAndAddRow(XWPFTable table, int index, List<Object> list, int col) throws Exception {
XWPFTableRow currentRow = table.getRow(index);
String[] params = parseCurrentRowGetParams(currentRow);
String listname = params[col];
boolean isCreate = !listname.contains("!fe:");
listname = listname.replace("!fe:", "").replace("$fe:", "").replace("fe:", "").replace("{{", "");
String[] keys = listname.replaceAll("\\s{1,}", " ").trim().split(" ");
params[col] = keys[1];
List<XWPFTableCell> tempCellList = new ArrayList();
tempCellList.addAll(table.getRow(index).getTableCells());
// int cellIndex = false;
Map<String, Object> tempMap = Maps.newHashMap();
LOGGER.debug("start for each data list :{}", list.size());
Iterator var11 = list.iterator();

while (var11.hasNext()) {
Object obj = var11.next();
currentRow = isCreate ? table.insertNewTableRow(index++) : table.getRow(index++);
tempMap.put("t", obj);

//如果有合並單元格情況,會導致params越界,這里需要補齊
String[] paramsNew = (String[]) ArrayUtils.clone(params);
if (params.length < currentRow.getTableCells().size()) {
for (int i = 0; i < currentRow.getTableCells().size() - params.length; i++) {
paramsNew = (String[]) ArrayUtils.add(paramsNew, 0, "placeholderLc_" + i);
}
}

String val;
int cellIndex;
for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); ++cellIndex) {
val = PoiElUtil.eval(paramsNew[cellIndex], tempMap).toString();
//源代碼的bug 此方法無法刪除單元格中的內容
//currentRow.getTableCells().get(cellIndex).setText("");
//使用此方法清空單元格內容
if (!Strings.isNullOrEmpty(val)) {
currentRow.getTableCells().get(cellIndex).getParagraphs().forEach(p -> p.getRuns().forEach(r -> r.setText("", 0)));
}
PoiWordStyleUtil.copyCellAndSetValue(cellIndex >= tempCellList.size() ? tempCellList.get(tempCellList.size() - 1) : tempCellList.get(cellIndex)
, currentRow.getTableCells().get(cellIndex), val);
}

while (cellIndex < paramsNew.length) {
val = PoiElUtil.eval(paramsNew[cellIndex], tempMap).toString();
PoiWordStyleUtil.copyCellAndSetValue((XWPFTableCell) tempCellList.get(cellIndex), currentRow.createCell(), val);
++cellIndex;
}
}

table.removeRow(index);
}

重啟項目,再次測試

 結果

 大功告成


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM