Table as a visual means of communication patterns and organizational consolidation of data, widely used in various occasions and in the documentation. Common forms can contain text, images and other elements, we can insert a picture when operating tables, write text and table formatting styles. Hereinafter, created by the Java programming Word document form and the formatting operation, including setting the font, font size, font color, font weight, etc., provided the cell alignment, the background color of the cell, cell merging / splitting, form border style, insert pictures and more.
Use tools: as Free Spire.Doc for the Java 2.0.0 (free version)
Jar file import
Method 1 : First, by obtaining official website jar package. Download and unzip the controls.
Introducing steps : Create a new directory in the program directory, and the name (in this example named lib); the control packet lib Spire.Doc.jar file folder (as in FIG. 1) in the program copied to the new directory. After copying the jar file, right-click on the jar file, select "Add as Library". Import completed (FIG. 2 below).
Figure 1 :
Figure 2 :
Method 2 : introduced through maven. Reference introduction method .
Java code sample (for reference)
* com.spire.doc Import;. Import com.spire.doc.documents *;. Import com.spire.doc.fields.DocPicture; Import com.spire.doc.fields.TextRange; Import in java.awt *;. public the CreateTable {class public static void main (String [] args) { // create a Document Object Document DOC = new new Document (); Section sec = doc.addSection (); // declare array contents String [] header = { "class" , "name", "gender", "study", "professional achievement"}; String [] [] the Data = { new new String [] { "class", "Wang Li", "female", "Y1256486" "138"}, new new String [] { "class", "Hong Feifei "," female "," Y5425875 "," 134 "}, new new String [] { "second class", "Liu Yang "," M "," B1546258 "," 141 "}, new String [] { "three classes", "Feng Gang", "M", "B1542367", "136"}, new String [] { "three classes", "Liu Siyuan", "M", "Z1263547", "133"} }; // add form the table sec.addTable = table (to true); // set the table number of rows and columns table.resetCells (+ data.length. 1, header.length); // set table as the first row header, write header array content, and format data header the TableRow a Table.getRows row = () GET (0);. row.isHeader (to true); row.setHeight (20 is); row. setHeightType (TableRowHeightType.Exactly); Exactly); row.getRowFormat().setBackColor(Color.ORANGE); for (int I = 0; I <header.length; I ++) { . row.getCells () GET (I) .getCellFormat ().setVerticalAlignment (VerticalAlignment.Middle); Paragraph p = row.getCells().get(i).addParagraph(); p.getFormat().setHorizontalAlignment(HorizontalAlignment.Center); TextRange range1 = p.appendText(header[i]); range1.getCharacterFormat().setFontName("Arial"); range1.getCharacterFormat().setFontSize(12f); range1.getCharacterFormat().setBold(true); range1.getCharacterFormat().setTextColor(Color.white); } //写入剩余组内容到表格,并格式化数据 for (int r = 0; r < data.length; r++) { TableRow dataRow = table.getRows().get(r + 1); dataRow.setHeight(25); dataRow.setHeightType(TableRowHeightType.Exactly); dataRow.getRowFormat().setBackColor(Color.white); for (int c = 0; c < data[r].length; c++) { dataRow.getCells().get(c).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle); TextRange range2 = dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); range2.getCharacterFormat().setFontName("Arial"); range2.getCharacterFormat().setFontSize(10f); } } //纵向合并指定单元格 table.applyVerticalMerge(0,1,2); table.applyVerticalMerge(0,4,5); //插入图片到指定单元格 DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png"); dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center); //设置单元格背景颜色 for (int j = 1; j < table.getRows().getCount(); j++) { if (j % 2 == 0) { TableRow row2 = table.getRows().get(j); for (int f = 1; f < row2.getCells().getCount(); f++) { row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144)); } } } // set the table border style table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap); // Save the document doc.saveToFile ( "CreateTable.docx", FileFormat.Docx_2013); } }
Table create effects:
(This article End)