在 Word 文档中,灵活地添加、修改和移除表格边框能够显著提升文档的可读性和专业度。首先,定制边框样式可以让重要信息更加突出,帮助读者快速定位关键数据或段落,增强视觉效果。其次,通过调整边框线的粗细、颜色和样式,可以实现更精细的设计控制,使文档风格统一且美观。最后,移除不必要的边框有助于减少视觉杂乱,让页面布局更加简洁明了,提高阅读体验。本文将介绍如何使用 Spire.Doc for Java 在 Java 项目中添加、修改和移除 Word 表格边框。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 这个链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
Java 添加 Word 表格边框
为了给整个表格中的所有单元格统一添加边框,需要逐一访问每个单元格,并对其边框属性进行可视化设置。以下是详细的步骤:
- 创建一个Document 对象。
 - 使用 Document.loadFromFile() 方法加载一个文档。
 - 使用 Document.getSections().get(0) 获取文档的第一个节。
 - 使用 Section.getTables().get(0) 获取节中的第一个表格。
 - 使用 for 循环遍历表格中的所有单元格。
 - 设置 TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single),即设置单元格边框为单线样式。
 - 设置 TableCell.getCellFormat().getBorders().setLineWidth(1f),即定义边框的宽度为 1pt。
 - 设置 TableCell.getCellFormat().getBorders().setColor(Color.black),即设置边框颜色为黑色。
 - 使用 Document.saveToFile() 方法保存到文档。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;
public class AddBorder {
    public static void main(String[] args) {
        // 创建一个新的Document对象
        Document doc = new Document();
        // 从文件加载文档
        doc.loadFromFile("表格示例1.docx");
        // 获取文档的第一个节
        Section section = doc.getSections().get(0);
        // 获取该节中第一个表格
        Table table = section.getTables().get(0);
        // 声明TableRow和TableCell变量,以便在循环中使用
        TableRow tableRow;
        TableCell tableCell;
        // 遍历表格的所有行
        for (int i = 0; i < table.getRows().getCount(); i++) {
            // 获取当前行
            tableRow = table.getRows().get(i);
            // 遍历当前行的所有单元格
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // 获取当前单元格
                tableCell = tableRow.getCells().get(j);
                // 设置当前单元格的边框样式为单线
                tableCell.getCellFormat().getBorders().setBorderType(BorderStyle.Single);
                // 设置边框的宽度
                tableCell.getCellFormat().getBorders().setLineWidth(1f);
                // 设置边框的颜色
                tableCell.getCellFormat().getBorders().setColor(Color.black);
            }
        }
        // 将修改后的文档保存为新的文件
        doc.saveToFile("添加单元格边框.docx", FileFormat.Docx_2016);
        // 关闭文档,释放资源
        doc.close();
    }
}

Java 修改 Word 表格边框
Spire.Doc 赋予用户丰富的边框自定义选项,通过调整如 TableCell.getCellFormat().getBorders().getBottom().setBorderType() 来选择边框样式,TableCell.getCellFormat().getBorders().getBottom().setLineWidth() 以设定边框厚度,以及 TableCell.getCellFormat().getBorders().getBottom().setColor() 来指定边框色彩,可以根据个人或项目需求,精细化设计文档中表格的边框细节。以下是详细的步骤:
- 创建一个 Document 对象。
 - 使用 Document.loadFromFile() 方法加载一个文档。
 - 使用 Document.getSections().get(0) 获取文档的第一个节。
 - 使用 Section.getTables().get(0) 获取节中的第一个表格。
 - 使用 for 循环遍历表格中的需要更改边框样式的单元格。
 - 通过 TableCell.getCellFormat().getBorders().getBottom().setColor(Color.ORANGE) 更改右边框的颜色为橘色。
 - 通过 TableCell.getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash) 更改右边框的样式为点划线。
 - 通过 TableCell.getCellFormat().getBorders().getBottom().setLineWidth(2) 更改右边框的宽度为 2pt。
 - 使用 Document.saveToFile() 方法保存到文档。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
import java.awt.*;
public class ModifyBorder {
    public static void main(String[] args) {
        // 创建一个新的Document对象
        Document doc = new Document();
        // 从文件加载文档
        doc.loadFromFile("表格示例2.docx");
        // 获取文档的第一个节
        Section section = doc.getSections().get(0);
        // 获取该节中第一个表格
        Table table = section.getTables().get(0);
        // 声明TableRow,以便在循环中使用
        TableRow tableRow;
        // 遍历表格的所有行
        for (int i = 1; i < table.getRows().getCount() - 1; i++) {
            tableRow = table.getRows().get(i);
            // 设置当前单元格的边框颜色
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setColor(Color.ORANGE);
            // 设置当前单元格的边框样式为点线
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setBorderType(BorderStyle.Dot_Dash);
            // 设置边框的宽度
tableRow.getCells().get(1).getCellFormat().getBorders().getBottom().setLineWidth(2);
        }
        // 将修改后的文档保存为新的文件
        doc.saveToFile("修改单元格边框.docx", FileFormat.Docx_2016);
        // 关闭文档,释放资源
        doc.close();
    }
}

Java 移除 Word 表格边框
在编辑 Word 文档时,边框设计的灵活性不仅限于整个表格层面,更延伸至每个独立单元格,允许进行细致入微的个性化调整。若目标为全面消除表格内外的所有边框痕迹,推荐采用分阶段实施的方法:第一步,从宏观角度出发,清除表格整体的边框样式;紧接着,进入微观操作阶段,遍历表格中的每一个单元格,逐一撤销其特有的边框设置。以下是详细的步骤:
- 创建一个 Document 对象。
 - 使用 Document.loadFromFile() 方法加载一个文档。
 - 使用 Document.getSections().get(0) 获取文档的第一个节。。
 - 使用 Section.getTables().get(0) 获取节中的第一个表格。
 - 使用 for 循环遍历表格中的所有单元格。
 - 使用 Table.getTableFormat().getBorders().setBorderType(BorderStyle.None) 对表格进行边框去除设置。
 - 使用 TableCell.getCellFormat().getBorders().setBorderType(BorderStyle.None) 对单元格进行边框去除设置。
 - 使用 Document.saveToFile() 方法保存到文档。
 
- Java
 
import com.spire.doc.*;
import com.spire.doc.documents.*;
public class RemoveBorder {
    public static void main(String[] args) {
        // 创建一个新的Document对象
        Document doc = new Document();
        // 从文件加载文档
        doc.loadFromFile("表格示例2.docx");
        // 获取文档的第一个节
        Section section = doc.getSections().get(0);
        // 获取该节中第一个表格
        Table table = section.getTables().get(0);
        // 移除表格设置的边框
        table.getFormat().getBorders().setBorderType(BorderStyle.None);
        // 声明TableRow,以便在循环中使用
        TableRow tableRow;
        // 遍历表格的所有行
        for (int i = 0; i < table.getRows().getCount(); i++) {
            tableRow = table.getRows().get(i);
            for (int j = 0; j < tableRow.getCells().getCount(); j++) {
                // 移除单元格设置的所有边框
                tableRow.getCells().get(j).getCellFormat().getBorders().setBorderType(BorderStyle.None);
            }
        }
        // 将修改后的文档保存为新的文件
        doc.saveToFile("移除单元格边框.docx", FileFormat.Docx_2016);
        // 关闭文档,释放资源
        doc.close();
    }
}

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。
    


					



