
Table (7)
This article demonstrates how to align a table in a Word document using Spire.Doc for Java.
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Table; import com.spire.doc.documents.RowAlignment; public class AlignTable { public static void main(String[] args) { //Create a Document object Document doc=new Document(); //Add a table Table table =doc.addSection().addTable(true); //Set column width table.setColumnWidth(new float[]{100f,150f}); //Set column number and row number table.resetCells(2,2); //Add text to cells table.get(0,0).addParagraph().appendText("Product Code"); table.get(0,1).addParagraph().appendText("Description"); table.get(1,0).addParagraph().appendText("T1052"); table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl"); //Align table to center table.getTableFormat().setHorizontalAlignment(RowAlignment.Center); //Save the document doc.saveToFile("AlignTable.docx", FileFormat.Docx_2013); } }
This article demonstrates how to auto fit a Word table to content or to window, as well as how to fix the cloumn widths, by using Spire.Doc for Java.
Autofit to content
import com.spire.doc.*; public class AutofitToContent { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Add a table Table table = section.addTable(); table.resetCells(3, 2); //Add content to the cells table.get(0,0).addParagraph().appendText("Product Code"); table.get(0,1).addParagraph().appendText("Description"); table.get(1,0).addParagraph().appendText("T1052"); table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl"); table.get(2,0).addParagraph().appendText("T1062"); table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi"); //Autofit column widths to contents table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents); //Save the document document.saveToFile("AutofitToContent.docx", FileFormat.Docx); } }
Autofit to window
import com.spire.doc.*; public class AutofitToWindow { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Add a table Table table = section.addTable(); table.resetCells(3, 2); //Add content to the cells table.get(0,0).addParagraph().appendText("Product Code"); table.get(0,1).addParagraph().appendText("Description"); table.get(1,0).addParagraph().appendText("T1052"); table.get(1,1).addParagraph().appendText("AdvoCareSlim Tropical Swirl"); table.get(2,0).addParagraph().appendText("T1062"); table.get(2,1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi"); //Autofit column widths to window table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window); //Save the document document.saveToFile("AutofitToWindow.docx", FileFormat.Docx); } }
Fix column width
import com.spire.doc.*; public class FixColumnWidths { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Add a table Table table = section.addTable(); table.resetCells(3, 2); //Add content to the cells table.get(0, 0).addParagraph().appendText("Product Code"); table.get(0, 1).addParagraph().appendText("Description"); table.get(1, 0).addParagraph().appendText("T1052"); table.get(1, 1).addParagraph().appendText("AdvoCareSlim Tropical Swirl"); table.get(2, 0).addParagraph().appendText("T1062"); table.get(2, 1).addParagraph().appendText("AdvoCareSlim Caffeine Free Strawberry Kiwi"); //Set the column widths for (int i = 0; i < table.getRows().getCount(); i++) { table.get(i,0).setCellWidth(80f,CellWidthType.Point); table.get(i,1).setCellWidth(160f,CellWidthType.Point); } //Fix the column widths so that the column width does not increases when the content exceeds the width table.autoFit(AutoFitBehaviorType.Fixed_Column_Widths); //Save the document document.saveToFile("FixColumnWidths.docx", FileFormat.Docx); } }
This article demonstrates how to insert images to table cells in a Word document using Spire.Doc for Java.
import com.spire.doc.AutoFitBehaviorType; import com.spire.doc.Document; import com.spire.doc.Section; import com.spire.doc.Table; import com.spire.doc.fields.DocPicture; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; public class InsertImageToTableCell { public static void main(String[] args) throws FileNotFoundException { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Add a table Table table = section.addTable(true); table.resetCells(2,2); table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents); //Load an image to InputStream InputStream inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\company-logo.png"); //Insert the image to the cell(0,0) DocPicture picture = table.get(0,0).addParagraph().appendPicture(inputStream); //Set the width and height of the image picture.setWidth(100); picture.setHeight(100); //Insert another image to the cell(1,1) inputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\intro.png"); picture = table.get(1,1).addParagraph().appendPicture(inputStream); picture.setWidth(100); picture.setHeight(100); //Save the document document.saveToFile("InsertImgToCell.docx"); } }
This article demonstrates how to insert a nested table in a table cell using Spire.Doc for Java.
import com.spire.doc.*; public class CreateNestedTable { public static void main(String[] args) { //Create a Document object Document doc = new Document(); //Add a section Section section = doc.addSection(); //Add a table Table table = section.addTable(true); table.resetCells(2, 3); //Set column width table.getRows().get(0).getCells().get(0).setWidth(50f); table.getRows().get(0).getCells().get(2).setWidth(50f); table.getRows().get(1).getCells().get(0).setWidth(50f); table.getRows().get(1).getCells().get(2).setWidth(50f); table.autoFit(AutoFitBehaviorType.Auto_Fit_To_Window); //Insert content to cells table.get(0,0).addParagraph().appendText("SI.No."); String text = "Earthwork excavation for foundation of buildings, water supply, " + "sanitary lines and electrical conduits either in pits or in " + "trenches 1.5m and above in width, in ordinary soil not exceeding " + "1.5m in depth including dressing the bottom and sides of pits and " + "trenches, stacking the excavated soil clear."; table.get(0,1).addParagraph().appendText(text); table.get(0,2).addParagraph().appendText("Qty"); //Add a nested table to cell(0,1) Table nestedTable = table.get(0,1).addTable(); nestedTable.resetCells(3, 4); nestedTable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents); //Add content to the cells of nested table nestedTable.get(0,0).addParagraph().appendText("SI.No."); nestedTable.get(0,1).addParagraph().appendText("Item"); nestedTable.get(0,2).addParagraph().appendText("Qty"); nestedTable.get(0,3).addParagraph().appendText("Rate"); nestedTable.get(1,0).addParagraph().appendText("1"); nestedTable.get(1,1).addParagraph().appendText("Sand"); nestedTable.get(1,2).addParagraph().appendText("30"); nestedTable.get(1,3).addParagraph().appendText("45"); nestedTable.get(2,0).addParagraph().appendText("2"); nestedTable.get(2,1).addParagraph().appendText("Cement"); nestedTable.get(2,2).addParagraph().appendText("30"); nestedTable.get(2,3).addParagraph().appendText("50"); //Save the document doc.saveToFile("NestedTable.docx", FileFormat.Docx_2013); } }
Set Style and Border of Table in Word in Java
Written by support iceblueIn this article, we will introduce how to set style and border of a Word table using Spire.Doc for Java.
The following screenshot shows the table before setting style and border:
import com.spire.doc.Document; import com.spire.doc.FileFormat; import com.spire.doc.Section; import com.spire.doc.Table; import com.spire.doc.documents.BorderStyle; import com.spire.doc.documents.DefaultTableStyle; import java.awt.*; public class SetTableStyleAndBorder { public static void main(String[] args){ //create a Document instance Document document = new Document(); //Load the Word document document.loadFromFile("tableSample.docx"); Section section = document.getSections().get(0); //get the first table Table table = section.getTables().get(0); //apply the table style table.applyStyle(DefaultTableStyle.Colorful_List); //set right border of table table.getTableFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getRight().setLineWidth(1.0F); table.getTableFormat().getBorders().getRight().setColor(Color.RED); //set top border of table table.getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getTop().setLineWidth(1.0F); table.getTableFormat().getBorders().getTop().setColor(Color.GREEN); //set left border of table table.getTableFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline); table.getTableFormat().getBorders().getLeft().setLineWidth(1.0F); table.getTableFormat().getBorders().getLeft().setColor(Color.YELLOW); //set bottom border of table table.getTableFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash); //set vertical and horizontal border table.getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Dot); table.getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.None); table.getTableFormat().getBorders().getVertical().setColor(Color.ORANGE); //save the result file document.saveToFile("setTableStyleAndBorder.docx", FileFormat.Docx_2013); } }
The following screenshot shows the table after setting style and border:
Java merge and split table cells on Word document
Written by support iceblueThis article will demonstrate how to merge and split table cells on word document in Java applications.
Merge Table cells:
import com.spire.doc.*; public class MergeTableCell { public static void main(String[] args) throws Exception { String output = "output/MergeTableCells.docx"; //Create a Document instance Document document = new Document(); Section section = document.addSection(); Table table = section.addTable(true); table.resetCells(4, 4); //how to merge cells horizontally table.applyHorizontalMerge(0, 0, 3); //how to merge cells vertically table.applyVerticalMerge(0, 2, 3); //save the document to file document.saveToFile(output, FileFormat.Docx); } }
Split table cells:
import com.spire.doc.*; public class SplitTableCell { public static void main(String[] args) throws Exception { String output = "output/SplitTableCells.docx"; //Create a Document instance Document document = new Document(); Section section = document.addSection(); Table table = section.addTable(true); table.resetCells(4, 4); //split the cell table.getRows().get(3).getCells().get(3).splitCell(2, 2); //save the document to file document.saveToFile(output, FileFormat.Docx); } }
This article demonstrates how to create table in a Word document using Spire.Doc for Java.
import com.spire.doc.*; import com.spire.doc.documents.*; import com.spire.doc.fields.TextRange; import java.awt.*; public class CreateTable { public static void main(String[] args) { //Create a Document object Document document = new Document(); //Add a section Section section = document.addSection(); //Data used to create table String[] header = {"Name", "Capital", "Continent", "Area", "Population"}; String[][] data = { new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"}, new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"}, new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"}, new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"}, new String[]{"Chile", "Santiago", "South America", "756943", "13200000"}, new String[]{"Colombia", "Bogota", "South America", "1138907", "33000000"}, new String[]{"Cuba", "Havana", "North America", "114524", "10600000"}, new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"}, new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"}, new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"}, new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"}, new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"}, new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"}, new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"}, new String[]{"Peru", "Lima", "South America", "1285215", "21600000"}, new String[]{"America", "Washington", "North America", "9363130", "249200000"}, new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"}, new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"} }; //Add table Table table = section.addTable(true); table.resetCells(data.length + 1, header.length); //Set the first row as table header and add data TableRow row = table.getRows().get(0); row.isHeader(true); row.setHeight(20); row.setHeightType(TableRowHeightType.Exactly); row.getRowFormat().setBackColor(Color.gray); 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 txtRange = p.appendText(header[i]); txtRange.getCharacterFormat().setBold(true); } //Add data to the rest of rows 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); dataRow.getCells().get(c).addParagraph().appendText(data[r][c]); } } //Set background color for cells for (int j = 1; j < table.getRows().getCount(); j++) { if (j % 2 == 0) { TableRow row2 = table.getRows().get(j); for (int f = 0; f < row2.getCells().getCount(); f++) { row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230)); } } } //save the document document.saveToFile("CreateTable.docx", FileFormat.Docx_2013); } }
Output: