Wordutil Java Export Word tool class

Source: Internet
Author: User
Tags getmessage

Import Java.io.BufferedInputStream;
Import Java.io.BufferedOutputStream;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import Java.io.Writer;
Import Java.net.URLEncoder;
Import Java.util.Map;
Import Javax.servlet.http.HttpServletResponse;
Import Org.apache.log4j.Logger;
Import freemarker.template.Configuration;
Import Freemarker.template.Template;
/**
* @Desc: Word Manipulation tool class
*
* */
public class Wordutil {
private static Logger log = Logger.getlogger (Wordutil.class);
/**
* @Desc: Generate Word file
* @paramdataMap The Dynamic Data you need to show in Word, save it with the map collection
* @paramtemplateName Word template name, for example: TEST.FTL
* @paramfilePath the target path generated by the file, for example: d:/wordfile/
* @paramfileName generated file name, for example: Test.doc
* */
public static void Createword (Map<string, object>datamap,string templatename,string filepath,string fileName) {
try {
Creating a Configuration instance
Configuration configuration = new configuration ();
Set encoding
Configuration.setdefaultencoding ("UTF-8");
FTL template file
File File = new file (FilePath);
configuration.setdirectoryfortemplateloading (file);
Get template
Template template = Configuration.gettemplate (templatename);
Output file
File OutFile = new file (FilePath + file.separator + fileName);
If the output destination folder does not exist, create
if (!outfile.getparentfile (). exists ()) {
Outfile.getparentfile (). Mkdirs ();
}
Merging templates and data models to generate files
Writer out = new BufferedWriter (new OutputStreamWriter (New FileOutputStream (OutFile), "UTF-8"));
Generating files
Template.process (DataMap, out);
Close the stream
Out.flush ();
Out.close ();
} catch (Exception e) {
Log.error ("Error generating Word document (Wordutil):" Msg: "+e.getmessage () +" ", File name:" + filename ");
E.printstacktrace ();
}
}
/** File Download
* @param path full path of path file, including file name
* @param response
* @return
* */
public static HttpServletResponse Downfile (String path, httpservletresponse response) {
try {
Path refers to the paths of the files you want to download.
File File = new file (path);
Gets the file name.
String filename = File.getname ();
Download the file in the form of a stream.
InputStream fis = new Bufferedinputstream (new FileInputStream (file));
byte[] buffer = new byte[fis.available ()];
Fis.read (buffer);
Fis.close ();
Empty response
Response.reset ();
Set the header of the response
String filename = urlencoder.encode (filename, "UTF-8");
if (Filename.length () >150) {//fix IE 6.0 bug
Filename=new String (filename.getbytes ("GBK"), "iso-8859-1"); }
Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);
Response.AddHeader ("Content-length", "" "+ File.length ());
OutputStream outs = new Bufferedoutputstream (Response.getoutputstream ());
Response.setcontenttype ("Application/octet-stream");
Outs.write (buffer);
Outs.flush ();
Outs.close ();
File.delete ();
}catch (IOException e) {
Log.error ("Download document (Wordutil) Error:" Msg: "+e.getmessage () +" ""); E.printstacktrace (); }
return response;
}
}

Wordutil Java Export Word tool class

Related Article

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.