Java generates Word documents

Source: Internet
Author: User

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

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.