如何通过Java应用程序创建Word表格

表格,又称为表,既是一种可视化交流模式,又是一种组织整理数据的手段。人们在通讯交流、科学研究以及数据分析活动当中广泛采用着形形色色的表格。那么如何通过Java应用程序创建Word表格呢?别担心,本文将详细为您介绍通过Java应用程序创建Word表格。下面是我整理的思路以及具体步骤,并附上Java代码供大家参考。

使用工具: Free Spire.Doc for Java (免费版)

程序环境:

方法1:手动引入。将 Free Spire.Doc for Java 下载到本地,解压,找到lib文件夹下的Spire.Doc.jar文件。在IDEA中打开如下界面,将本地路径中的jar文件引入Java程序

方法2: 如果您想通过 Maven安装,则可以在 pom.xml 文件中添加以下代码导入 JAR 文件。

<repositories>

        <repository>

            <id>com.e-iceblue</id>

            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>

        </repository>

    </repositories>

<dependencies>

    <dependency>

        <groupId>e-iceblue</groupId>

        <artifactId>spire.doc.free</artifactId>

        <version>5.2.0</version>

    </dependency>

</dependencies>

具体步骤:

下表列出了一些负责创建和格式化表格的核心类和方法。

名称

描述

Table类

表示 Word 文档中的表格。

TableRow类

表示表中的一行。

TableCell类

表示表格中的特定单元格。

Section.addTbale()方法

将新表格添加到指定节。

Table.resetCells()方法

重置行号和列号。

Table.getRows()方法

获取表格行。

TableRow.setHeight() 方法

设置指定行的高度。

TableRow.getCells()方法

返回单元格集合。

TableRow.getFormat()方法

获取指定行的格式。

 

  •  创建一个 Document 对象,并向其添加一个节。
  • 准备标题行和其他行的数据,分别存储在一维字符串数组和二维字符串数组中。
  • 使用 Section.addTable() 方法将表格添加到节。
  • 将数据插入标题行,并设置行格式,包括行高、背景颜色和文本对齐方式。
  • 将数据插入其余行,并对这些行应用格式。
  • 使用 Document.saveToFile() 方法保存文件。

完整代码:

【Java】

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

import java.awt.*;

public class CreateTable {

    public static void main(String[] args) {

        //创建一个 Document 对象
        Document document = new Document();

        //添加一个节
        Section section = document.addSection();

        //定义表格数据
        String[] header = {"国家", "首都", "大陆", "国土面积", "人口数量"};
        String[][] data =
                {
                        new String[]{"玻利维亚", "拉巴斯", "南美", "1098575", "7300000"},
                        new String[]{"巴西", "巴西利亚", "南美", "8511196", "150400000"},
                        new String[]{"加拿大", "渥太华", "北美", "9976147", "26500000"},
                        new String[]{"智利”“圣地亚哥",  "南美", "756943", "13200000"},
                        new String[]{"哥伦比亚", "波哥大", "南美", "1138907", "33000000"},
                        new String[]{"古巴", "哈瓦那", "北美", "114524", "10600000"},
                        new String[]{"厄瓜多尔", "基多", "南美", "455502", "10600000"},
                        new String[]{"萨尔瓦多", "圣萨尔瓦多", "北美", "20865", "5300000"},
                        new String[]{ "圭亚那", "乔治城","南美", "214969", "800000"},

                };

        //添加表格
        Table table = section.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.gray);
        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 txtRange = p.appendText(header[i]);
            txtRange.getCharacterFormat().setBold(true);
        }

        //将数据添加到其余行
        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);
                dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);
            }
        }

        //设置单元格的背景颜色
        for (int j = 1; j < table.getRows().getCount(); j++) {
            if (j % 2 == 0) {
                TableRow row2 = table.getRows().get(j);
                for (int f = 0; f < row2.getCells().getCount(); f++) {
                    row2.getCells().get(f).getCellFormat().setBackColor(new Color(173, 216, 230));
                }
            }
        }

        //保存文件
        document.saveToFile("创建表格.docx", FileFormat.Docx_2013);
    }
}

效果图: