人员简历导出下载(20230105)
parent
06da61d889
commit
5450b99e37
@ -1,84 +0,0 @@
|
||||
package com.engine.organization.util;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author
|
||||
* @date
|
||||
* @apiNote 生成动态文档帮助类
|
||||
*/
|
||||
@Data
|
||||
public class WordUtil {
|
||||
private Configuration configuration = null;
|
||||
|
||||
/*
|
||||
* 模板文件存放的目录
|
||||
*/
|
||||
private String baseDir;
|
||||
|
||||
/*
|
||||
* 模板文件名称
|
||||
*/
|
||||
private String templateFile;
|
||||
|
||||
/*
|
||||
* word生成的输出目录
|
||||
*/
|
||||
private String outputDir;
|
||||
|
||||
public WordUtil(){
|
||||
configuration = new Configuration();
|
||||
configuration.setDefaultEncoding("utf-8");
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* <p>转换成word<br>
|
||||
*/
|
||||
public String createWord(Map<String,Object> dataMap){
|
||||
|
||||
configuration.setClassForTemplateLoading(this.getClass(), "");//模板文件所在路径
|
||||
|
||||
Template t = null;
|
||||
|
||||
try {
|
||||
//得到模板文件
|
||||
configuration.setDirectoryForTemplateLoading(new File(baseDir));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
t = configuration.getTemplate(templateFile,"utf-8");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// GwUtil.getFileNo(""); 调用生成随机数的方法
|
||||
File outFile = new File(outputDir + "222.doc"); //导出文件
|
||||
|
||||
Writer out = null;
|
||||
try {
|
||||
try {
|
||||
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (FileNotFoundException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
try {
|
||||
t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件
|
||||
return outFile.getPath();
|
||||
} catch (TemplateException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "文档生成失败";
|
||||
}
|
||||
}
|
@ -0,0 +1,144 @@
|
||||
package com.engine.organization.util.word;
|
||||
|
||||
import org.apache.poi.openxml4j.opc.OPCPackage;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
||||
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
|
||||
import org.apache.xmlbeans.XmlException;
|
||||
import org.apache.xmlbeans.XmlToken;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
|
||||
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class CustomXWPFDocument extends XWPFDocument {
|
||||
|
||||
public CustomXWPFDocument(InputStream in) throws IOException {
|
||||
super(in);
|
||||
}
|
||||
|
||||
public CustomXWPFDocument() {
|
||||
super();
|
||||
}
|
||||
|
||||
public CustomXWPFDocument(OPCPackage pkg) throws IOException {
|
||||
super(pkg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @param width 宽
|
||||
* @param height 高
|
||||
* @param paragraph 段落
|
||||
*/
|
||||
public void createPicture(String blipId,int id, int width, int height, XWPFParagraph paragraph, CustomXWPFDocument doc) {
|
||||
final int EMU = 9525;
|
||||
width *= EMU;
|
||||
height *= EMU;
|
||||
CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
|
||||
String picXml = ""
|
||||
+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
|
||||
+ " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
|
||||
+ " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
|
||||
+ " <pic:nvPicPr>" + " <pic:cNvPr id=\""
|
||||
+ id
|
||||
+ "\" name=\"Generated\"/>"
|
||||
+ " <pic:cNvPicPr/>"
|
||||
+ " </pic:nvPicPr>"
|
||||
+ " <pic:blipFill>"
|
||||
+ " <a:blip r:embed=\""
|
||||
+ blipId
|
||||
+ "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
|
||||
+ " <a:stretch>"
|
||||
+ " <a:fillRect/>"
|
||||
+ " </a:stretch>"
|
||||
+ " </pic:blipFill>"
|
||||
+ " <pic:spPr>"
|
||||
+ " <a:xfrm>"
|
||||
+ " <a:off x=\"0\" y=\"0\"/>"
|
||||
+ " <a:ext cx=\""
|
||||
+ width
|
||||
+ "\" cy=\""
|
||||
+ height
|
||||
+ "\"/>"
|
||||
+ " </a:xfrm>"
|
||||
+ " <a:prstGeom prst=\"rect\">"
|
||||
+ " <a:avLst/>"
|
||||
+ " </a:prstGeom>"
|
||||
+ " </pic:spPr>"
|
||||
+ " </pic:pic>"
|
||||
+ " </a:graphicData>" + "</a:graphic>";
|
||||
|
||||
inline.addNewGraphic().addNewGraphicData();
|
||||
XmlToken xmlToken = null;
|
||||
try {
|
||||
xmlToken = XmlToken.Factory.parse(picXml);
|
||||
} catch (XmlException xe) {
|
||||
xe.printStackTrace();
|
||||
}
|
||||
inline.set(xmlToken);
|
||||
|
||||
CTPositiveSize2D extent = inline.addNewExtent();
|
||||
extent.setCx(width);
|
||||
extent.setCy(height);
|
||||
|
||||
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
|
||||
docPr.setId(id);
|
||||
docPr.setName("图片" + blipId);
|
||||
docPr.setDescr("头像");
|
||||
}
|
||||
|
||||
public void createPicture(String blipId, int id, int width, int height, XWPFParagraph paragraph) {
|
||||
final int EMU = 9525;
|
||||
width *= EMU;
|
||||
height *= EMU;
|
||||
//String blipId = getAllPictures().get(id).getPackageRelationship().getId();
|
||||
CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
|
||||
|
||||
String picXml = "" +
|
||||
"<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">" +
|
||||
" <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
|
||||
" <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">" +
|
||||
" <pic:nvPicPr>" +
|
||||
" <pic:cNvPr id=\"" + id + "\" name=\"Generated\"/>" +
|
||||
" <pic:cNvPicPr/>" +
|
||||
" </pic:nvPicPr>" +
|
||||
" <pic:blipFill>" +
|
||||
" <a:blip r:embed=\"" + blipId + "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>" +
|
||||
" <a:stretch>" +
|
||||
" <a:fillRect/>" +
|
||||
" </a:stretch>" +
|
||||
" </pic:blipFill>" +
|
||||
" <pic:spPr>" +
|
||||
" <a:xfrm>" +
|
||||
" <a:off x=\"0\" y=\"0\"/>" +
|
||||
" <a:ext cx=\"" + width + "\" cy=\"" + height + "\"/>" +
|
||||
" </a:xfrm>" +
|
||||
" <a:prstGeom prst=\"rect\">" +
|
||||
" <a:avLst/>" +
|
||||
" </a:prstGeom>" +
|
||||
" </pic:spPr>" +
|
||||
" </pic:pic>" +
|
||||
" </a:graphicData>" +
|
||||
"</a:graphic>";
|
||||
|
||||
inline.addNewGraphic().addNewGraphicData();
|
||||
XmlToken xmlToken = null;
|
||||
try {
|
||||
xmlToken = XmlToken.Factory.parse(picXml);
|
||||
} catch (XmlException xe) {
|
||||
xe.printStackTrace();
|
||||
}
|
||||
inline.set(xmlToken);
|
||||
|
||||
CTPositiveSize2D extent = inline.addNewExtent();
|
||||
extent.setCx(width);
|
||||
extent.setCy(height);
|
||||
|
||||
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
|
||||
docPr.setId(id);
|
||||
docPr.setName("图片" + id);
|
||||
docPr.setDescr("头像");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue