|
|
|
@ -119,39 +119,22 @@ public class BaseUtil {
|
|
|
|
|
XWPFDocument xwpfDocument = null; |
|
|
|
|
try { |
|
|
|
|
xwpfDocument = WordExportUtil.exportWord07(templatePath, params); |
|
|
|
|
if (StringUtils.isNotEmpty(findName)) { |
|
|
|
|
// 超链接处理
|
|
|
|
|
if (findName != null && !findName.isEmpty()) { |
|
|
|
|
String findText = params.get(findName).toString(); |
|
|
|
|
if (StringUtils.isNotEmpty(findText)) { |
|
|
|
|
if (findText != null && !findText.isEmpty()) { |
|
|
|
|
JSONArray jsonArray = new JSONArray(findText); |
|
|
|
|
for (XWPFTable table : xwpfDocument.getTables()) { |
|
|
|
|
for (int row = 0; row < table.getNumberOfRows(); row++) { |
|
|
|
|
for (int col = 0; col < table.getRow(row).getTableCells().size(); col++) { |
|
|
|
|
XWPFTableCell cell = table.getRow(row).getCell(col); |
|
|
|
|
for (XWPFTableRow row : table.getRows()) { |
|
|
|
|
for (XWPFTableCell cell : row.getTableCells()) { |
|
|
|
|
for (XWPFParagraph p : cell.getParagraphs()) { |
|
|
|
|
Iterator<XWPFRun> iterator = p.getRuns().iterator(); |
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
XWPFRun r = iterator.next(); |
|
|
|
|
String text = r.getText(0); |
|
|
|
|
for (int index = 0; index < p.getRuns().size(); index++) { |
|
|
|
|
XWPFRun run = p.getRuns().get(index); |
|
|
|
|
String text = run.getText(0); |
|
|
|
|
if (findText.equals(text)) { |
|
|
|
|
r.setText("", 0); // 清除原有文本
|
|
|
|
|
XWPFParagraph paragraph = r.getParagraph(); |
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) { |
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
|
|
|
|
String name = jsonObject.getString("name"); |
|
|
|
|
String url = jsonObject.getString("url"); |
|
|
|
|
url = URLEncoder.encode(url, "UTF-8"); |
|
|
|
|
String id = paragraph.getDocument().getPackagePart().addExternalRelationship(url, XWPFRelation.HYPERLINK.getRelation()).getId(); |
|
|
|
|
CTHyperlink cLink = paragraph.getCTP().addNewHyperlink(); |
|
|
|
|
cLink.setId(id); |
|
|
|
|
CTText ctText = CTText.Factory.newInstance(); |
|
|
|
|
ctText.setStringValue(name); |
|
|
|
|
CTR ctr = CTR.Factory.newInstance(); |
|
|
|
|
ctr.setTArray(new CTText[]{ctText}); |
|
|
|
|
cLink.setRArray(new CTR[]{ctr}); |
|
|
|
|
if (i<jsonArray.length()-1) { |
|
|
|
|
paragraph.createRun().addBreak(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
run.setText("", 0); // 清除原有文本
|
|
|
|
|
XWPFParagraph paragraph = run.getParagraph(); |
|
|
|
|
insertJsonArrayIntoParagraph(jsonArray, paragraph); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -160,17 +143,72 @@ public class BaseUtil {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// 表格处理
|
|
|
|
|
if (templatePath.equals("template/卫生自查表.docx")) { |
|
|
|
|
formatHygieneRecordTable(xwpfDocument); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("数据写入异常: {}",e.getCause().toString()+ e.getStackTrace()); |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return xwpfDocument; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 车辆检查表格式处理 |
|
|
|
|
* @param xwpfDocument word文件类 |
|
|
|
|
*/ |
|
|
|
|
private static void formatCarCheckRecordTable(XWPFDocument xwpfDocument) { |
|
|
|
|
// 获取文档中所有的表格
|
|
|
|
|
List<XWPFTable> tableList = xwpfDocument.getTables(); |
|
|
|
|
if (tableList.size() > 0) { |
|
|
|
|
// 循环表格
|
|
|
|
|
for (XWPFTable table : tableList) { |
|
|
|
|
// 获取表格所有行数
|
|
|
|
|
List<XWPFTableRow> rows = table.getRows(); |
|
|
|
|
// 第五行到倒数第三行为需合并区域
|
|
|
|
|
for (int row = 5; row < rows.size() - 2;) { |
|
|
|
|
XWPFTableCell startCell = rows.get(row).getCell(0); |
|
|
|
|
String startText = startCell.getText(); |
|
|
|
|
int index = row + 1; |
|
|
|
|
for (; index < rows.size(); index++) { |
|
|
|
|
XWPFTableCell endCell = rows.get(index).getCell(0); |
|
|
|
|
String endText = endCell.getText(); |
|
|
|
|
if (!startText.equals(endText)) { |
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
mergeCellsVertically(table, 0, row, index - 1); |
|
|
|
|
row = index; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static void insertJsonArrayIntoParagraph(JSONArray jsonArray, XWPFParagraph paragraph) throws IOException { |
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) { |
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i); |
|
|
|
|
String name = jsonObject.getString("name"); |
|
|
|
|
String url = jsonObject.getString("url"); |
|
|
|
|
// URL编码格式化
|
|
|
|
|
String encodeName = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20"); |
|
|
|
|
String prefixString = url.substring(0, url.lastIndexOf("/") + 1); |
|
|
|
|
String encodeUrl = prefixString + encodeName; |
|
|
|
|
String id = paragraph.getDocument().getPackagePart().addExternalRelationship(encodeUrl, |
|
|
|
|
XWPFRelation.HYPERLINK.getRelation()).getId(); |
|
|
|
|
CTHyperlink cLink = paragraph.getCTP().addNewHyperlink(); |
|
|
|
|
cLink.setId(id); |
|
|
|
|
CTText ctText = CTText.Factory.newInstance(); |
|
|
|
|
ctText.setStringValue(name); |
|
|
|
|
CTR ctr = CTR.Factory.newInstance(); |
|
|
|
|
ctr.setTArray(new CTText[]{ctText}); |
|
|
|
|
cLink.setRArray(new CTR[]{ctr}); |
|
|
|
|
if (i < jsonArray.length() - 1) { |
|
|
|
|
paragraph.createRun().addBreak(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 卫生自查表格式处理 |
|
|
|
|
* @param xwpfDocument word文件类 |
|
|
|
|
*/ |
|
|
|
|