This document uses the itext jar package to encapsulate and export existing data. You can also use office to open the RTF file.
The Code includes setting the header and footer, adding a table, etc.
For details, see the source code: annotation description.
/*** Wordserver. java * copyright (c) 2012 * created: cuiran 13:20:40 */package COM. cayden. web. base; import Java. AWT. color; import Java. io. fileoutputstream; import Java. io. ioexception; import java.net. URL; import Java. util. list; import Org. apache. commons. logging. log; import Org. apache. commons. logging. logfactory; import COM. cayden. web. action. qybj. queryproducesaction; import COM. cayden. web. domain. qybj. qybjcomp Any; import COM. cayden. web. domain. qybj. qybjproduce; import COM. cayden. web. domain. qybj. qybjproducedevice; import COM. cayden. web. domain. qybj. qybjproduceparam; import COM. cayden. web. dto. qybj. qybjproducedto; import COM. lowagie. text. cell; import COM. lowagie. text. document; import COM. lowagie. text. extends entexception; import COM. lowagie. text. element; import COM. lowagie. text. font; import COM. lowagie. text. headerfooter; Import COM. lowagie. text. image; import COM. lowagie. text. pagesize; import COM. lowagie. text. paragraph; import COM. lowagie. text. phrase; import COM. lowagie. text. table; import com.lowagie.text.pdf. basefont; import COM. lowagie. text. RTF. rtfwriter2; import COM. lowagie. text. RTF. field. rtfpagenumber; import COM. lowagie. text. RTF. field. rtftotalpagenumber; import COM. lowagie. text. RTF. headerfooter. rtfheaderfooter;/*** I Text * data export word class * @ author cuiran * @ version todo */public class wordserver {Private Static log = logfactory. getlog (wordserver. class. getname ()); /***** create a Word document * @ Param filename file name * @ Param DTO Data encapsulation object * @ throws define entexception * @ throws ioexception */Public String creatertfcontext (string filename, qybjproducedto DTO) throws merge entexception, ioexception {string Path = ""; string ImagePath = ""; Try {/*** get the object first */qybjproduce = DTO. getqybjproduce (); List <qybjproducedevice> devices = DTO. getpdevices (); List <qybjproduceparam> Params = DTO. getpparams (); qybjcompany = DTO. getqybjcompany ();/*** get export file path */URL url = wordserver. class. getclassloader (). getresource (""); Path = URL. getpath (). replace ("WEB-INF/classes/", "file/" + filename + ". rtf "); ImagePath = URL. getpath (). replace ("WEB-INF/clas SES/", qybjcompany. getimagepath (); document = new document (pagesize. a4); rtfwriter2 rtfwriter2 = rtfwriter2.getinstance (document, new fileoutputstream (PATH); document. open (); // set the Chinese font basefont bfchinese = basefont. createfont ("stsongstd-light", "UniGB-UCS2-H", basefont. not_embedded); // title Font titlefont = new font (bfchinese, 12, Font. bold); font titlebaofont = new font (bfchinese, 20, Font. BOL D); font underlinefont = new font (bfchinese, 12, Font. underline); // font contextfont = new font (bfchinese, 10, Font. normal); // body Title 1 style font contexttitle1font = new font (bfchinese, 12, Font. normal); // set bold font boldfont = new font (bfchinese, 10, Font. bold);/*** test part * // paragraph Title = new paragraph ("title"); // set the title format alignment // Title. setaliignment (element. align_center); // Title. setfont (titlefont );/ /// Document. Add (title); // string contextstring = "itext is a Java class library that can quickly generate PDF files. The Java class of itext is useful for read-only documents that need to generate texts, tables, and images. Its Class Library is particularly well-formed with Java Servlet. Using itext and PDF allows you to properly control servlet output. "; // Paragraph context = new paragraph (contextstring); // left-aligned body format // context. setalignment (element. align_left); // context. setfont (contextfont); // number of rows left blank in the previous paragraph (title) // context. setspacingbefore (20); // you can specify the number of columns in the first row. // context. setfirstlineindent (20); // document. add (context);/*** set the body */Paragraph producename = new paragraph (qybjproduce. getproducename (), titlefont); document. add (producename); paragraph bjpricetext = new paragraph ("Price:", titlefont); paragraph bjprice = new paragraph (qybjproduce. getprice (), underlinefont); paragraph bjdatetext = new paragraph ("quotation Date:", titlefont); paragraph bjdate = new paragraph (qybjproduce. getbjdate (), underlinefont); paragraph customertext = new paragraph ("customer unit:", titlefont); paragraph customer = new paragraph (qybjproduce. getcompany (), underlinefont); paragraph linkmantext = new paragraph ("Contact Person:", titlefont); paragraph linkman = new paragraph (qybjproduce. getlinkman (), underlinefont); paragraph faxtext = new paragraph ("Chuan Zhen:", titlefont); paragraph Fax = new paragraph (qybjproduce. getlinkfax (), underlinefont); paragraph linkphonetext = new paragraph ("Tel:", titlefont); paragraph linkphone = new paragraph (qybjproduce. getlinkphone (), underlinefont); paragraph bjcol = new paragraph (); bjcol. add (bjpricetext); bjcol. add (customertext); bjcol. add (faxtext);/*** use table */table tableproduce = new table (4, 3); float [] widths = {0.2f, 0.4f, 0.2f, 0.2f}; tableproduce. setwidths (widths); tableproduce. setborderwidth (1); tableproduce. setborder (0); tableproduce. setbordercolor (color. black); tableproduce. setpadding (0); tableproduce. setspacing (0); tableproduce. setwidth (100f); cell cell0 = new cell (); // cell cell0.add (bjpricetext); // cell0.setborderwidth (0f); tableproduce. addcell (cell0); cell0 = new cell (); // price cell cell0.add (bjprice); // cell0.setborderwidth (0f); tableproduce. addcell (cell0); cell0 = new cell (); // quote date tag cell cell0.add (bjdatetext); tableproduce. addcell (cell0); cell0 = new cell (); // quote date cell cell0.add (bjdate); tableproduce. addcell (cell0); cell0 = new cell (); // cell0.add (mermertext); tableproduce. addcell (cell0); cell0 = new cell (); // cell0.add (customer); tableproduce. addcell (cell0); cell0 = new cell (); // contact label cell cell0.add (linkmantext); tableproduce. addcell (cell0); cell0 = new cell (); // contact cell cell0.add (linkman); tableproduce. addcell (cell0); cell0 = new cell (); // Fax label cell cell0.add (faxtext); tableproduce. addcell (cell0); cell0 = new cell (); // Fax cell cell0.add (fax); tableproduce. addcell (cell0); cell0 = new cell (); // contact phone number label cell cell0.add (linkphonetext); tableproduce. addcell (cell0); cell0 = new cell (); // contact cell cell0.add (linkphone); tableproduce. addcell (cell0); document. add (tableproduce);/*** add production line remarks */Paragraph remarktext = new paragraph (qybjproduce. getremark (), contextfont); remarktext. setalignment (paragraph. align_center); document. add (remarktext); paragraph paramtitletext = new paragraph ("1. main technical parameters: ", contexttitle1font); document. add (paramtitletext);/*** add technical parameter table */table tablemainparam = new table (3, Params. size (); float [] widthmainparams = {0.08f, 0.3f, 0.62f}; tablemainparam. setwidths (widthmainparams); tablemainparam. setborderwidth (1); tablemainparam. setborder (0); tablemainparam. setbordercolor (color. black); tablemainparam. setpadding (0); tablemainparam. setspacing (0); tablemainparam. setwidth (100f); For (INT I = 0; I <Params. size (); I ++) {Cell cellparam = new cell (); // cell qybjproduceparam Param = Params. get (I); paragraph xuhaotext = new paragraph (I + 1) + ". ", contextfont); cellparam. add (xuhaotext); tablemainparam. addcell (cellparam); cellparam = new cell (); // Parameter Name: paragraph paramnametext = new paragraph (Param. getparamename (), contextfont); cellparam. add (paramnametext); tablemainparam. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph paramvaluetext = new paragraph (Param. getparamevalue (), contextfont); cellparam. add (paramvaluetext); tablemainparam. addcell (cellparam);} document. add (tablemainparam);/*** add device configuration 1, */Paragraph devicetitletext = new paragraph ("2. device Configuration: ", contexttitle1font); document. add (devicetitletext);/*** add device configuration 2, */table tabledevice = new table (7, devices. size (); float [] widthdevices = {0.04f, 0.2f, 0.2f, 0.08f, 0.08f, 0.2f, 0.2f}; tabledevice. setwidths (widthdevices); tabledevice. setborderwidth (1); tabledevice. setborder (0); tabledevice. setbordercolor (color. black); tabledevice. setpadding (0); tabledevice. setspacing (0); tabledevice. setwidth (100f);/*** set table header */cell celldevice0 = new cell (); paragraph xuhao = new paragraph ("no.", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcells (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("device name", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("type", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("quantity", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("brand name", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("function", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); celldevice0 = new cell (); xuhao = new paragraph ("Remarks", contextfont); xuhao. setalignment (paragraph. align_center); celldevice0.add (xuhao); tabledevice. addcell (celldevice0); For (INT I = 0; I <devices. size (); I ++) {Cell cellparam = new cell (); // cell qybjproducedevice device = devices. get (I); paragraph xuhaotext = new paragraph (I + 1) + ". ", contextfont); cellparam. add (xuhaotext); tabledevice. addcell (cellparam); cellparam = new cell (); // Parameter Name: paragraph devicenametext = new paragraph (device. getdevicename (), contextfont); cellparam. add (devicenametext); tabledevice. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph sizetext = new paragraph (device. getsize (), contextfont); cellparam. add (sizetext); tabledevice. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph amounttext = new paragraph (device. getamount (), contextfont); cellparam. add (amounttext); tabledevice. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph factorytext = new paragraph (device. getfactory (), contextfont); cellparam. add (factorytext); tabledevice. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph functext = new paragraph (device. getfunc (), contextfont); cellparam. add (functext); tabledevice. addcell (cellparam); cellparam = new cell (); // parameter value: paragraph remark1text = new paragraph (device. getremark (), contextfont); cellparam. add (remark1text); tabledevice. addcell (cellparam);} document. add (tabledevice); paragraph specstitletext = new paragraph ("3. device composition specification: ", contexttitle1font); document. add (specstitletext); table tablespecs = new table (2, devices. size (); float [] widthspecss = {0.04f, 0.96f}; tablespecs. setwidths (widthspecss); tablespecs. setborderwidth (1); tablespecs. setborder (0); tablespecs. setbordercolor (color. black); tablespecs. setpadding (0); tablespecs. setspacing (0); tablespecs. setwidth (100f); For (INT I = 0; I <devices. size (); I ++) {Cell cellspec = new cell (); // cell qybjproducedevice device = devices. get (I); paragraph xuhaotext = new paragraph (I + 1) + ". ", contextfont); cellspec. add (xuhaotext); cellspec. setrowspan (2); // set the table to two rows of tablespecs. addcell (cellspec); cellspec = new cell (); // canonical name paragraph specnametext = new paragraph (device. getspecname (), contextfont); cellspec. add (specnametext); tablespecs. addcell (cellspec); cellspec = new cell (); // standard content paragraph specstext = new paragraph (device. getspecs (), contextfont); cellspec. add (specstext); tablespecs. addcell (cellspec);} document. add (tablespecs);/*** set the header */image headerimage = image. getinstance (ImagePath); paragraph headerpara2 = new paragraph (qybjcompany. getcompanyname (), titlefont); headerpara2.setalignment (headerfooter. align_center); Response header1 = new rtfheaderfooter (headerimage); paragraph headerpara1 = new paragraph (); Response (headerpara2); headerpara1.add (header1); Response (headerfooter. align_center); phrase headerpara = new phrase ();/*** Insert table * three columns and two rows */Table = new table (); table. setborderwidth (1); table. setborder (0); table. setbordercolor (color. black); table. setpadding (0); table. setspacing (0); // table. setalignment (table. align_middle); table. setwidth (100f); cell = new cell (headerpara1); // cell. setborderwidth (0f); // set the table to no cell border. setheader (true); cell. setrowspan (2); // The current cell occupies two rows, with a vertical span of table. addcell (cell); table. endheaders (); // the header ends the cell cellheader = new cell (); paragraph producetitle = new paragraph (qybjproduce. getproducename (), titlefont); producetitle. setalignment (headerfooter. align_center); cellheader. add (producetitle); table. addcell (cellheader); cellheader = new cell (); paragraph parafooter = new paragraph (); parafooter. add (new phrase ("Total"); parafooter. add (New rtftotalpagenumber (); // Several parafooter pages in total. add (new phrase ("page"); parafooter. setalignment (1); cellheader. add (parafooter); table. addcell (cellheader); cellheader = new cell (); paragraph ptitle = new paragraph ("", titlebaofont); ptitle. setalignment (1); cellheader. add (ptitle); cellheader. setuseascender (true); cellheader. sethorizontalalignment (element. align_center); // horizontally centered cellheader. setverticalalignment (element. align_middle); // vertically centered table. addcell (cellheader); cellheader = new cell (); paragraph para1footer = new paragraph (); para1footer. add (new phrase ("th"); para1footer. add (New rtfpagenumber (); // page of para1footer. add (new phrase ("page"); para1footer. setalignment (1); cellheader. add (para1footer); cellheader. setuseascender (true); cellheader. sethorizontalalignment (element. align_center); // horizontally centered cellheader. setverticalalignment (element. align_middle); // vertically centered table. addcell (cellheader); headerpara. add (table); headerfooter header = new headerfooter (headerpara, false); header. setalignment (headerfooter. align_center); document. setheader (header);/*** set footer */Paragraph footerpara1 = new paragraph ("company address:" + qybjcompany. getcompanyaddr (), contextfont); paragraph footerpara2 = new paragraph ("Sales Hotline:" + qybjcompany. getmoblephone () + "" + qybjcompany. getlegalperson (), contextfont); paragraph footerpara3 = new paragraph ("this quotation is valid for one month from the date of quotation", titlefont); paragraph footerpara = new paragraph. add (footerpara1); footerpara. add (footerpara2); footerpara. add (footerpara3); headerfooter footer = new headerfooter (footerpara, false); footer. setalignment (headerfooter. align_left); document. setfooter (footer); // Add an image to the end of the table // image PNG = image. getinstance ("C:/1.png"); // document. add (PNG); document. close ();} catch (exception e) {// todo: handle exceptionlog. error ("an exception occurred when exporting word:", e) ;}return path ;}/ *** todo * @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stub }}
Wordserver. Java http://vdisk.weibo.com/s/hbt4yjarpackage:
Hi, recommended file for you "itextasian. Jar" http://vdisk.weibo.com/s/hBt2f
Hi, recommended file for you "iText-2.1.7.jar" http://vdisk.weibo.com/s/hBt0N
Hi, recommended file for you "itext-rtf-2.1.7.jar" http://vdisk.weibo.com/s/hBsXY