itext-rtf-2.1.7.jar,:http://download.csdn.net/detail/xuxu198899223/7717727
itext-2.1.7.jar:http://download.csdn.net/detail/xuxu198899223/7717717
Package Word;import Java.io.fileoutputstream;import Java.io.ioexception;import java.util.arraylist;import Java.util.list;import Com.lowagie.text.document;import Com.lowagie.text.documentexception;import Com.lowagie.text.element;import Com.lowagie.text.font;import Com.lowagie.text.pagesize;import Com.lowagie.text.paragraph;import Com.lowagie.text.pdf.basefont;import Com.lowagie.text.rtf.rtfwriter2;public Class Wordutil {private static document document; private static Basefont Basefont; /** * Create word and set the paper document * @param filePath document PATH * @throws documentexception * @throws IOException * * private static void Openwordfile (String filePath) throws Documentexception, IOException {document = new document ( PAGESIZE.A4); Rtfwriter2.getinstance (document, New FileOutputStream (FilePath)); Document.open (); Basefont = Basefont.createfont (); }/** * Set title * @param title * @return * @throws documentexception */PRivate Static Boolean settitle (String title) throws documentexception {font font = new Font (Basefont, Font.bol D); Paragraph ptitle = new Paragraph (title + "\ n"); Ptitle.setfont (font); Ptitle.setalignment (Element.align_center); Return Document.add (Ptitle); /** * Set Document Contents * @param content Document Contents * @return * @throws documentexception * * * private static Boo Lean SetContent (String content) throws documentexception {font font = new Font (Basefont, ten, font.normal); Paragraph pcontent = new Paragraph (content); Set the font pcontent.setfont (fonts); Pcontent.setalignment (Element.align_left); Pcontent.setspacingafter (5); Pcontent.setfirstlineindent (20); Return Document.add (pcontent); /** * Create rich content Word document * @param filePath document Save address * @param title document Title * @param contents Document content * @ret URN */public static Boolean Createwordfile (string filePath, String titLe, list<string> contents) {Boolean returnvalue = false; try {openwordfile (FilePath); returnvalue = Settitle (title); for (int i = 0; i < contents.size (); i++) {returnvalue = returnvalue && setcontent (contents.get (i)); } document.close (); } catch (Documentexception e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } return returnvalue; /** * Create a single content Word document * @param filePath document Save address * @param title document title * @param content Document Contents * @ret URN */public static Boolean Createwordfile (String FilePath, string title, string content) {Boolean RETURNV Alue = false; try {openwordfile (FilePath); returnvalue = Settitle (title); returnvalue = returnvalue && setcontent (content); Document.close (); } catch (Documentexception e){E.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } return returnvalue; } @SuppressWarnings ({"Unused", "static-access"}) public static void main (string[] args) {Wordutil wordutil = new Wordutil (); list<string> strlist = new arraylist<string> (); Incoming content is string wordutil.createwordfile ("C:\\word.doc", "title centered", "I love Java"); The incoming content is a string list//wordutil.createwordfile ("E:\\word.doc", "title centered", Strlist); }}
Java generates Word documents