Java在Word中创建表格
人生爱情感言表格作为⼀种可视化交流模式及组织整理数据的⼿段,在各种场合及⽂档中应⽤⼴泛。常见的表格可包含⽂字、图⽚等元素,我们操作表格时可以插⼊图⽚、写⼊⽂字及格式化表格样式等。下⾯,将通过Java编程在Word⽂档中创建表格并实现格式化操作,包括设置字体、字号、字体颜⾊、字体粗细等,设置单元格对齐⽅式、单元格背景⾊、单元格合并、设置表格边框样式、插⼊图⽚等。
使⽤⼯具:Free Spire.Doc for Java 2.0.0 (免费版)
Jar⽂件导⼊
⽅法1:⾸先通过jar包。下载控件包并解压。
导⼊步骤:在程序中新建⼀个directory⽬录,并命名(本⽰例中命名为lib);将控件包lib⽂件夹下的Spire.Doc.jar⽂件(如下图1)复制到程序中新建的⽬录下。复制jar⽂件后,⿏标右键点击jar⽂件,选择”Add as Library”。完成导⼊(如下图2)。
图1:
图2:
⽅法2:通过maven导⼊。参考。
Java代码⽰例(供参考)
Step 1: 创建⽂档
Document doc = new Document();
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);
//设置表格第⼀⾏作为表头,写⼊表头数组内容,并格式化表头数据
TableRow row = Rows().get(0);
row.isHeader(true);
岳阳旅游景点row.setHeight(20);
row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) {
Paragraph p = Cells().get(i).addParagraph();
TextRange range1 = p.appendText(header[i]);
}
//写⼊剩余组内容到表格,并格式化数据
for (int r = 0; r < data.length; r++) {
TableRow dataRow = Rows().get(r + 1);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
for (int c = 0; c < data[r].length; c++) {
TextRange range2 = Cells().get(c).addParagraph().appendText(data[r][c]);
}
}
Step 4:合并单元格
table.applyVerticalMerge(0,1,2);
table.applyVerticalMerge(0,4,5);
Step 5:插⼊图⽚到单元格
DocPicture dp = Rows().get(1).getCells().get(0).addParagraph().appendPicture("1.png"); dp.getOwnerParagraph().getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
Step 6:设置单元格背景⾊
for (int j = 1; j < Rows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = Rows().get(j);
for (int f = 1; f < Cells().getCount(); f++) {
}
}
}
Step 7:设置表格边框样式
Step 8: 保存⽂档
doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
表格创建效果:
全部代码:
import com.spire.doc.*;
import com.spire.doc.documents.*;
import com.spire.doc.fields.DocPicture;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class CreateTable {
摩的组词public static void main(String[] args){
//创建Document对象
Document doc = new Document();
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);
//设置表格第⼀⾏作为表头,写⼊表头数组内容,并格式化表头数据
TableRow row = Rows().get(0);
row.isHeader(true);
row.setHeight(20);
row.setHeightType(TableRowHeightType.Exactly);
for (int i = 0; i < header.length; i++) {
TextRange range1 = p.appendText(header[i]);
陈乔恩最新电视剧
}
//写⼊剩余组内容到表格,并格式化数据
for (int r = 0; r < data.length; r++) {
TableRow dataRow = Rows().get(r + 1);
dataRow.setHeight(25);
dataRow.setHeightType(TableRowHeightType.Exactly);
for (int c = 0; c < data[r].length; c++) {
TextRange range2 = Cells().get(c).addParagraph().appendText(data[r][c]);
}
}
//纵向合并指定单元格
table.applyVerticalMerge(0,1,2);
table.applyVerticalMerge(0,4,5);
//插⼊图⽚到指定单元格
DocPicture dp = Rows().get(1).getCells().get(0).addParagraph().appendPicture("1.png");
//设置单元格背景颜⾊
for (int j = 1; j < Rows().getCount(); j++) {
if (j % 2 == 0) {
TableRow row2 = Rows().get(j);
for (int f = 1; f < Cells().getCount(); f++) {
}
}
}
//设置表格边框样式
//保存⽂档
doc.saveToFile("CreateTable.docx", FileFormat.Docx_2013);
}
}
View Code
(本⽂完)
转载请注明出处!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论