Detailed description of jsp export doc file instances by format, detailed description of jsp export doc

Source: Internet
Author: User

Detailed description of jsp export doc file instances by format, detailed description of jsp export doc

Detailed description of jsp export doc file instances by format

Principle:The doc file can be saved as an xml file. The xml file uses a string to represent the representation of the doc file, we only need to use Java to replace the content to be filled and then download it to the customer.

1. Fill in the data according to your document.
2. Save the document as an xml file, edit the xml file, and replace the filled content with a certain format. For example, replace the name with three replicas $ {name}
3. Read the file and replace $ {name} in the file with a real name.
4. download.

Next, let's look at the Code:

First, the conversion class

package com.my.util;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.Map;public class DowloadDocByTemplate {  public InputStream dowload(String templatePath,Map map) throws IOException{        File file = new File(templatePath);    StringBuffer sb = new StringBuffer();      BufferedReader br;        br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));        String s;        while ((s = br.readLine()) != null) {          sb.append(s);                  }        br.close();         s = sb.toString();          Iterator<String> keys = map.keySet().iterator();          while(keys.hasNext()) {            String key = (String) keys.next();            s=s.replace(key, (String) map.get(key));         }          return new ByteArrayInputStream(s.getBytes("UTF-8"));  }}  

Then there is action:

/*** Check the download permission * @ return */public String downLoadcheck () {return SUCCESS;} public String getTargetFileName () {return "zengguoqg.doc";} public InputStream getInputStream () {// ExportToWordUtil etw = new ExportToWordUtil (); StringBuffer carnumber = new StringBuffer ("<w: tr w: rsidR = '000000'> <w: trPr> <w: trHeight w: hRule = 'extract' w: val = '000000'/> </w: trPr> "); String s1 =" <w: tc> <w: tcPr> <w: tcW w: w = \ "236 \" w: type = \ "dxa \"/> </w: tcPr> <w: p w: rsidR = \ "00000000 \" w: rsidRDefault = \ "009F38EE \"> "+" <w: pPr> <w: rPr> <w: rFonts w: hint = \ "eastAsia \"/> </w: rPr> </w: pPr> <w: r> "+" <w: rPr> <w: rFonts w: hint = \ "eastAsia \"/> </w: rPr> <w: t> "; String number =" 450981199202073518 "; char [] ch = number. toCharArray (); for (int I = 0; I <ch. length; I ++) {carnumber. append (s1 ). append (ch [I]). append ("</w: t> </w: r> </w: p> </w: tc>");} carnumber. append ("</w: tr>"); Map <String, String> map = new HashMap (); map. put ("$ {name}", "Zeng Qiang"); map. put ("$ {sex}", "male"); map. put ("$ {writeDate}", "2014.1.2"); map. put ("$ {cardNumber}", "1100106"); map. put ("$ {personNumber}", carnumber. toString (); map. put ("$ {birthday}", "2014.1.2"); map. put ("$ {Education}", "Undergraduate"); map. put ("$ {marride}", "Unmarried"); map. put ("$ {comeFrom}", "Guangxi"); map. put ("$ {nation}", "Han"); map. put ("$ {ResidenceAddress}", "Guangxi"); map. put ("$ {PostalCode}", "543002"); map. put ("$ {QQ}", "1104566175"); map. put ("$ {job}", "software engineer"); map. put ("$ {politicalStatus}", "political outlook"); map. put ("$ {GraduateSchool}", ""); map. put ("$ {professional}", "Software Engineering"); map. put ("$ {beginWorkTime}", "2014.1.2"); map. put ("$ {bankCarNumber}", "622709"); map. put ("$ {emergencyContactPerson}", "Zeng Qiang"); map. put ("$ {relationship}", "Students"); map. put ("$ {ummoveTelephone}", "0774133"); map. put ("$ {mobilePhone}", "1350335"); map. put ("$ {adreess}", "Guangxi"); map. put ("$ {mainExperience}", "No job"); map. put ("$ {favoriteAndGoodAt}", "basketball"); String path = ServletActionContext. getServletContext (). getRealPath ("/fileTemplate/renyuandengjibiao2.xml"); System. out. println (path); // return etw. export (path, "test. xml ", map," out.doc "); try {return new DowloadDocByTemplate (). dowload (path, map);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace (); return null ;}}}

Finally, download the Strut Configuration:

<Package name = "dowload" extends = "struts-default"> <action name = "dowload" class = "zhaopingAction" method = "downLoadcheck"> <result name = "success" type = "stream"> <param name = "contentType"> application/ZIP </param> <! -- The default value is text/plain --> <param name = "inputName"> inputStream </param> <param name = "contentDisposition"> filename = "$ {targetFileName}" </param & gt; <param name = "bufferSize"> 4096 </param> <! -- Buffer size during output --> </result> </action>

That's all. Xml files are stored in WebRoot/fileTemplate/renyuandengjibiao2.xml.

If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article and hope to help you. Thank you for your support for this site!

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.