java word表格_Java 在Word中创建表格

2023-10-30 22:50
文章标签 java 表格 word 创建表格

本文主要是介绍java word表格_Java 在Word中创建表格,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

表格作为一种可视化交流模式及组织整理数据的手段,在各种场合及文档中应用广泛。常见的表格可包含文字、图片等元素,我们操作表格时可以插入图片、写入文字及格式化表格样式等。下面,将通过Java编程在Word文档中创建表格并实现格式化操作,包括设置字体、字号、字体颜色、字体粗细等,设置单元格对齐方式、单元格背景色、单元格合并、设置表格边框样式、插入图片等。

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

Jar文件导入

方法1:首先通过

导入步骤:在程序中新建一个directory目录,并命名(本示例中命名为lib);将控件包lib文件夹下的Spire.Doc.jar文件(如下图1)复制到程序中新建的目录下。复制jar文件后,鼠标右键点击jar文件,选择”Add as Library”。完成导入(如下图2)。

图1:

6cfe2366f3506bdc772a68797e0e7359.png

图2:

daa29a0c20ab265adcc718756d174d4f.png

方法2:通过maven导入。参考

Java代码示例(供参考)

Step 1: 创建文档

Document doc = newDocument();

Section sec= doc.addSection();

Step 2:声明数组内容

//声明数组内容

String[] header = {"班级","姓名","性别", "学号", "专业成绩"};

String[][] data={new String[]{"一班","王丽", "女", "Y1256486", "138"},new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},new String[]{"二班","刘洋", "男", "B1546258", "141"},new String[]{"三班","冯刚", "男", "B1542367", "136"},new String[]{"三班","刘思源", "男", "Z1263547", "133"},

};

Step 3:添加表格并写入数据

//添加表格

Table table = sec.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.ORANGE);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 range1=p.appendText(header[i]);

range1.getCharacterFormat().setFontName("Arial");

range1.getCharacterFormat().setFontSize(12f);

range1.getCharacterFormat().setBold(true);

range1.getCharacterFormat().setTextColor(Color.white);

}//写入剩余组内容到表格,并格式化数据

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);

TextRange range2=dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);

range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

range2.getCharacterFormat().setFontName("Arial");

range2.getCharacterFormat().setFontSize(10f);

}

}

Step 4:合并单元格

table.applyVerticalMerge(0,1,2);

table.applyVerticalMerge(0,4,5);

Step 5:插入图片到单元格

DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");

dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

Step 6:设置单元格背景色

for (int j = 1; j < table.getRows().getCount(); j++) {if (j % 2 == 0) {

TableRow row2=table.getRows().get(j);for (int f = 1; f < row2.getCells().getCount(); f++) {

row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));

}

}

}

Step 7:设置表格边框样式

table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);

Step 8: 保存文档

doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);

表格创建效果:

1530d30dc985070a2b131b87ec4d73fe.png

全部代码:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

import com.spire.doc.*;import com.spire.doc.documents.*;importcom.spire.doc.fields.DocPicture;importcom.spire.doc.fields.TextRange;import java.awt.*;public classCreateTable {public static voidmain(String[] args){//创建Document对象

Document doc = newDocument();

Section sec=doc.addSection();//声明数组内容

String[] header = {"班级","姓名","性别", "学号", "专业成绩"};

String[][] data={new String[]{"一班","王丽", "女", "Y1256486", "138"},new String[]{"一班","洪菲菲", "女", "Y5425875", "134"},new String[]{"二班","刘洋", "男", "B1546258", "141"},new String[]{"三班","冯刚", "男", "B1542367", "136"},new String[]{"三班","刘思源", "男", "Z1263547", "133"},

};//添加表格

Table table = sec.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.ORANGE);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 range1=p.appendText(header[i]);

range1.getCharacterFormat().setFontName("Arial");

range1.getCharacterFormat().setFontSize(12f);

range1.getCharacterFormat().setBold(true);

range1.getCharacterFormat().setTextColor(Color.white);

}//写入剩余组内容到表格,并格式化数据

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);

TextRange range2=dataRow.getCells().get(c).addParagraph().appendText(data[r][c]);

range2.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

range2.getCharacterFormat().setFontName("Arial");

range2.getCharacterFormat().setFontSize(10f);

}

}//纵向合并指定单元格

table.applyVerticalMerge(0,1,2);

table.applyVerticalMerge(0,4,5);//插入图片到指定单元格

DocPicture dp = table.getRows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");

dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);//设置单元格背景颜色

for (int j = 1; j < table.getRows().getCount(); j++) {if (j % 2 == 0) {

TableRow row2=table.getRows().get(j);for (int f = 1; f < row2.getCells().getCount(); f++) {

row2.getCells().get(f).getCellFormat().setBackColor(new Color(144,238,144));

}

}

}//设置表格边框样式

table.getTableFormat().getBorders().setBorderType(BorderStyle.Thick_Thin_Large_Gap);//保存文档

doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);

}

}

View Code

(本文完)

转载请注明出处!

这篇关于java word表格_Java 在Word中创建表格的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/311079

相关文章

eclipse如何运行springboot项目

《eclipse如何运行springboot项目》:本文主要介绍eclipse如何运行springboot项目问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目js录当在eclipse启动spring boot项目时出现问题解决办法1.通过cmd命令行2.在ecl

Java中的Closeable接口及常见问题

《Java中的Closeable接口及常见问题》Closeable是Java中的一个标记接口,用于表示可以被关闭的对象,它定义了一个标准的方法来释放对象占用的系统资源,下面给大家介绍Java中的Clo... 目录1. Closeable接口概述2. 主要用途3. 实现类4. 使用方法5. 实现自定义Clos

Jvm sandbox mock机制的实践过程

《Jvmsandboxmock机制的实践过程》:本文主要介绍Jvmsandboxmock机制的实践过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、背景二、定义一个损坏的钟1、 Springboot工程中创建一个Clock类2、 添加一个Controller

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

Python实现pdf电子发票信息提取到excel表格

《Python实现pdf电子发票信息提取到excel表格》这篇文章主要为大家详细介绍了如何使用Python实现pdf电子发票信息提取并保存到excel表格,文中的示例代码讲解详细,感兴趣的小伙伴可以跟... 目录应用场景详细代码步骤总结优化应用场景电子发票信息提取系统主要应用于以下场景:企业财务部门:需

MQTT SpringBoot整合实战教程

《MQTTSpringBoot整合实战教程》:本文主要介绍MQTTSpringBoot整合实战教程,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录MQTT-SpringBoot创建简单 SpringBoot 项目导入必须依赖增加MQTT相关配置编写

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Spring Security介绍及配置实现代码

《SpringSecurity介绍及配置实现代码》SpringSecurity是一个功能强大的Java安全框架,它提供了全面的安全认证(Authentication)和授权(Authorizatio... 目录简介Spring Security配置配置实现代码简介Spring Security是一个功能强

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur