|
|
|
@ -5,10 +5,12 @@ import com.hnac.hzims.common.logs.utils.StringUtils;
|
|
|
|
|
import com.hnac.hzims.safeproduct.constants.SafeProductConstant; |
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
|
import org.apache.commons.io.IOUtils; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFDocument; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTable; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTableCell; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.XWPFTableRow; |
|
|
|
|
import org.apache.poi.xwpf.usermodel.*; |
|
|
|
|
import org.json.JSONArray; |
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink; |
|
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; |
|
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText; |
|
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; |
|
|
|
|
import org.springblade.core.log.exception.ServiceException; |
|
|
|
|
import org.springblade.core.tool.utils.DateUtil; |
|
|
|
@ -68,10 +70,10 @@ public class BaseUtil {
|
|
|
|
|
* @param pdfPath pdf生成路径 |
|
|
|
|
* @param response 响应类 |
|
|
|
|
*/ |
|
|
|
|
public static void exportDocument(String templateFile, Map<String, Object> params, String wordPath, String pdfPath, HttpServletResponse response) { |
|
|
|
|
public static void exportDocument(String templateFile, Map<String, Object> params, String wordPath, String pdfPath,String findName, HttpServletResponse response) { |
|
|
|
|
try { |
|
|
|
|
List<XWPFDocument> res = new ArrayList<>(); |
|
|
|
|
XWPFDocument document = BaseUtil.fillDocument(templateFile, params); |
|
|
|
|
XWPFDocument document = BaseUtil.fillDocument(templateFile, params,findName); |
|
|
|
|
res.add(document); |
|
|
|
|
BaseUtil.exportWord(res, wordPath); |
|
|
|
|
} catch (Exception e) { |
|
|
|
@ -112,15 +114,55 @@ public class BaseUtil {
|
|
|
|
|
* @param params 数据参数 |
|
|
|
|
* @return 文件 |
|
|
|
|
*/ |
|
|
|
|
public static XWPFDocument fillDocument(String templatePath, Map<String, Object> params) { |
|
|
|
|
public static XWPFDocument fillDocument(String templatePath, Map<String, Object> params, String findName) { |
|
|
|
|
XWPFDocument xwpfDocument = null; |
|
|
|
|
try { |
|
|
|
|
xwpfDocument = WordExportUtil.exportWord07(templatePath, params); |
|
|
|
|
if (StringUtils.isNotEmpty(findName)) { |
|
|
|
|
String findText = params.get(findName).toString(); |
|
|
|
|
if (StringUtils.isNotEmpty(findText)) { |
|
|
|
|
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 (XWPFParagraph p : cell.getParagraphs()) { |
|
|
|
|
Iterator<XWPFRun> iterator = p.getRuns().iterator(); |
|
|
|
|
while (iterator.hasNext()) { |
|
|
|
|
XWPFRun r = iterator.next(); |
|
|
|
|
String text = r.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"); |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if (templatePath.equals("template/卫生自查表.docx")) { |
|
|
|
|
formatHygieneRecordTable(xwpfDocument); |
|
|
|
|
} |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
log.error("数据写入异常: {}", e.getMessage()); |
|
|
|
|
log.error("数据写入异常: {}",e.getCause().toString()+ e.getStackTrace()); |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
return xwpfDocument; |
|
|
|
|