0
点赞
收藏
分享

微信扫一扫

如何通过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程序

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

方法 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>

具体步骤:

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

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

  • 创建一个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);
}
}

效果图:

如何通过Java应用程序创建Word表格_数据_03

举报

相关推荐

0 条评论