Java uses Freemaker and (excelxml table or Wordxml table) to export any document you want to format

Source: Internet
Author: User

Do the management system compared to love, but also like before with the HTML report, but also like before with the div fixed format, fixed fill data location,

1. Write Excel or Word

Like this: In the middle of the cell you can fill in some letters or Chinese, this is convenient to find its location, like me because my Java code encapsulates the map, I can go to the value

2. Save the file in the form of an XML table (. xml), and then throw the file in your action package, or under a different folder.

3. This time the file is still. xml format file, but we need the Freemaker file format, so you need to rename the suffix to change it to. FTL.

After the change, you can open it, remember the picture I wrote a lot of that El expression, looking for you to write these expressions in the template location: like this

4 This time our template is basically finished. Then the next step is to write the method and then take out the data you want to fill in the cell. If Word is similar, you don't have to use HTML to draw a div like you used to.

5 first of all, write a method of implementing the class

6 load the data into the template. Use the Freemaker list loop to populate the data where you want it, and if it's a single piece of data, it doesn't need a loop. (Just part of my Code)

@RequestMapping (value= "/downloadzcexcel.action")
public void Zcexcelimport (HttpServletRequest request, httpservletresponse response)
Throws Exception {
String Filename=null;
Documenthandler Handler=null;
String Tempbasepath=null;
String Tempname=null;
Request.setcharacterencoding ("Utf-8");
map<string, object> zcdata = new hashmap<string, object> ();
String sbzg=request.getparameter ("Sbzg");
String bbmc=request.getparameter ("BBMC");
String Year=request.getparameter ("year");
String exceltype=request.getparameter ("Exceltype");
String xkztype=request.getparameter ("Xkztype");
Bbmc=java.net.urldecoder.decode (BBMC, "UTF-8");//Parameter decoding
Sbzg=java.net.urldecoder.decode (SBZG, "UTF-8");
String pid=request.getparameter ("pid");
Exceltype=java.net.urldecoder.decode (Exceltype, "UTF-8"); Report type
If ("Jury voting table". Equals (Exceltype)) {
Xkztype=java.net.urldecoder.decode (Xkztype, "UTF-8");
Tempbasepath= "/com/daqsoft/titlemgmt/action";
Tempname= "ZCPWHTP.FTL";
Handler=new Documenthandler (Tempbasepath);
List<map> LIST=ZCEXCELSERVICE.GETEXCELBYPWH (SBZG, xkztype,year);
Zcdata.put ("Rows", list);
Zcdata.put ("BBMC", BBMC);
Zcdata.put ("Xkz", Xkztype);
Zcdata.put ("COUNT", List.size ());
}else{
if ("706". Equals (PID)) {
Tempbasepath= "/com/daqsoft/titlemgmt/action"; Qualification Review, Roster report (Turn series)
Tempname= "ZCEXCELBYZXL.FTL";
Handler=new Documenthandler (Tempbasepath);
List<map> List=zcexcelservice.getsbzgbyzc (sbzg,year);
Zcdata.put ("Rows", list);
Zcdata.put ("BBMC", BBMC);
Zcdata.put ("COUNT", List.size ());
}else{
Tempbasepath= "/com/daqsoft/titlemgmt/action"; Qualification Review, Roster report (normal)
Tempname= "ZCEXCEL.FTL";
Handler=new Documenthandler (Tempbasepath);
List<map> List=zcexcelservice.getsbzgbyzc (sbzg,year);
Zcdata.put ("Rows", list);
Zcdata.put ("BBMC", BBMC);
Zcdata.put ("COUNT", List.size ());
}
}
File file = null;
InputStream inputstream = null;
Servletoutputstream outservletoutputstream = null;
try {
File=handler.createdoc (Zcdata, FileName, Tempbasepath, tempname);
Inputstream=new fileinputstream (file);
Response.setcharacterencoding ("Utf-8");
Response.setcontenttype ("Application/msexcel");
Response.AddHeader ("Content-disposition", "Attachment;filename=" +urlencoder.encode (BBMC, "utf-8") + ". xls");
Outservletoutputstream=response.getoutputstream ();
byte[] buffer = new BYTE[512];
int bytestoread =-1;
while ((Bytestoread = inputstream.read (buffer))! =-1) {
Outservletoutputstream.write (buffer, 0, bytestoread);
}
} catch (Exception e) {
E.printstacktrace ();
}finally{

if (InputStream! = null)
Inputstream.close ();
if (outservletoutputstream! = null)
Outservletoutputstream.close ();
if (file! = null)
File.delete (); Delete temporary files
}
}

7 The following is the need to use a tool class, and the output of the data stream is the same

Public File Createdoc (map<string,object> datamap,string filename,string tempbasepath,string tempName) throws unsupportedencodingexception {
DataMap to fill in the Modeled data file
Set the modeled appliance method and path, Freemarker supports a variety of template loading methods. Can be re-servlet,classpath, database loading,
Here our templates are placed under the template package
Tempbasepath: '/com/daqsoft/hrmanage/action '
Tempname: ' GBDDTEMP.FTL '


Filename=filename!=null?filename: "Temp" + (int) (Math.random () * 100000);
Template T=null;
try {
TEST.FTL for the template to be loaded
t = configuration.gettemplate (tempname);
} catch (IOException e) {
E.printstacktrace ();
}
Output document path and name
File OutFile = new file (fileName);
Writer out = null;
try {
Out=new OutputStreamWriter (New FileOutputStream (OutFile), "Utf-8");
T.process (DataMap, out);
Out.close ();
} catch (Exception e) {
E.printstacktrace ();
}
System.out.println ("------------Complete file Encapsulation---------------");
return outFile;




}

Java uses Freemaker and (excelxml tables or Wordxml tables) to export any documents you want to format

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.