四时宝库

程序员的知识宝库

Java 创建、加载、操作和保存WPS文字(Word文档)

在日常工作、学习和生活中,我们经常会使用WPS文字(Word文档)这一款办公软件来对文档进行编辑处理。本文主要想介绍如何在后端通过使用Java代码的方式来创建、加载、操作和保存WPS文字(Word文档)。

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

Jar包导入:在安装配置好JDK和Intellij IDEA之后,需将以上使用工具中的Jar包导入IDEA。导入方式有两种:其一,在E-iceblue中文官网上获取产品包,解压后在lib文件夹下找到Spire.Pdf.jar,然后手动导入IDEA;其二,在IDEA中创建Maven项目,然后在Pom.xml文件下键入以下代码,最后点击“Import Changes”即可。

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

代码示例

示例一 创建WPS文字(Word文档)

在新建WPS文字时,Free Spire.Doc for Java支持指定文本内容,设置字符名称、字号、字体颜色,以及段落格式,段前缩进,断后间距等。

import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.HorizontalAlignment;
import com.spire.doc.documents.Paragraph;
import com.spire.doc.documents.ParagraphStyle;
import java.awt.*;
import java.io.*;

public class CreateWPS {
    public static void main(String[] args) throws IOException {
        //创建Document实例
        Document document = new Document();
        //添加节到文档
        Section section = document.addSection();

        //添加五个段落至该节
        Paragraph para1 = section.addParagraph();
        para1.appendText("断桥,又小雪");

        Paragraph para2 = section.addParagraph();
        para2.appendText("寂寞的夜里窗外下着雪传来一首曲凄凉而婉约;一个不眠之夜,心中的惆怅与谁言;深秋的夜,寂寞的夜,何处去寻花与蝶。");

        Paragraph para3 = section.addParagraph();
        para3.appendText("在这个不眠夜,孤身一人,惟有这小雪,陪我度过,心中的压抑与谁诉说。");

        Paragraph para4 = section.addParagraph();
        para4.appendText("晶莹的冰花扬扬洒洒的挥下,冲淡了最后的暖意,盖住了飘落的花瓣;晶莹的冰花凄凄惨惨的撒下,淹没了最后的喜悦," +
                "留下了暗淡的阴霾,它轻轻地飘着,逍遥地舞着,凝结在我的心田,亮晶晶地,美却略显凄凉。");

        Paragraph para5 = section.addParagraph();
        para5.appendText("清风散过,瓣瓣落,落尽处,泪滴荡空。点点粉嫩,是离情,别是一般眷恋绕心头。满地残落叶,余憔悴,怎堪回首," +
                "一切尽在无语凝噎中。冷冷凄凄,惨惨兮。");

        //将第一段作为标题,设置标题段落格式
        ParagraphStyle style1 = new ParagraphStyle(document);
        style1.setName("titleStyle");
        style1.getCharacterFormat().setBold(true);
        style1.getCharacterFormat().setTextColor(Color.BLUE);
        style1.getCharacterFormat().setFontName("Lucida Sans Unicode");
        style1.getCharacterFormat().setFontSize(12f);
        document.getStyles().add(style1);
        para1.applyStyle("titleStyle");
        //设置标题段落居中对齐
        para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);

        //设置其余四个段落的格式
        ParagraphStyle style2 = new ParagraphStyle(document);
        style2.setName("paraStyle");
        style2.getCharacterFormat().setFontName("Lucida Sans Unicode");
        style2.getCharacterFormat().setFontSize(11f);
        document.getStyles().add(style2);
        para2.applyStyle("paraStyle");
        para3.applyStyle("paraStyle");
        para4.applyStyle("paraStyle");
        para5.applyStyle("paraStyle");

        //设置第二、三、四、五段落的段首缩进
        para2.getFormat().setFirstLineIndent(25f);
        para3.getFormat().setFirstLineIndent(25f);
        para4.getFormat().setFirstLineIndent(25f);
        para5.getFormat().setFirstLineIndent(25f);

        //设置第一段落的段后间距
        para1.getFormat().setAfterSpacing(10f);

        //保存文档
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        document.saveToStream(bos, FileFormat.Doc);

        //将流写入WPS文件
        FileOutputStream fos = new FileOutputStream("output/CreateWpsWord.wps");
        fos.write(bos.toByteArray());
        //关闭流
        bos.close();
        fos.close();
    }
}

结果文档

示例二 加载、操作和保存WPS文字(Word文档)

我将上示例的结果文档保存至本地,接着用以下代码加载文档,并给文档正文第一段设置背景颜色,最后再保存为WPS文字文档。

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 LoadAndEditWpsWord {
    public static void main(String[] args) throws IOException {
        //通过流加载WPS文字文档
        FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\Test1\\Desktop\\CreateWpsWord.wps"));
        Document document = new Document();
        document.loadFromStream(inputStream, FileFormat.Doc);

        //获取文档的第一个节
        Section section = document.getSections().get(0);
        //获取该节中第二个段落
        Paragraph paragraph = section.getParagraphs().get(1);
        //给该段落设置背景颜色
        paragraph.getFormat().setBackColor(Color.pink);

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

设置效果

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言