将值从java变量传递到microsoft word(doc和docx)变量
如何用java变量的值替换microsoft word文档变量值?我有一个.doc或.docx文件模板,我在其中定义了一些变量将值从java变量传递到microsoft word(doc和docx)变量,java,ms-word,docx,doc,Java,Ms Word,Docx,Doc,如何用java变量的值替换microsoft word文档变量值?我有一个.doc或.docx文件模板,我在其中定义了一些变量 当用户从我的应用程序中单击下载按钮时,.doc或.docx变量必须从java变量中获取值 我负责一出戏!使用扩展名为.docx的模板生成word文档的intranet。为了实现这一点,我们有以下继承树:Document>Word>[someDocument] 抽象类Word处理替换Word文档中的变量 public abstract class Word extends
当用户从我的应用程序中单击下载按钮时,.doc或.docx变量必须从java变量中获取值 我负责一出戏!使用扩展名为.docx的模板生成word文档的intranet。为了实现这一点,我们有以下继承树:Document>Word>[someDocument] 抽象类Word处理替换Word文档中的变量
public abstract class Word extends Document {
public static JAXBContext context = org.docx4j.jaxb.Context.jc;
public Word(String inputfilepath){
super(inputfilepath);
}
public String generer(String outputfilepath) throws Exception {
//String inputfilepath = System.getProperty("user.dir")+"/app/doc/adhesionTemplate.docx";
//String outputfilepath = System.getProperty("user.dir")+ "/test-out.docx";
// Open a document from the file system
// 1. Load the Package
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
// 2. Fetch the document part
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document) documentPart.getJaxbElement();
// xml --> string
String xml = XmlUtils.marshaltoString(wmlDocumentEl, true);
//Change the variables using an abstract function getMapping()
HashMap<String, String> mappings = getMapping();
Object obj = XmlUtils.unmarshallFromTemplate(xml, mappings);
// change JaxbElement
documentPart.setJaxbElement((org.docx4j.wml.Document) obj);
//Footers :
List<SectionWrapper> wrappers = wordMLPackage.getDocumentModel().getSections();
for (SectionWrapper sw : wrappers) {
FooterPart footer = sw.getHeaderFooterPolicy().getDefaultFooter();
if (footer != null) {
Ftr footerDoc = footer.getJaxbElement();
String footerXml = XmlUtils.marshaltoString(footerDoc, true);
Object footerObj = XmlUtils.unmarshallFromTemplate(footerXml, mappings);
footer.setJaxbElement( (Ftr) footerObj);
}
}
// Save it
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
Console.commande("sudo chmod 660 \"" + outputfilepath + "\"");
System.out.println("Saved output to:" + outputfilepath);
return outputfilepath;
}
公共抽象类Word扩展文档{
公共静态JAXBContext context=org.docx4j.jaxb.context.jc;
公共字(字符串输入文件路径){
超级(输入文件路径);
}
公共字符串生成器(字符串输出文件路径)引发异常{
//字符串inputfilepath=System.getProperty(“user.dir”)+“/app/doc/adhesionTemplate.docx”;
//字符串outputfilepath=System.getProperty(“user.dir”)+“/test out.docx”;
//从文件系统中打开文档
//1.加载包
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage.load(新java.io.File(inputfilepath));
//2.取文件部分
MainDocumentPart documentPart=wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl=(org.docx4j.wml.Document)documentPart.getJaxbElement();
//xml-->字符串
字符串xml=XmlUtils.marshaltoString(wmlDocumentEl,true);
//使用抽象函数getMapping()更改变量
HashMappings=getMapping();
Object obj=XmlUtils.unmarshallFromTemplate(xml,映射);
//换皮带
documentPart.setJaxbElement((org.docx4j.wml.Document)obj);
//页脚:
List wrappers=wordMLPackage.getDocumentModel().getSections();
用于(SectionWrapper sw:wrappers){
FooterPart footer=sw.getHeaderFooterPolicy().getDefaultFooter();
如果(页脚!=null){
Ftr footerDoc=footer.getJaxbElement();
字符串footerXml=XmlUtils.marshallToString(footerDoc,true);
对象footerObj=XmlUtils.unmarshallFromTemplate(footerXml,映射);
footer.setJaxbElement((Ftr)footerObj);
}
}
//省省吧
SaveToZipFile saver=新的SaveToZipFile(wordMLPackage);
saver.save(outputfilepath);
command(“sudo chmod 660\”“+outputfilepath+”\”);
System.out.println(“将输出保存到:“+outputfilepath”);
返回outputfilepath;
}
然后,我们有从这个单词抽象类继承的类:
public class FAC extends Word {
public FAC() {
super(System.getProperty("user.dir") + "/templates/Facture.docx");
}
@Override
public HashMap<String, String> getMapping() {
//Preparing variables
int price = blablabla;
HashMap<String, String> map = new HashMap<String, String>();
map.put("FACDate", Utils.dateConvert(new Date()));
map.put("somePrice", String.valueOf(price));
return map;
}
}
公共类FAC扩展Word{
公共外交事务委员会(){
super(System.getProperty(“user.dir”)+“/templates/Facture.docx”);
}
@凌驾
公共HashMap getMapping(){
//准备变量
整数价格=布拉布拉布拉;
HashMap=newHashMap();
map.put(“FACDate”,Utils.dateConvert(newdate());
map.put(“somePrice”,String.valueOf(price));
返回图;
}
}
注意:“Document”超类没有什么特殊的,只有一个变量“inputFilePath”和抽象方法getMapping()
希望这些帮助,无论是您还是像我这样的未来观众:p我使用docx4j就是为了这个目的:
String inputfilepath = "binding-simple1.docx";
String outputfilepath = "OUT_VariableReplace.docx";
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
.load(new java.io.File(inputfilepath));
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
HashMap<String, String> mappings = new HashMap<String, String>();
mappings.put("subjectId", "E000001");
// Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
documentPart.variableReplace(mappings);
Docx4J.save(wordMLPackage, new File(outputfilepath));
String inputfilepath=“binding-simple1.docx”;
字符串outputfilepath=“OUT\u variableplace.docx”;
WordprocessingMLPackage wordMLPackage=WordprocessingMLPackage
.load(新的java.io.File(inputfilepath));
MainDocumentPart documentPart=wordMLPackage.getMainDocumentPart();
HashMappings=新的HashMap();
映射。put(“主体”、“E000001”);
//方法1(从3.0.0开始;如果尚未导致解组,则速度更快):
documentPart.variableReplace(映射);
保存(wordMLPackage,新文件(outputfilepath));
变量参数如下:
${subjectId}
你想要的是一个库吗?你可以从docx中查看他们的docx4j替换文本,我的类必须替换你看到的定义的doc或docx变量中的值?但是它不适用于.doc文件。我认为不存在一个同时具有所需功能的doc和docx的库。如果你不懂语言c、 您可以查看哪些操作与您的操作完全相同,并且可以在命令行中使用: