Java generates Word documents

Source: Internet
Author: User

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

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.