1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java表格边框问题_Java 设置Word表格边框

java表格边框问题_Java 设置Word表格边框

时间:2018-08-06 12:17:44

相关推荐

java表格边框问题_Java 设置Word表格边框

概述

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

表格边框

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

1.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_);

doc.dispose();

}

}

整体边框效果:

【示例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_);

doc.dispose();

}

}

指定边框设置效果:

【实例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_);

doc.dispose();

}

}

单元格边框设置效果:

(完)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。