Build and download Word contracts based on Word templates (contract_templet.tld) and preview contract content based on wordhtml template (CONTRACT_TEMPLETHTML.TLD)

Source: Internet
Author: User

1. Action

String Templete=constantsappparams.contract_templet_doc;//contract_templet.tld
String Templetehtml=constantsappparams.contract_templet_html;//contract_templethtml.tld

P2pappinfo pai = (p2pappinfo) baselogic.getentitybypk (p2pappinfo.class,new Long (Request.getparameter ("PriNumber"))) ;

Map map = Freemarkerutils.convertbean (PAI);//Turn entity classes into maps

String flag = Request.getparameter ("flag");
if ("1". Equals (flag)) {//preview
freemarkerutils. Createwordfile (Request,map,templetehtml,date + pai.getloanname () + "loan contract. html", Pai.getloancontractno ());
StringBuffer path = new StringBuffer ();
String str = Request.getcontextpath ();
Path.append (str);
Path.append (File.separator);
Path.append (constantsappparams.contract__html);
Path.append (File.separator);
Path.append (Pai.getloancontractno ());
Path.append (File.separator);
Path.append (date + pai.getloanname () + "loan contract. html");
Response.setcharacterencoding ("Utf-8");
PrintWriter writer = Response.getwriter ();
Writer.write (Path.tostring ());
Writer.flush ();
Writer.close ();
} else{//download
Response.setcontenttype ("Application/msword");
Response.setheader ("Content-disposition", "Attachment;filename="
+ New String ("Loan Contract". GetBytes ("gb2312"), "iso8859-1")
+ ". Doc");
Response.set
Request.setcharacterencoding ("Utf-8");
Response.setcharacterencoding ("Utf-8");
Freemarkerutils.downloadword (Request, Response,
Map
Templete,
Date + pai.getloanname () + "loan contract");
}

2,freemarkerutils

public class Freemarkerutils {
/**
* Generate a preview of the contract
* @param request
* @param response
* @param dataMap
* @param templatename
* @param fileName
* @throws Exception
*/
public static void Downloadword (HttpServletRequest request,httpservletresponse response,map datamap,string Templatename,string fileName) throws exception{
Freemarkerconfigurationfactory fcf=new freemarkerconfigurationfactory ();
Configuration configuration = Fcf.createconfiguration ();
Fcf.setdefaultencoding ("Utf-8");
Configuration.setdefaultencoding ("Utf-8");
String path=request.getsession (). Getservletcontext (). Getrealpath ("");
Path=path+file.separator+constantsappparams.contract_templet_folder;
Configuration.setdirectoryfortemplateloading (The new File (path));
Template template = Configuration.gettemplate (templatename);
Template.setencoding ("Utf-8");
PrintWriter out = Response.getwriter ();

OutputStreamWriterwriter = new OutputStreamWriter (Response.getoutputstream (), "Utf-8 ");
Template.Process(Datamap,writer);
Writer.flush ();
Writer.close ();
}
/**
* Bean Transfer Map
* @param Bean
* @return
* @throws introspectionexception
* @throws illegalaccessexception
* @throws InvocationTargetException
*/
public static Map Convertbean (Object Bean)
Throws Introspectionexception, Illegalaccessexception, invocationtargetexception {
Class type = Bean.getclass ();
Map Returnmap = new HashMap ();
BeanInfo BeanInfo =Introspector. getbeaninfo (type);
PropertyDescriptor[] PropertyDescriptors = BeanInfo.getpropertydescriptors();
for (int i = 0; i< propertydescriptors.length; i++) {
PropertyDescriptorDescriptor = Propertydescriptors[i];
String propertyname = Descriptor.GetName();
if (!propertyname.equals ("class")) {
Method Readmethod = descriptor.Getreadmethod();
Object result = Readmethod.Invoke(Bean, new object[0]);
if (result! = null) {
Returnmap.put (propertyname, result);
} else {
Returnmap.put (PropertyName, "");
}
}
}
return returnmap;
}
/**
* Create a downloadable contract
* @param request
* @param dataMap
* @param templatename
* @param string
* @param string2
* @throws Exception
*/
public static void Createwordfile (HttpServletRequest request,map DataMap,
String templatename, String string, String Loancontractno) throws exception{
Freemarkerconfigurationfactory fcf=new freemarkerconfigurationfactory ();
Configuration configuration = Fcf.createconfiguration ();
Fcf.setdefaultencoding ("Utf-8");
Configuration.setdefaultencoding ("Utf-8");
String path=request.getsession (). Getservletcontext (). Getrealpath ("");
String filepath=path+file.separator+constantsappparams.contract__html;
Path=path+file.separator+constantsappparams.contract_templet_folder;
Configuration.setdirectoryfortemplateloading (The new File (path));
Template template = Configuration.gettemplate (templatename);
Template.setencoding ("Utf-8");
Filepath=filepath+file.separator+loancontractno;
File File=new file (FilePath);
if (!file.exists ()) {
File.mkdir ();
}
File File2=new file (FilePath, string);
Writerout = new BufferedWriter (newOutputStreamWriter(New FileOutputStream (File2), "UTF-8"));
Template.Process(Datamap,out);
Out.flush ();
Out.close ();
}
}

Build and download Word contracts based on Word templates (contract_templet.tld) and preview contract content based on wordhtml template (CONTRACT_TEMPLETHTML.TLD)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.