[Web development] ☆★Using itext to export word tables and process images

Source: Internet
Author: User

In actual project development, we need to export a large amount of data in the background to word or excel to facilitate user operations. Of course, freemarker, itext, poi and other technologies can complete this function, this article describes how to export word with itext.

The first thing we need to understand is that whether freemarker, itext, or poi is to first make a template or draw a template and then fill the content in it, then with this idea, we can do this function well.


Three required packages for development:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/140120/223641N06-0.jpg "style =" float: left; "title =" QQ20140117152456.png "alt =" wKiom1LY2vvANViGAACZHD5WK4M639.jpg "/>





The following code is used directly:

Package com. csg. action; import java. awt. color; import java. io. fileOutputStream; import java. io. IOException; 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. image; import com. lowagie. text. pageSize; import com. lowagie. text. paragraph; import com. lowagie. text. phrase; impo Rt com. lowagie. text. table; import com.lowagie.text.pdf. baseFont; import com. lowagie. text. rtf. rtfWriter2; import com. lowagie. text. rtf. style. rtfFont; public class WordTest2 {public void createDocContext (String file) throws extends entexception, IOException {// set the paper size Document = new document (PageSize. a4); // create a Writer to associate with the document object. The Writer can write the document to the disk RtfWriter2.getInstance (document, ne W FileOutputStream (file); document. open (); // set the Chinese font BaseFont bfChinese = BaseFont. createFont ("STSongStd-Light", "UniGB-UCS2-H", BaseFont. NOT_EMBEDDED); // title Font Style RtfFont titleFont = new RtfFont ("", 21, Font. BOLD, Color. BLACK);/* Font titleFont = new Font (bfChinese, 21, Font. BOLD); * // text Font contextFont = new Font (bfChinese, 12, Font. NORMAL); Paragraph title = new Paragraph ("xx Emy of art collection Review Core table "); // set the title format alignment mode title. setAlignment (Element. ALIGN_CENTER); title. setFont (titleFont); document. add (title); // set Table aTable = new Table (2); // set Table to 2 columns int width [] = {4, 96 }; // percentage of each column aTable. setWidths (width); // set the percentage of each column to aTable. setWidth (100); // The page width is 100% aTable. setAlignment (Element. ALIGN_CENTER); // The center displays aTable. setAlignment (Element. ALIGN_MIDDLE); // the vertical center displays aTable. setAutoFillEmptyCells (True); // automatically fills up aTable. setBorderWidth (1); // Border Width aTable. setBorderColor (Color. BLACK); // The border color aTable. setPadding (0); // padding. You can see what aTable means. setSpacing (0); // distance between cells aTable. setBorder (1); // border Font fontChinese = new Font (bfChinese, 12, Font. NORMAL, Color. black); Cell cell = new Cell (new Phrase ("art description", fontChinese); cell. setHorizontalAlignment (Element. ALIGN_CENTER); cell. setBorderColor (Color. BLA CK); aTable. addCell (cell); cell = new Cell (new Phrase. ", FontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setHorizontalAlignment (Element. ALIGN_CENTER); aTable. addCell (cell); cell = new Cell ("#5"); aTable. addCell (cell); cell = new Cell (new Phrase (": _________ Date: ___ years ___ months ___ Day" + "", fontChinese); cell. setHorizontalAlignment (Element. ALIGN_RIGHT); aTable. addCell (cell); document. add (aTable); // set the Table and create a Table with 10 columns. The background cyclic data aTable = n Ew Table (10); aTable. setWidths (new int [] {4, 10, 20, 10, 20, 10, 20, 20, 6}); aTable. setWidth (100); aTable. addCell (new Cell ("#1"); aTable. addCell (new Cell ("Serial Number"); aTable. addCell (new Cell ("file name"); aTable. addCell (new Cell ("author"); aTable. addCell (new Cell ("S"); aTable. addCell (new Cell ("Product Name"); aTable. addCell (new Cell ("size"); aTable. addCell (new Cell ("number of pieces"); aTable. addCell (new Cell ("add to Favorites (excluding tax )") ); ATable. addCell (new Cell ("tax"); for (int I = 0; I <10; I ++) {aTable. addCell (new Cell (""); aTable. addCell (new Cell ("Cell" + (I + 1) + "pieces"); aTable. addCell (new Cell ("1234"); aTable. addCell (new Cell ("1234"); aTable. addCell (new Cell ("123"); aTable. addCell (new Cell ("123"); aTable. addCell (new Cell ("123"); aTable. addCell (new Cell ("123"); aTable. addCell (new Cell ("123"); aTable. addCell (new Cell ("123") ;}; document. add (aTable); aTable = new Table (2); // set the Table to 2 columns of aTable. setWidths (new int [] {5, 95}); // set the percentage of each column to aTable. setWidth (100); // The page width is 100% cell = new Cell (new Phrase ("collection team member execution collection description", fontChinese); cell. setHorizontalAlignment (Element. ALIGN_CENTER); cell. setBorderColor (Color. BLACK); aTable. addCell (cell); cell = new Cell (new Phrase. After comprehensive analysis, the 14 pieces of work are recommended to be included in the "Collection" collection and issued with a collection certificate. If no, please review ", fontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setHorizontalAlignment (Element. ALIGN_CENTER); aTable. addCell (cell); cell = new Cell (new Phrase ("signature of team members", fontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setRowspan (8); // merge rows aTable. addCell (cell); cell = new Cell (new Phrase ("", fontChinese); cell. setRowspan (8); aTable. addCell (cell); cell = new Cell (New Phrase ("review by deputy team lead", fontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setRowspan (9); aTable. addCell (cell); cell = new Cell (new Phrase ("", fontChinese); cell. setRowspan (9); aTable. addCell (cell); cell = new Cell (new Phrase ("collection team lead Review", fontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setRowspan (8); aTable. addCell (cell); cell = new Cell (new Phrase ("", fontCh Inese); cell. setRowspan (8); aTable. addCell (cell); cell = new Cell (new Phrase ("Art Committee opinion", fontChinese); cell. setVerticalAlignment (Element. ALIGN_MIDDLE); cell. setRowspan (8); aTable. addCell (cell); cell = new Cell (new Phrase ("", fontChinese); cell. setRowspan (8); aTable. addCell (cell); document. add (aTable); // set five table loops for 5 times (int I = 0; I <5; I ++) {// print the image aTable = new Table (2); // set the Table to 2 columns and 3 rows of aTable. SetWidths (new int [] {50, 50}); // set the percentage of each column to aTable. setWidth (100); // 100% page width // Add image 1 cell = new Cell (); cell. setBorderWidth (0); // set the border line to 0 Image img = Image. getInstance ("d:/201305.jpg"); img. setAbsolutePosition (0, 0); img. scaleAbsolute (12, 35); // directly set the display size img. scalePercent (50); // indicates that the displayed size is 50% img of the original size. scalePercent (25, 50); // display ratio of image height and width img. setRotation (30); // rotate the cell to a certain angle. add (img); aTable. addCell (ce Ll); // Add Image 2 cell = new Cell (); cell. setBorderWidth (0); cell. setVerticalAlignment (Element. ALIGN_RIGHT); img. setAlignment (Image. RIGHT); // set the Image display position img = Image. getInstance ("d:/201307.jpg");/** img. scaleAbsolute (); // directly set the display size */img. scalePercent (50); // indicates that the displayed size is 50% img of the original size. scalePercent (25, 50); // display ratio of image height and width img. setRotation (30); // rotate the cell to a certain angle. add (img); aTable. addCell (cell); // Information cell = new Cell (New Phrase ("File 1:" symbol of civilization "", fontChinese); cell. setBorderWidth (0); aTable. addCell (cell); cell = new Cell (new Phrase ("file 2: Shi Yu 1", fontChinese); cell. setBorderWidth (0); aTable. addCell (cell); cell = new Cell (new Phrase ("fallen: 23/25 civilized Symbol: xxx 2009", fontChinese); cell. setBorderWidth (0); aTable. addCell (cell); cell = new Cell (new Phrase ("one of 7/15 stone prints xxx 2011", fontChinese); cell. setBorderWidth (0); ATable. addCell (cell); document. add (aTable);} document. add (new Paragraph ("\ n" + "\ n"); title = new Paragraph (""); // set the title format Alignment Method title. setAlignment (Element. ALIGN_CENTER); titleFont = new RtfFont ("", 16, Font. BOLD, Color. BLACK); title. setFont (titleFont); title. setSpacingAfter (10); document. add (title); // text String contextString = "xxx1969-born in Nanjing, Beijing, born in Chaozhou, Guangdong Province. In 1996, he graduated from the copyright department of Guangzhou Emy of fine arts and stayed at the school. He is now an associate professor in the copyright department of Guangzhou Emy of Fine Arts, a guest professor from Shanxi University, a member of the Chinese Artists Association, and the executive vice chairman of the Association for admission tickets, painter of Guangdong youth Painting Institute. "+" Multiple works were selected for the National Art Exhibition, National Exhibition, international exhibition, and other academic exhibitions. He has won the Ninth National Art Exhibition Excellence Award, the second prize of Contemporary Chinese youth exhibition, the second prize of the State Council for celebrating Macao's return to the motherland propaganda and posters, the second prize of the excellent exhibition of integrity culture of the whole army, the bronze prize of Guangdong art exhibition, and the Guangdong printmaking prize. academic Awards. "+" \ N "// line feed +" works and papers published in "art", "Art observation", "Chinese printmaking", "Art Journal", and "Northern art "academic Publications. Publishing textbooks such as stone prints and printprints; publishing works of zhongrui xxx; write "China art event", "Contemporary Chinese art", and "Chinese contemporary young and middle-aged painter Shi printmaking Collection. "+" \ N "// newline +" works were edited by Guangdong Art Museum, Shenzhou printmaking Museum, China Century Altar, Shaoxing Lu Xun Memorial Hall, guanlan Art Museum, Shantou Museum, Guangzhou Art College, and the international version of the painting Museum in Istanbul. collections of academic institutions and individuals. "; Paragraph context = new Paragraph (contextString); // The left-aligned context of the body format. setAlignment (Element. ALIGN_LEFT); context. setFont (contextFont); // number of rows left blank from the previous paragraph title. setSpacingBefore (10); // you can specify the number of empty columns in the first row. setFirstLineIndent (20); document. add (context); document. close ();} public static void main (String [] args) {WordTest2 B = new WordTest2 (); try {B. createDocContext ("d:/demo.doc"); System. out. println ("exported successfully");} catch (incluentexception e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}}

Copy the sample image in this article to D: disk!

This article focuses on testing. In practice, this code is only for reference. In a hurry, please correct me!

Source code: http://down.51cto.com/data/1071741


This article from the "promise always attachment Xiao Chai," blog, please be sure to keep this source http://1936625305.blog.51cto.com/6410597/1352558

Related Article

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.