网站首页 > 编程文章 正文
在日常工作、学习和生活中,我们经常会使用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();
}
}
设置效果
猜你喜欢
- 2024-09-28 WPS Office 2019 For Linux 个人版发布——从未有广告!
- 2024-09-28 Linux系统中WPS文字加粗后产生黑块问题
- 2024-09-28 word文档乱码、丢失、无法打开,教你一个妙招,3秒立刻恢复
- 2024-09-28 Word 中如何设置文本白色突出显示,这里我总结了 4 种方法
- 2024-09-28 WPS Office 2019上架Windows 10应用商店
- 2024-09-28 看完WPS 2019这些功能,你还会花钱用Office吗?网友:要好好想想
- 2024-09-28 课程丨WPS 文字的文字设置详解一:“字体”设置技巧
- 2024-09-28 赛效:wps下载手机版怎么操作的?(手机版wps下载官方2019)
- 2024-09-28 如何把一个字放大,占满整张A4纸?|特大字号的设...
- 2024-09-28 无需下载第三方工具,WPS也可以把图片中的文字...
你 发表评论:
欢迎- 05-142014年最流行前端开发框架对比评测
- 05-14七爪源码:如何使用 Next.js 构建 Shopify 店面
- 05-14Web 前端怎样入门?
- 05-14我为什么不建议你使用框架
- 05-14推荐几个好用的React UI 框架
- 05-14PDFsharp:强大的 .NET 跨平台 PDF 处理库
- 05-14一组开源免费的Web动画图标,荐给需要的设计师和程序员
- 05-14salesforce 零基础学习(二十九)Record Types简单介绍
- 最近发表
- 标签列表
-
- spire.doc (59)
- system.data.oracleclient (61)
- 按键小精灵源码提取 (66)
- pyqt5designer教程 (65)
- 联想刷bios工具 (66)
- c#源码 (64)
- graphics.h头文件 (62)
- mysqldump下载 (66)
- sqljdbc4.jar下载 (56)
- libmp3lame (60)
- maven3.3.9 (63)
- 二调符号库 (57)
- 苹果ios字体下载 (56)
- git.exe下载 (68)
- diskgenius_winpe (72)
- pythoncrc16 (57)
- solidworks宏文件下载 (59)
- qt帮助文档中文版 (73)
- satacontroller (66)
- hgcad (64)
- bootimg.exe (69)
- android-gif-drawable (62)
- axure9元件库免费下载 (57)
- libmysqlclient.so.18 (58)
- springbootdemo (64)
本文暂时没有评论,来添加一个吧(●'◡'●)