动态生成word文档原来如此简单!Aspose.Words助力以Java编程方式创建丰富的Word文档

时间:2024-05-18 18:57:09

近年来,Word文档的动态生成已成为组成报告,报价,发票和其他类型文档的流行功能。各种制造公司都基于数据库中存储的数据生成发票。在这种情况下,文档自动化可以节省手动文档创建过程中所需的时间,精力和资源。

本文旨在针对文档自动化过程,介绍如何以Java编程方式创建丰富的Word文档。如果想要测试这项新功能,可点击此处下载最新版试用。

在以下各节中,您将学习如何使用Java以编程方式创建包含不同元素(例如文本,段落,表格,列表,图像等)的Word文档。

①使用Java创建Word文档

大多数情况下,Word文档中相当一部分内容是基于文本的。因此,我们将通过创建带有标题和段落的Word文档来开始我们的旅程。以下是使用Aspose.Words for Java执行此操作的步骤:

  • 创建一个Document类的对象。
  • 创建一个DocumentBuilder类的对象,并使用Document对象对其进行初始化。
  • 创建一个Font类的对象,并设置字体大小,字体等。
  • 使用ParagraphFormat类设置段落的属性。
  • 使用DocumentBuilder.write()方法将文本写入文档。
  • 调用Document.save()方法创建文档。

下面的代码示例演示如何创建包含Java中文本的Word文档。

// Create a Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify font formatting
Font font = builder.getFont();
font.setSize(18);
font.setBold(true);
font.setColor(Color.BLACK);
font.setName("Arial");
builder.write("How to Create a Rich Word Document?");
builder.insertBreak(BreakType.LINE_BREAK);
// Start the paragraph
font.setSize(12);
font.setBold(false);
ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.setFirstLineIndent(12);
paragraphFormat.setKeepTogether(true);
builder.write("This article shows how to create a Word document containing text, images and lists.");
// Save the document
doc.save("Rich Word Document.docx");

输出结果:

动态生成word文档原来如此简单!Aspose.Words助力以Java编程方式创建丰富的Word文档

②使用Java在Word文档中创建表

Word文档中的表用于以行和列的形式组织内容。在本节中,我们将创建一个包含两行两列的简单表。创建表包括四个基本操作:

  • 启动表
  • 插入细胞
  • 结束行
  • 结束表

以下是在Word文档中创建表的步骤:

  • 创建一个Document类的对象。
  • 创建一个DocumentBuilder类的对象,并使用Document对象对其进行初始化
  • 使用Table类创建一个表。
  • 使用DocumentBuilder.insertCell()方法插入一个单元格。
  • 根据您的要求设置表的属性。
  • 使用DocumentBuilder.write()方法将文本添加到单元格中。
  • 分别使用DocumentBuilder.endRow()和DocumentBuilder.endTable()方法结束行和表。
  • 保存文档。

下面的示例演示如何在Java中的Word文档中创建表。

// Create a Document object
Document doc = new Document();
// Create a DocumentBuilder object
DocumentBuilder builder = new DocumentBuilder(doc);
// Create table
Table table = builder.startTable();
// Insert a cell
builder.insertCell();
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);
builder.write("This is Row 1 Cell 1");
builder.insertCell();
builder.write("This is Row 1 Cell 2");
// End row
builder.endRow();
// start a next row and set its properties
builder.getRowFormat().setHeight(100);
builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);
builder.insertCell();
builder.write("This is Row 2 Cell 1");
builder.insertCell();
builder.write("This is Row 2 Cell 2");
builder.endRow();
// End table
builder.endTable();
// Save the document
doc.save("Rich Word Document.docx");

输出结果:

动态生成word文档原来如此简单!Aspose.Words助力以Java编程方式创建丰富的Word文档

③使用Java在Word文档中创建列表

以下是将列表添加到Word文档的步骤。

  • 创建一个Document类的对象。
  • 使用Document.getLists()。add()方法将所需的列表类型添加到文档中。
  • 将列表从文档中获取到List对象中。
  • 使用DocumentBuilder对象填充列表。
  • 保存文档。

下面的代码示例演示如何使用Java在Word文档中创建列表。

// Create a Document object
Document doc = new Document();
doc.getLists().add(ListTemplate.BULLET_CIRCLE);
List list = doc.getLists().get(0);
// Set true to specify that the list has to be restarted at each section.
list.isRestartAtEachSection(true);
DocumentBuilder builder = new DocumentBuilder(doc);
builder.getListFormat().setList(list);
for (int i = 1; i < 45; i++) { builder.writeln(String.format("List Item " + i)); // Insert section break. if (i == 15) builder.insertBreak(BreakType.SECTION_BREAK_NEW_PAGE); } builder.getListFormat().removeNumbers(); // Save the document doc.save("Rich Word Document.docx");

输出结果:

动态生成word文档原来如此简单!Aspose.Words助力以Java编程方式创建丰富的Word文档

④使用Java将图像插入Word文档

将图像插入Word文档就像饼图一样简单。以下是执行此操作的一些简单步骤:

  • 创建一个Document类的对象。
  • 创建一个DocumentBuilder类的对象,并使用Document对象对其进行初始化。
  • 使用DocumentBuilder.insertImage()方法插入图像。
  • 保存文档。

下面的代码示例演示如何使用Java将图像插入Word文档。

// Create a Document object
Document doc = new Document();
// Create DocumentBuiler
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert Image
builder.insertImage("aspose-words.png");
// Save the document
doc.save("Rich Word Document.docx");

输出结果:

动态生成word文档原来如此简单!Aspose.Words助力以Java编程方式创建丰富的Word文档

 


如果您有任何疑问或需求,请随时加入Aspose技术交流群(642018183),我们很高兴为您提供查询和咨询