存放地点变更生成出门证
This commit is contained in:
@@ -0,0 +1,101 @@
|
|||||||
|
package com.yxt.common.base.utils;
|
||||||
|
|
||||||
|
import com.jacob.activeX.ActiveXComponent;
|
||||||
|
import com.jacob.com.ComThread;
|
||||||
|
import com.jacob.com.Dispatch;
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.Version;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author dimengzhe
|
||||||
|
* @Date 2022/10/18 17:11
|
||||||
|
* @Description
|
||||||
|
*/
|
||||||
|
public class WordConvertUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据ftl模板生成word
|
||||||
|
*
|
||||||
|
* @param map 数据
|
||||||
|
* @param typeName 模板名称
|
||||||
|
* @param sourcePath 模板路径
|
||||||
|
* @param targetPath 保存目标路径
|
||||||
|
* @param fileName 文件名
|
||||||
|
*/
|
||||||
|
public static void creatWord(Map<String, Object> map, File file, String targetPath, String fileName, String dir) {
|
||||||
|
String curDate = DateUtils.dateConvertStr(new Date(), "yyyy年MM月dd日");
|
||||||
|
try {
|
||||||
|
//Configuration 用于读取ftl文件
|
||||||
|
Configuration configuration = new Configuration(new Version("2.3.0"));
|
||||||
|
configuration.setDefaultEncoding("utf-8");
|
||||||
|
//指定路径的第二种方式
|
||||||
|
configuration.setDirectoryForTemplateLoading(new File(dir));
|
||||||
|
//输出文档路径及名称
|
||||||
|
File targetFile = new File(targetPath);
|
||||||
|
if (!targetFile.exists()) {
|
||||||
|
targetFile.mkdirs();
|
||||||
|
}
|
||||||
|
targetPath = targetPath + fileName;
|
||||||
|
File outFile = new File(targetPath);
|
||||||
|
//以utf-8的编码读取ftl文件
|
||||||
|
Template template = configuration.getTemplate(file.getName(), "utf-8");
|
||||||
|
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
|
||||||
|
template.process(map, out);
|
||||||
|
out.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* word转换为pdf
|
||||||
|
*
|
||||||
|
* @param wordFile word的路径
|
||||||
|
* @param pdfPath pdf的路径
|
||||||
|
* @param pdfName pdf名称
|
||||||
|
*/
|
||||||
|
public static void doc2pdf(String wordFile, String pdfPath, String pdfName) {
|
||||||
|
ActiveXComponent app = null;
|
||||||
|
System.out.println("开始转换...");
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
Dispatch document = null;
|
||||||
|
try {
|
||||||
|
ComThread.InitSTA();
|
||||||
|
// 打开word
|
||||||
|
app = new ActiveXComponent("Word.Application");
|
||||||
|
// 获得word中所有打开的文档
|
||||||
|
Dispatch documents = app.getProperty("Documents").toDispatch();
|
||||||
|
// 打开文档
|
||||||
|
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch();
|
||||||
|
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
||||||
|
File targetFile = new File(pdfPath);
|
||||||
|
if (!targetFile.exists()) {
|
||||||
|
targetFile.mkdirs();
|
||||||
|
}
|
||||||
|
String pdfFile = pdfPath + pdfName;
|
||||||
|
File target = new File(pdfFile);
|
||||||
|
if (target.exists()) {
|
||||||
|
target.delete();
|
||||||
|
}
|
||||||
|
Dispatch.call(document, "SaveAs", pdfFile, 17);
|
||||||
|
long end = System.currentTimeMillis();
|
||||||
|
System.out.println("转换完成..用时:" + (end - start) + "ms.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println("转换失败" + e.getMessage());
|
||||||
|
} finally {
|
||||||
|
// 关闭文档
|
||||||
|
Dispatch.call(document, "Close", false);
|
||||||
|
// 关闭office
|
||||||
|
app.invoke("Quit", 0);
|
||||||
|
System.out.println("关闭文档");
|
||||||
|
// 如果没有这句话,winword.exe进程将不会关闭
|
||||||
|
ComThread.Release();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user