bootstrap 黑边框表格样式_Java 设置Word表格边框

概述:

本文介绍通过Java程序设置Word表格边框的方法,设置边框时可对整个表格设置,也可对指定单元格设置,同时可对边框进行格式化设置设置,包括边框类型、样式、颜色、线条宽度等等,下面将分三个示例来展示如何设置边框效果:

1. 表格边框

1.1 对整个表格设置统一的边框样式

1.2 对表格指定边框设置样式

2. 单元格边框(指定单元格设置边框)


程序环境准备:

本文使用的是IDEA,并且需要使用到Word库(Free Spire.Doc for Java免费版),可手动下载并将lib文件夹夹下的Spire.Doc.jar导入java程序;如果想通过Maven仓库下载导入,可参考这里的jar导入方法。


Java 代码示例:

【示例1】对整个表格设置统一的边框样式

import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;


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

        //获取Section
        Section section = doc.getSections().get(0);

        //获取第一个表格
        Table table = section.getTables().get(0);

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

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

整体边框效果:

a7667eb756f934d40abba1d3916aaf44.png

【示例2】对表格指定边框设置样式

import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;

import java.awt.*;

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

        //获取Section
        Section section = doc.getSections().get(0);

        //获取第一个表格
        Table table = section.getTables().get(0);

        //设置上边框
        table.getTableFormat().getBorders().getTop().setBorderType(BorderStyle.Dot_Dash);
        table.getTableFormat().getBorders().getTop().setLineWidth(2f);
        table.getTableFormat().getBorders().getTop().setColor(Color.red);

        //设置右边框
        table.getTableFormat().getBorders().getRight().setBorderType(BorderStyle.Double);
        table.getTableFormat().getBorders().getRight().setLineWidth(2f);
        table.getTableFormat().getBorders().getRight().setColor(Color.green);

        //设置下边框
        table.getTableFormat().getBorders().getBottom().setBorderType(BorderStyle.None);

        //设置左边框
        table.getTableFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
        table.getTableFormat().getBorders().getLeft().setLineWidth(2f);
        table.getTableFormat().getBorders().getLeft().setColor(Color.blue);

        //设置垂直边框
        table.getTableFormat().getBorders().getVertical().setBorderType(BorderStyle.Dot);
        table.getTableFormat().getBorders().getVertical().setLineWidth(2f);
        table.getTableFormat().getBorders().getVertical().setColor(Color.orange);

        //设置水平边框
        table.getTableFormat().getBorders().getHorizontal().setBorderType(BorderStyle.Wave);
        table.getTableFormat().getBorders().getHorizontal().setLineWidth(2f);
        table.getTableFormat().getBorders().getHorizontal().setColor(Color.magenta);

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

指定边框设置效果:

e156f347e8020d9c684359d62585ff50.png

【实例3】指定单元格设置边框

import com.spire.doc.*;
import com.spire.doc.documents.BorderStyle;

import java.awt.*;

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

        //获取Section
        Section section = doc.getSections().get(0);

        //获取第一个表格
        Table table = section.getTables().get(0);

        //获取单元格,设置上、下边框
        TableCell cell1 = table.get(0,0);
        cell1.getCellFormat().getBorders().getTop().setBorderType(BorderStyle.Single);
        cell1.getCellFormat().getBorders().getTop().setLineWidth(2f);
        cell1.getCellFormat().getBorders().getTop().setColor(Color.red);
        cell1.getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dash_Dot_Stroker);
        cell1.getCellFormat().getBorders().getBottom().setLineWidth(2);
        cell1.getCellFormat().getBorders().getBottom().setColor(Color.pink);

        //获取单元格,设置左、右边框
        TableCell cell2 = table.get(1,1);
        cell2.getCellFormat().getBorders().getLeft().setBorderType(BorderStyle.Hairline);
        cell2.getCellFormat().getBorders().getLeft().setLineWidth(2);
        cell2.getCellFormat().getBorders().getLeft().setColor(Color.yellow);
        cell2.getCellFormat().getBorders().getRight().setBorderType(BorderStyle.Double);
        cell2.getCellFormat().getBorders().getRight().setLineWidth(2f);
        cell2.getCellFormat().getBorders().getRight().setColor(Color.magenta);

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

单元格边框设置效果:

0556e397fb2d223e2210fcd2cf048d7d.png

(完)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值