本文将介绍如何使用Spire.Doc for Java给Word文档中的表格设置样式和边框。
原Word文档:

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){
        //创建Document实例
        Document document = new Document();
        //加载Word文档
        document.loadFromFile("tableSample.docx");
        //获取第一个section
        Section section = document.getSections().get(0);
        //获取section中第一个表格
        Table table = section.getTables().get(0);
        //给表格应用样式
        table.applyStyle(DefaultTableStyle.Colorful_List);
        //设置表格的右边框
        table.getFormat().getBorders().getRight().setBorderType(BorderStyle.Hairline);
        table.getFormat().getBorders().getRight().setLineWidth(1.0F);
        table.getFormat().getBorders().getRight().setColor(Color.RED);
        //设置表格的顶部边框
        table.getFormat().getBorders().getTop().setBorderType(BorderStyle.Hairline);
        table.getFormat().getBorders().getTop().setLineWidth(1.0F);
        table.getFormat().getBorders().getTop().setColor(Color.GREEN);
        //设置表格的左边框
        table.getFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
        table.getFormat().getBorders().getLeft().setLineWidth(1.0F);
        table.getFormat().getBorders().getLeft().setColor(Color.YELLOW);
        //设置表格的底部边框
        table.getFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);
        //设置表格的水平和垂直边框
        table.getFormat().getBorders().getVertical().setBorderType(BorderStyle.Dot);
        table.getFormat().getBorders().getHorizontal().setBorderType(BorderStyle.None);
        table.getFormat().getBorders().getVertical().setColor(Color.ORANGE);
        //保存结果文档
        document.saveToFile("setTableStyleAndBorder.docx", FileFormat.Docx_2013);
    }
}
结果文档:

    


					



