This is the tool class code
Package common.util;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import Java.util.Map;
Import Sun.misc.BASE64Encoder;
Import freemarker.template.Configuration;
Import Freemarker.template.Template;
Import freemarker.template.TemplateException;
Import Freemarker.template.TemplateExceptionHandler;
public class Docutil {
Private Configuration Configure=null;
Public Docutil () {
Configure=new Configuration ();
Configure.setdefaultencoding ("Utf-8");
}
/**
* Generate Word files based on doc template
* @param dataMap need to fill in the template data
* @param downloadtype file name
* @param savepath Save path
*/
public void Createdoc (map<string,object> datamap,string downloadtype,string savepath) {
try {
Load a template that needs filling
Template Template=null;
Set the template appliance method and path, Freemarker supports a variety of template loading methods. Can be re-servlet,classpath, database loading.
Load the template file and place it under Testdoc
Configure.setclassfortemplateloading (This.getclass (), "Testdoc");
Set Object Wrapper
Configure.setobjectwrapper (New Defaultobjectwrapper ());
Setting the exception handler
Configure.settemplateexceptionhandler (Templateexceptionhandler.ignore_handler);
Define template object, note that the type name is identical to Downloadtype
Template=configure.gettemplate (downloadtype+ ". xml");
File Outfile=new file (Savepath);
Writer Out=null;
Out=new BufferedWriter (New OutputStreamWriter (New FileOutputStream (OutFile), "Utf-8"));
Template.process (DataMap, out);
Out.close ();
} catch (IOException e) {
E.printstacktrace ();
} catch (Templateexception e) {
E.printstacktrace ();
}
}
Here the template path is
public string Getimagestr (string imgfile) {
InputStream In=null;
Byte[] Data=null;
try {
In=new FileInputStream (Imgfile);
Data=new byte[in.available ()];
In.read (data);
In.close ();
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
Base64encoder encoder=new Base64encoder ();
return Encoder.encode (data);
}
}
Method invocation
@Override
public string Exportword (Teachmaterialdealinfo teachmaterialdealinfo,
String titlename) {
String s = Uuid.randomuuid (). toString ();
String filepath = Servletactioncontext.getservletcontext (). Getrealpath ("upload/notice/" +s+ "/");
String Zippath = Servletactioncontext.getservletcontext (). Getrealpath ("upload/notice/" +s+ "/week-job.zip");
String notice = Servletactioncontext.getservletcontext (). Getrealpath ("upload/notice/");
CreateFile (notice);
CreateFile (filepath);
List<teachmaterialdealinfo> teachmaterial = Teachmaterialdealinfodao.finddealinfo (TeachMaterialDealInfo );
Teachmaterial teachMaterial1 = Applicationdao.getmaterinfobymaterid (Teachmaterialdealinfo.getmaterid ());
Docutil Docutil=null;
Map<string, object> datamap=null;
.... Assigning value to datamap such as Datamap.put ("Writername", "ha hehe"); Calling an XML file is the way to get something worthwhile here I'm using the Freemarker method
Docutil.createdoc (DataMap, "BaseDoc", filepath+ "/" +teachmaterial.get (i). Getwritername () + ". Doc");
}
XML file is dumped as XML after Word file is first written.
Java generates Word documents