Java generates Word document "two"

Source: Internet
Author: User

The/** * <p>expwordbyitext method is primarily used to export documents from Itext to word.</p> * <p> pros and cons-you can create Word formats with discretionary control, but code control is slightly more complex than using templates Other document properties such as header footer can be set .</p> * <p> third-party Jar-itext-2.1.7.jar, itext-rtf-2.1.7.jar.<br> * Import com.lowagie.text.document;<br> * Import com.lowagie.text.documentexception;<br> * Import com.lowagie.text.element;<br> * Import com.lowagie.text.headerfooter;<br> * Import com.lowagie.text.paragraph;<br> * Import com.lowagie.text.phrase;<br> * Import com.lowagie.text.rectangle;<br> * Import Com.lowagie.text.rtf.RtfWriter2; * </p> * <p> * Mountains and Rivers Dream Jul, 2015-5:07:55 PM * </p> * @param content * @param filePath * @throws Fileno  Tfoundexception * @throws documentexception */private static void Expwordbyitext (string content, String FilePath) throws FileNotFoundException, documentexception {document doc = new Document (PAGESIZE.A4); Rtfwriter2.getinstance (Doc, New FileOutputStream (FilePath));d Oc.open (); Paragraph p = new PaRagraph (content);d Oc.add (p);d oc.close ();} 

 

/** * <p> * Dowmload method is mainly used for-download. * </p> */public String dowmload (httpservletrequest request, httpservletresponse response) throws IOException { String fileName = "Export test. doc"; String FullPath = "d:/" + fileName; File File = new file (fullPath); InputStream in = new Bufferedinputstream (new FileInputStream (FullPath));// ContentType See Http://tool.oschina.net/commonsresponse.setContentType ("Application/octet-stream"); fileName = new String (Filename.replaceall ("", ""). GetBytes ("gb2312"), "iso8859-1"), Response.AddHeader ("Content-disposition", " Attachment;filename= "+ filename"; Response.AddHeader ("Content-length", "" "+ File.length ());//mode one outputstream out = new Bufferedoutputstream (Response.getoutputstream ()); byte[] buffer = new byte[in.available ()];in.read (buffer); O Ut.write (buffer); In.close (); Out.flush (); Out.close ();//mode two string line = Null;outputstreamwriter writer = new OutputStreamWriter (Response.getoutputstream (), "UTF-8"); BufferedReader reader = new BufferedReader (New InputstreamreadER (in)), while (line = Reader.readline ())! = null) {Writer.write (line); Writer.write ("\ r \ n");} In.close (); Writer.flush (); Writer.close (); return "Download";}

Java generates Word document "two"

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.