WPS Office 是目前国内常用的办公软件之一,包含 WPS 文字、WPS 表格、WPS 演示等模块,可以实现查看和编辑文字、表格、演示文档等功能。在这篇文章中,我们将介绍如何使用 Spire.Doc for Java 在 Java 应用程序中加载,操作和保存 WPS 文字文档。
安装 Spire.Doc for Java
首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件:
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>13.10.6</version>
    </dependency>
</dependencies>
从本地加载、操作和保存 WPS 文字文档
主要步骤如下:
- 创建 Document 实例并通过 Document.loadFromFile(String, FileFormat.WPS) 方法从本地加载 WPS 文字文档。
 - 使用 Document.getSections().get(sectionIndex) 方法获取文档的指定节。
 - 使用 Section.getParagraphs().get(paragraphIndex) 方法获取该节中的指定段落。
 - 使用 Paragraph.getFormat().setBackColor() 方法给该段落设置背景颜色。
 - 使用 Document.saveToFile(String, FileFormat.WPS) 方法保存结果文档。
 
- Java
 
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import java.awt.*;
import java.io.*;
public class LoadWpsWordFromFile {
    public static void main(String[] args) throws IOException {
        //加载WPS文字文档
        Document document = new Document();
        document.loadFromFile("Sample.wps", FileFormat.WPS);
        
        //获取文档的第一个节
        Section section = document.getSections().get(0);
        //获取该节中第一个段落
        Paragraph paragraph = section.getParagraphs().get(0);
        //给该段落设置背景颜色
        paragraph.getFormat().setBackColor(Color.pink);
        
        //保存结果文档
        document.saveToFile("Output.wps", FileFormat.WPS);
    }
}从流加载、操作和保存 WPS 文字文档
主要步骤如下:
- 使用 FileInputStream 类将 WPS 文字文档读取到流。
 - 创建 Document 实例并通过 Document.loadFromStream(InputStream, FileFormat.WPS) 方法加载流。
 - 使用 Document.getSections().get(sectionIndex) 方法获取文档的指定节。
 - 使用 Section.getParagraphs().get(paragraphIndex) 方法获取该节中的指定段落。
 - 使用 Paragraph.getFormat().setBackColor() 方法给该段落设置背景颜色。
 - 创建 ByteArrayOutputStream 实例并通过 Document.saveToStream(OutputStream, FileFormat.WPS) 方法将结果文档保存到流。
 - 创建 FileOutputStream 实例并通过 FileOutputStream.write() 方法将流写入 WPS 文字文档。
 
- Java
 
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import java.awt.*;
import java.io.*;
public class LoadWpsWordFromStream {
        //通过流加载WPS文字文档
        FileInputStream inputStream = new FileInputStream(new File("Sample.wps"));
        Document document = new Document();
        document.loadFromStream(inputStream, FileFormat.WPS);
        //获取文档的第一个节
        Section section = document.getSections().get(0);
        //获取该节中第一个段落
        Paragraph paragraph = section.getParagraphs().get(0);
        //给该段落设置背景颜色
        paragraph.getFormat().setBackColor(Color.pink);
        //将结果文档保存到流
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        document.saveToStream(bos, FileFormat.WPS);
        //将流写入WPS文字文档
        FileOutputStream fos = new FileOutputStream("Output.wps");
        fos.write(bos.toByteArray());
        //关闭流
        bos.close();
        fos.close();
    }
}原 WPS 文字文档:

结果文档:

申请临时 License
如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请 该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。 获取有效期 30 天的临时许可证。
    


					



