Java add tables, nested tables in Word

Table as a visual mode of communication and organization, ways to organize 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, nested 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 (free version)
Jar file acquisition and import:
Method 1: through the official website to download the package file. After the download, unzip the file and the lib folder under the Spire.Doc.jar file into java program.
Method 2: mounting introduced through maven repository, reference may be introduced into the method .


Java code examples

[Example 1] Adding table

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;

public class CreateTable {
    public static void main(String[] args){
        //创建Document对象
        Document doc = new Document();
        Section sec = doc.addSection();

        //声明数组内容
        String[] header = {"班级","姓名","性别", "学号", "专业成绩"};
        String[][] data =
                {
                        new String[]{"一班","王丽", "女", "Y1256486", "138"},
                        new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},
                        new String[]{"二班","刘洋", "男", "B1546258", "141"},
                        new String[]{"三班","冯刚", "男", "B1542367", "136"},
                        new String[]{"三班","刘思源", "男", "Z1263547", "133"},
                };

        //添加表格
        Table table = sec.addTable(true);

        //设置表格的行数和列数
        table.resetCells(data.length + 1, header.length);

        //设置表格第一行作为表头,写入表头数组内容,并格式化表头数据
        TableRow row = table.getRows().get(0);
        row.isHeader(true);
        row.setHeight(20);
        row.setHeightType(TableRowHeightType.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));
                }
            }
        }

        //设置表格边框样式
        table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);

        //保存文档
        doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
    }
}

Form add effects:

Java add tables, nested tables in Word

[Example 2] was added nested tables

import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.TextRange;

public class NestedTable {
    public static void main(String[]args){
        //加载测试文档
        Document doc = new Document("sample.docx");

        //获取指定表格中的单元格,并设置行高、列宽
Section sec = doc.getSections().get(0);
        Table table = sec.getTables().get(0);
        table.getRows().get(0).setHeight(120f);
        table.getRows().get(0).getCells().get(0).setWidth(380);

        //添加嵌套表格到指定单元格
        Table nestedtable = table.get(0,0).addTable(true);
        nestedtable.getTableFormat().setHorizontalAlignment(RowAlignment.Center);//设置嵌套表格在单元格中的对齐方式
        nestedtable.resetCells(4,4);//指定嵌套表格行数、列数
        nestedtable.autoFit(AutoFitBehaviorType.Auto_Fit_To_Contents);//设置嵌套表格内容自适应方法
        //声明表格数组内容
        String[][] data ={
                new String[]{"编号","产区","最新型号","生产日期",},
                new String[]{"1","A","V2.2.0","2019-06-21"},
                new String[]{"2","B","V2.6.1","2019-06-18"},
                new String[]{"3","C","V2.6.2","2019-06-14"},
        };

        //填充数组内容到嵌套表格
        for (int i = 0; i < data.length; i++) {
            TableRow dataRow = nestedtable.getRows().get(i);
            dataRow.getCells().get(i).setWidth(160);
            dataRow.setHeight(25);
            dataRow.setHeightType(TableRowHeightType.Exactly);
            for (int j = 0; j < data[i].length; j++) {
                dataRow.getCells().get(j).getCellFormat().setVerticalAlignment(VerticalAlignment.Middle);
                TextRange range = dataRow.getCells().get(j).addParagraph().appendText(data[i][j]);
                range.getCharacterFormat().setFontName("楷体");
                range.getCharacterFormat().setFontSize(11f);
                range.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
            }
        }

        //保存文档
        doc.saveToFile("nesedtable1.docx",FileFormat.Docx_2010);
    }
}

Nested tables add effects:
Java add tables, nested tables in Word

(This article End)

Guess you like

Origin blog.51cto.com/eiceblue/2454497