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"