1、下载文件aspose-words-21.1.0-jdk17-crack.jar
2、在spring boot 项目根目录下建立lib文件夹,放进去,放在resource文件夹下无效。
3、在pom文件中添加依赖
com.aspose aspose-words21.1 jar system ${project.basedir}/lib/aspose-words-21.1.0-jdk17-crack.jar
刷新maven,项目里面就可以引用了。后面附上文件
WriteWordUtil.java
package com.ruoyi.kaopingservice.service.util;
import com.aspose.words.document;
import com.aspose.words.SaveFormat;
import com.deepoove.poi.XWPFTemplate;
import org.apache.poi.xwpf.usermodel.XWPFdocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WriteWordUtil {
public void writeDocx(String path, Map map) throws Exception {
InputStream is = new FileInputStream(path);
XWPFdocument doc = new XWPFdocument(is);
}
private void replaceInPara(XWPFdocument doc, Map params) {
Iterator iterator = doc.getParagraphsIterator();
XWPFParagraph para;
while (iterator.hasNext()) {
para = iterator.next();
this.replaceInPara(para, params);
}
}
private void replaceInPara(XWPFParagraph para, Map params) {
List runs;
Matcher matcher;
if (this.matcher(para.getParagraphText()).find()) {
runs = para.getRuns();
for (int i = 0; i < runs.size(); i++) {
XWPFRun run = runs.get(i);
String runText = run.toString();
matcher = this.matcher(runText);
if (matcher.find()) {
while ((matcher = this.matcher(runText)).find()) {
runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
}
// 直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
// 所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
para.removeRun(i);
if (runText.equals("null")) {
runText = "";
}
para.insertNewRun(i).setText(runText);
}
}
}
}
private void replaceInTable(XWPFdocument doc, Map params) {
Iterator iterator = doc.getTablesIterator();
XWPFTable table;
List rows;
List cells;
List paras;
while (iterator.hasNext()) {
table = iterator.next();
rows = table.getRows();
for (XWPFTableRow row : rows) {
cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
String cellTextString = cell.getText();
for (Map.Entry e : params.entrySet()) {
if (cellTextString.contains("${" + e.getKey() + "}")) {
cellTextString = cellTextString.replace("${" + e.getKey() + "}", e.getValue().toString());
}
}
cell.removeParagraph(0);
if (cellTextString.contains("${") && cellTextString.contains("}")) {
cellTextString = "";
}
cell.setText(cellTextString);
// paras = cell.getParagraphs();
// for (XWPFParagraph para : paras) {
// this.replaceInPara(para, params);
// }
}
}
}
}
private Matcher matcher(String str) {
Pattern pattern = Pattern.compile("\$\{(.+?)\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(str);
return matcher;
}
private void close(InputStream is) {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void close(OutputStream os) {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String templateWrite(String filePath, Map params, String outFilePath) throws Exception {
InputStream is = new FileInputStream(filePath);
WriteWordUtil writeWordUtil = new WriteWordUtil();
XWPFdocument doc = new XWPFdocument(is);
// 替换段落里面的变量
writeWordUtil.replaceInPara(doc, params);
// 替换表格里面的变量
writeWordUtil.replaceInTable(doc, params);
OutputStream os = new FileOutputStream(outFilePath);
doc.write(os);
writeWordUtil.close(os);
writeWordUtil.close(is);
os.flush();
os.close();
return "";
}
public static void templateWrite2(String filePath, Map params, String outFilePath) throws Exception {
XWPFTemplate template = XWPFTemplate.compile(filePath).render(params);
FileOutputStream out = new FileOutputStream(outFilePath);
template.write(out);
out.flush();
out.close();
template.close();
}
public static boolean getLicense() throws Exception {
boolean result = false;
try {
// File file=new File("License.xml");
// String filePath=file.getAbsolutePath();
// System.out.println("注册文件地址:"+filePath);
// FileInputStream fin = new FileInputStream(filePath);
// InputStream is=(InputStream)fin;
// //InputStream is=fin;
// InputStream is =(new com.aspose.words.document()).getClass().getResourceAsStream("/com.aspose.words.lic_2999.xml");
System.out.println("注册文件地址:"+(new com.aspose.words.document()).getClass().getName());
// License aposeLic = new License();
// aposeLic.setLicense(is);
//aposeLic.setLicense(fin);
result = true;
// is.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
// result = true;
return result;
}
public static void doc2pdf(String inPath, String outPath) throws Exception {
System.out.println("doc2pdf-->inPath-->" + inPath);
System.out.println("doc2pdf-->outPath-->" + outPath);
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
throw new Exception("com.aspose.words lic ERROR!");
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
document doc = new document(inPath); // word文档
// 支持RTF HTML,Opendocument, PDF,EPUB, XPS转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Img2pdf(String inPath, String outPath) throws Exception {
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档有水印
throw new Exception("com.aspose.words lic ERROR!");
}
try {
long old = System.currentTimeMillis();
File file = new File(outPath);
FileOutputStream os = new FileOutputStream(file);
document doc = new document(inPath); // word文档
// 支持RTF HTML,Opendocument, PDF,EPUB, XPS转换
doc.save(os, SaveFormat.PDF);
long now = System.currentTimeMillis();
os.close();
System.out.println("convert OK! " + ((now - old) / 1000.0) + "秒");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main() {
//测试一下转换
try {
String docPath = "G:\doc\lanxin\20210710\demo.docx";
String pthPath = "G:\doc\lanxin\20210710\demo.pdf";
WriteWordUtil.doc2pdf(docPath, pthPath);
}catch(Exception ex){
ex.printStackTrace();
}
}
}



