Poi generates a simple version of the word document (including text, tables, images, and Font Style settings)

Source: Internet
Author: User
 

Poi generates Word documents in two ways:

① Generate a Word document using a Word template;

② Directly generate WORD Documents by writing code;

Here I demonstrate the second method, that is, writing code to generate WORD Documents, not much nonsense, direct code;

 

/*** Town street daily report export word */@ requestmapping (Params = "exportwordfortowndaily") Public void exportwordfortowndaily (httpservletresponse response, string SelectDate) {If (stringutils. isnullorempty (SelectDate) {return;} simpledateformat SDF = new simpledateformat ("yyyy-mm-dd"); try {SDF. parse (SelectDate);} catch (exception e) {e. printstacktrace (); return;} caseentity entity = new caseentity (); entity. setrepotingtime_begin (SelectDate + "00:00:00"); entity. setrepotingtime_end (SelectDate + "23:59:59"); // obtain the actual score list of ZhenJie. <caseentity> caseentitylist = statisticsservice. gettownactualscore (entity); // obtain the list of towns and streets <Map <string, Object> towndaily = statisticsservice. towndaily (entity); // format the query date (year, month, day) simpledateformat sdf2 = new simpledateformat ("mm DD, YYYY"); try {SelectDate = sdf2.format (SDF. parse (SelectDate);} catch (parseexcepti On E) {e. printstacktrace ();} // ------------------------------------ generate a Word document watermark // create a Document Object xwpfdocumentutil document = new xwpfdocumentutil (); // create a title section xwpfparagraph titleparag. createparagraph (); xwpfrun titlerun = titleparag. createrun (); titlerun. settext ("town street daily"); titlerun. setfontsize (20); titlerun. setbold (true); // whether the font is bold titleparag. setalignment (para Graphalignment. left); // The section is left // line feed // xwpfparagraph brparagraph1 = document. createparagraph (); // xwpfrun brrun = brparagraph1.createrun (); // brrun. settext ("\ n"); // create a paragraph xwpfparagraph P = document. createparagraph (); int rows = caseentitylist. size () + 1; int Cols = 3; // create a table xwpftable table = document. createtable (rows, cols); // table attribute cttblpr tablepr = table. getcttbl (). addnewtblpr (); // table width cttblwidth width = Tab Lepr. addnewtblw (); width. SETW (biginteger. valueof (5000); // set the table width to non-automatic width. settype (sttblwidth. DXA); // the header row xwpftablerow headrow = table. getrow (0); xwpftablecell headcell0 = headrow. getcell (0); xwpftablecell headcell1 = headrow. getcell (1); xwpftablecell headcell2 = headrow. getcell (2); P = headcell0.addparagraph (); xwpfrun headrun0 = P. createrun (); headrun0.settext ("Serial Number"); headrun0.setfontsize (12); headrun0.setb Old (true); // whether to bold headcell0.setcolor ("dedede"); // vertically Center P. setverticalalignment (textalignment. center); // horizontally Center P. setalignment (paragraphalignment. center); P = headcell1.addparagraph (); xwpfrun headrun1 = P. createrun (); headrun1.settext ("town street"); headrun1.setfontsize (12); headrun1.setbold (true); headcell1.setcolor ("dedede"); // vertically Center P. setverticalalignment (textalignment. center); // horizontally Center P. setalignment (paragraphalignment. CE NTER); P = headcell2.addparagraph (); xwpfrun headrun2 = P. createrun (); headrun2.setfontsize (12); headrun2.settext ("total points"); headrun2.setbold (true); headcell2.setcolor ("dedede"); // vertically Center P. setverticalalignment (textalignment. center); // horizontally Center P. setalignment (paragraphalignment. center); // table subject row for (INT I = 0; I <caseentitylist. size (); I ++) {caseentity = caseentitylist. get (I); xwpftablerow contentrow = table. getro W (I + 1); xwpftablecell cell0 = contentrow. getcell (0); xwpftablecell cell1 = contentrow. getcell (1); xwpftablecell cell2 = contentrow. getcell (2); P = cell0.addparagraph (); xwpfrun prun0 = P. createrun (); prun0.settext (I + 1 + ""); // vertically centered cell0.setverticalignment (xwpftablecell. xwpfvertalign. center); // horizontally Center P. setalignment (paragraphalignment. center); P = cell1.addparagraph (); xwpfrun prun1 = P. createrun (); prun1.settext (CA Seentity. gettownname (); // vertically centered cell1.setverticalalignment (xwpftablecell. xwpfvertalign. center); // horizontally Center P. setalignment (paragraphalignment. center); P = cell2.addparagraph (); xwpfrun prun2 = P. createrun (); prun2.settext (caseentity. gettownactualscore (). tostring (); // vertically centered cell2.setverticalignment (xwpftablecell. xwpfvertalign. center); // horizontally Center P. setalignment (paragraphalignment. center );}//--------------------------- -- Town streets ------------------------------ // create the TITLE section xwpfparagraph title1 = document. createparagraph (); xwpfrun title1run = title1.createrun (); title1run. settext ("\ r serious street problems"); title1run. setfontsize (20); title1run. setbold (true); try {for (Map <string, Object> townmap: towndaily) {caseentity towncase = (caseentity) townmap. get ("towncase"); xwpfparagraph = document. createparagraph (); xwpfrun xwpfrun_town = Xwpfparagraph. createrun (); xwpfrun = xwpfparagraph. createrun (); xwpfrun_town.settext (towncase. gettownname (); xwpfrun_town.setfontsize (14); xwpfrun_town.setbold (true); xwpfrun_town.setcolor ("00cc44"); xwpfrun. settext ("," + SelectDate + ":"); xwpfrun. setfontsize (14); xwpfrun. setbold (true); List <Map <string, Object> caseinfolist = (list <Map <string, Object>) townmap. get ("caseinfolist"); integer No = 0; For (Map <string, Object> map: caseinfolist) {caseentity caseinfo = (caseentity) map. get ("caseinfo"); xwpfparagraph xwpfparagraph1 = document. createparagraph (); xwpfrun xwpfrun1 = random (); xwpfrun xwpfrun2 = random (); xwpfrun xwpfrun3 = random (); random (++ no) + ","); xwpfrun2.settext (caseinfo. getproblemstreet (); xwpfrun3.settext ("," + casein Fo. getproblemnotes (); values (14); values (14); xwpfrun2.setcolor ("4747d1"); values (14); List <attachmententity> filelist = (list <attachmententity>) map. get ("filelist"); For (attachmententity at: filelist) {string mainpath =. getmainpath (); string Path =. getpath (); If (! Stringutils. isnullorempty (mainpath )&&! Stringutils. isnullorempty (PATH) {// the first image document. addpicturedata (New fileinputstream (mainpath + path), xwpfdocumentutil. picture_type_jpeg); document. createpicture (document. getallpictures (). size ()-1,300,400, document. createparagraph () ;}}} outputstream output = response. getoutputstream (); // generate the name of the Word file string filename = new string ("town street newspaper .docx "). getbytes ("UTF-8"), "iso-8859-1"); response. setheader ("content-disposition", "attachment; filename =" + filename); // write the word document to the output stream document. write (output);} catch (invalidformatexception e) {e. printstacktrace ();} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} // -------------------------------- Word document ended -----------------------------------------------------}

  

Shows the effect:

 

 

 

 

 

Poi generates a simple version of the word document (including text, tables, images, and Font Style settings)

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.