---
title: "Java 加载、操作和保存WPS文字文档"
author: "Miaonly"
date: "2021-07-27T09:51:06Z"
tags: []
categories: ["编程语言"]
slug: "java-wps"
draft: false
lastmod: "2021-07-27T09:51:06Z"
id: 2520547
url: "https://miaonly.iteye.com/blog/2520547"
---

本文通过Java程序代码来展示如何来加载、操作及保存WPS格式的文字文档。

**一、基本步骤：**加载时，通过流加载WPS文字文档，完成相关文字操作后，再将结果文档保存到流，将流写入WPS文档，关闭流。

**二、程序环境**

这里使用的是Word类库工具 **[Free Spire.Doc for Java](https://www.e-iceblue.cn/Introduce/Free-Spire-Doc-JAVA.html)**，可通过在pom.xml中配置Maven仓库路径并指定spire.doc.free的依赖，具体配置内容如下：

```
<repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
<dependencies>
    <dependency>
        <groupId> e-iceblue </groupId>
        <artifactId>spire.doc.free</artifactId>
        <version>3.9.0</version>
    </dependency>
</dependencies>
```

 

 **【Java代码】**

这里对WPS文字文档的操作进行了段落背景设置、文字高亮、加粗、添加图片等简单操作。

```
import com.spire.doc.*;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.documents.TextWrappingStyle;
import com.spire.doc.fields.DocPicture;

import java.awt.*;
import java.io.*;

public class OperateWPS_Word {
    public static void main(String[] args)throws IOException {
        //通过流加载WPS文字文档
        FileInputStream inputStream = new FileInputStream(new File("test.wps"));
        Document document = new Document();
        document.loadFromStream(inputStream, FileFormat.Doc);

        //查找所有“北京冬奥会”文本
        TextSelection[] textSelections = document.findAllString("北京冬奥会", false, false);
        //设置文本高亮色、加粗
        for (TextSelection selection : textSelections)
        {
            selection.getAsOneRange().getCharacterFormat().setHighlightColor(Color.YELLOW);
            selection.getAsOneRange().getCharacterFormat().setBold(true);
        }

        //获取文档的第一个节
        Section section = document.getSections().get(0);

        //获取第2段，设置段落背景色
        Paragraph paragraph1 = section.getParagraphs().get(1);
        paragraph1.getFormat().setBackColor(new Color(176,224,230));
        paragraph1.getStyle().getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //获取第3段，添加图片到段落
        Paragraph paragraph2 = section.getParagraphs().get(2);
        DocPicture picture = paragraph2.appendPicture("img.png");
        picture.setWidth(200f);
        picture.setHeight(250f);
        picture.setTextWrappingStyle(TextWrappingStyle.Through);


        //将结果文档保存到流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        document.saveToStream(bos, FileFormat.Doc);
        //将流写入WPS文档
        FileOutputStream fos = new FileOutputStream("Output.wps");
        fos.write(bos.toByteArray());
        //关闭流
        bos.close();
        fos.close();
    }
}
```

 

关于更多使用Java如何操作Word文档的方法，可前往：[https://miaonly.iteye.com/admin/categories/383041](/admin/blogs/%E5%85%B3%E4%BA%8E%E4%BD%BF%E7%94%A8Java%E5%A6%82%E4%BD%95%E6%93%8D%E4%BD%9CWord%E6%96%87%E6%A1%A3%E7%9A%84%E6%9B%B4%E5%A4%9A%E6%96%B9%E6%B3%95%EF%BC%8C%E5%8F%AF%E5%89%8D%E5%BE%80%EF%BC%9A)

 

 

—End—