1.将预先设置好的word模板存入数据库或者服务器或者本地都可
2.本人模板如下:
分为两部分,第一个标红的剪头可以进行文档中模板的替换内筒,第二个红箭头代表流转历史根据不同节点不同的人员进行动态生成
3:处理代码如
//word转pdf需要的工具类
private void wordChangePdf(String wordFilePath,
HttpServletResponse response, String newFileName) {
FileOutputStream os = null;
try {
// 凭证 不然切换后有水印
InputStream is = new ClassPathResource("/env/license.xml")
.getInputStream();
License aposeLic = new License();
aposeLic.setLicense(is);
boolean license = true;
if (!license) {
System.out.println("License验证不通过...");
}
// 生成一个空的PDF文件
String pdf = wordFilePath.substring(0, wordFilePath.indexOf("."))
+ ".pdf";
File file = new File(pdf);
os = new FileOutputStream(file);
// 要转换的word文件
com.aspose.words.Document doc = new com.aspose.words.Document(
wordFilePath);
doc.save(os, SaveFormat.PDF);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
response.reset();
response.setContentType("application/x-msdownload;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(pdf, "UTF-8"));
response.setContentType("application/pdf");
OutputStream servletOS = response.getOutputStream();
servletOS.write(bos.toByteArray());
servletOS.flush();
os.close();
fis.close();
bos.close();
servletOS.close();
file.delete();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void createPaymentWordNew(String seqno, String filePath,
HttpServletResponse response) {
try {
OlSysTemplateDefineFile olSysTemplateDefineFile = new
OlSysTemplateDefineFile();
Map wordData = new HashMap();
wordData = this.getPayment(seqno);
olSysTemplateDefineFile = olSysTemplateDefineFileDao
.selectFileByTemplateDesc(OlStatusCode.TEMPLATE_FILE_TYPE_NOTICE_FILE
.getCode());
byte[] buffer = olSysTemplateDefineFile.getTemplateFile();
String newFileName = (String) wordData.get("fileName");
String fileName = olSysTemplateDefineFile.getTemplateName()
+ ".docx";
File dir = new File(filePath + File.separator);
if (!dir.exists() && !dir.isDirectory()) {
dir.getParentFile().mkdirs();
}
File file = new File(filePath + File.separator + fileName);
if (!file.isDirectory()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write(buffer);
bos.close();
WordUtil wordUtil = new WordUtil();
XWPFDocument xwpfDocument = wordUtil.generateWord(wordData,
filePath + File.separator + fileName);
List tables = xwpfDocument.getTables();
List beforeRows = tables.get(0).getRows();
// 动态信息
List
public static XWPFDocument generateWord(Mapparam, String filePath) { XWPFDocument doc = null; try { OPCPackage pack = POIXMLDocument.openPackage(filePath); doc = new XWPFDocument(pack); if (param != null && param.size() > 0) { // 处理段落 String tempString = ""; Set runSet = new HashSet<>(); char lastChar = ' '; List paragraphList = doc.getParagraphs(); if (paragraphList != null && paragraphList.size() > 0) { for (XWPFParagraph paragraph : paragraphList) { List runs = paragraph.getRuns(); for (int o=0; o (); runSet.add(run); tempString = text; } else if (ch == '{') { if (lastChar == '$') { if (runSet.contains(run)) { } else { runSet.add(run); tempString = tempString + text; } } else { runSet = new HashSet<>(); tempString = ""; } } else if (ch == '}') { if (tempString != null && tempString.indexOf("${") >= 0) { if (runSet.contains(run)) { } else { runSet.add(run); tempString = tempString + text; } } else { runSet = new HashSet<>(); tempString = ""; } if (runSet.size() > 0) { String replaceText = replaceText( tempString, param); if (!replaceText.equals(tempString)) { int index = 0; XWPFRun aRun = null; for (XWPFRun tempRun : runSet) { tempRun.setText("", 0); if (index == 0) { aRun = tempRun; } index++; } if(replaceText.indexOf("n")>0) { String[] text1 = replaceText.split("n"); paragraph.removeRun(o); run = paragraph.insertNewRun(o); for(int f=0;f (); tempString = ""; } } else { if (runSet.size() <= 0) continue; if (runSet.contains(run)) continue; runSet.add(run); tempString = tempString + text; } lastChar = ch; } } } } List allTable = doc.getTables(); for (XWPFTable xwpfTable : allTable) { // 获取表格行数据 List rows = xwpfTable.getRows(); int x=0; for (int z=0;z cells = xwpfTableRow .getTableCells(); for (int k=0;k paragraphs = xwpfTableCell .getParagraphs(); for (XWPFParagraph xwpfParagraph : paragraphs) { // List iRuns=xwpfParagraph.getIRuns(); List runs = xwpfParagraph.getRuns(); for (int j=0; j (); runSet.add(run); tempString = text; } else if (ch == '{') { if (lastChar == '$') { if (runSet.contains(run)) { } else { runSet.add(run); tempString = tempString + text; } } else { runSet = new HashSet<>(); tempString = ""; } } else if (ch == '}') { if (tempString != null && tempString.indexOf("${") >= 0) { if (runSet.contains(run)) { } else { runSet.add(run); tempString = tempString + text; } } else { runSet = new HashSet<>(); tempString = ""; } if (runSet.size() > 0) { String replaceText = replaceText( tempString, param); if (!replaceText .equals(tempString)) { int index = 0; XWPFRun aRun = null; for (XWPFRun tempRun : runSet) { tempRun.setText("", 0); if (index == 0) { aRun = tempRun; } index++; } if(replaceText.indexOf("n")>0) { String[] text1 = replaceText.split("n"); xwpfParagraph.removeRun(j); run = xwpfParagraph.insertNewRun(j); for(int f=0;f (); tempString = ""; if(replaceText.indexOf("delete")!=-1){ delete=true; } } } else { if (runSet.size() <= 0) continue; if (runSet.contains(run)) continue; runSet.add(run); tempString = tempString + text; } lastChar = ch; } } } } if(delete){ xwpfTable.removeRow(z); z--; } } } } } catch (Exception e) { e.printStackTrace(); } return doc; }
public static void insertTable(XWPFDocument document, XWPFTable table, ListtableList, List daList, Integer type, Integer rows) { if (2 == type) { // 创建行和创建需要的列 for (int i = 0; i < daList.size(); i++) { // 添加一个新行 XWPFTableRow row = table.insertNewTableRow(rows); for (int k = 0; k < daList.get(0).length; k++) { XWPFTableCell createCell = row.createCell();// 根据String数组第一条数据的长度动态创建列 } } // 创建行,根据需要插入的数据添加新行,不处理表头 for (int i = 0; i < daList.size(); i++) { List cells = table.getRow(i + rows) .getTableCells(); for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell02 = cells.get(j); cell02.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER); setAlignCenter(cell02); cell02.setText(daList.get(i)[j]); } } } }
public static void mergeCellsHorizontal(XWPFTable table, int row,
int startCell, int endCell) {
for (int i = startCell; i <= endCell; i++) {
XWPFTableCell cell = table.getRow(row).getCell(i);
if (i == startCell) {
// The first merged cell is set with RESTART merge value
cell.getCTTc().addNewTcPr().addNewHMerge()
.setVal(STMerge.RESTART);
} else {
// Cells which join (merge) the first one, are set with CONTINUE
cell.getCTTc().addNewTcPr().addNewHMerge()
.setVal(STMerge.CONTINUE);
}
}
}
有问题可以留言,看到了会回.



