Java 創(chuàng)建、加載、操作和保存WPS文字(Word文檔)
在日常工作、學(xué)習(xí)和生活中,我們經(jīng)常會(huì)使用WPS文字(Word文檔)這一款辦公軟件來(lái)對(duì)文檔進(jìn)行編輯處理。本文主要想介紹如何在后端通過(guò)使用Java代碼的方式來(lái)創(chuàng)建、加載、操作和保存WPS文字(Word文檔)。
使用工具:Free .Doc for Java(免費(fèi)版)
Jar包導(dǎo)入:在安裝配置好JDK和 IDEA之后,需將以上使用工具中的Jar包導(dǎo)入IDEA。導(dǎo)入方式有兩種:其一,在E-中文官網(wǎng)上獲取產(chǎn)品包,解壓后在lib文件夾下找到.Pdf.jar,然后手動(dòng)導(dǎo)入IDEA;其二,在IDEA中創(chuàng)建項(xiàng)目,然后在Pom.xml文件下鍵入以下代碼,最后點(diǎn)擊“ ”即可。
com.e-iceblue
http://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.doc.free
3.9.0
代碼示例
示例一 創(chuàng)建WPS文字(Word文檔)
在新建WPS文字時(shí),F(xiàn)ree .Doc for Java支持指定文本內(nèi)容,設(shè)置字符名稱、字號(hào)、字體顏色,以及段落格式,段前縮進(jìn),斷后間距等。
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 {
//創(chuàng)建Document實(shí)例
Document document = new Document();
//添加節(jié)到文檔
Section section = document.addSection();
//添加五個(gè)段落至該節(jié)
Paragraph para1 = section.addParagraph();
para1.appendText("斷橋,又小雪");
Paragraph para2 = section.addParagraph();
para2.appendText("寂寞的夜里窗外下著雪傳來(lái)一首曲凄涼而婉約;一個(gè)不眠之夜,心中的惆悵與誰(shuí)言;深秋的夜,寂寞的夜,何處去尋花與蝶。");
Paragraph para3 = section.addParagraph();
para3.appendText("在這個(gè)不眠夜,孤身一人,惟有這小雪,陪我度過(guò),心中的壓抑與誰(shuí)訴說(shuō)。");
Paragraph para4 = section.addParagraph();
para4.appendText("晶瑩的冰花揚(yáng)揚(yáng)灑灑的揮下,沖淡了最后的暖意,蓋住了飄落的花瓣;晶瑩的冰花凄凄慘慘的撒下,淹沒(méi)了最后的喜悅," +
"留下了暗淡的陰霾,它輕輕地飄著,逍遙地舞著,凝結(jié)在我的心田,亮晶晶地,美卻略顯凄涼。");
Paragraph para5 = section.addParagraph();
para5.appendText("清風(fēng)散過(guò),瓣瓣落,落盡處,淚滴蕩空。點(diǎn)點(diǎn)粉嫩,是離情,別是一般眷戀繞心頭。滿地殘落葉,余憔悴,怎堪回首," +
"一切盡在無(wú)語(yǔ)凝噎中。冷冷凄凄,慘慘兮。");
//將第一段作為標(biāo)題,設(shè)置標(biāo)題段落格式
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");
//設(shè)置標(biāo)題段落居中對(duì)齊
para1.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//設(shè)置其余四個(gè)段落的格式
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");
//設(shè)置第二、三、四、五段落的段首縮進(jìn)
para2.getFormat().setFirstLineIndent(25f);
para3.getFormat().setFirstLineIndent(25f);
para4.getFormat().setFirstLineIndent(25f);
para5.getFormat().setFirstLineIndent(25f);
//設(shè)置第一段落的段后間距
para1.getFormat().setAfterSpacing(10f);
//保存文檔
ByteArrayOutputStream bos = new ByteArrayOutputStream();
document.saveToStream(bos, FileFormat.Doc);
//將流寫(xiě)入WPS文件
FileOutputStream fos = new FileOutputStream("output/CreateWpsWord.wps");
fos.write(bos.toByteArray());
//關(guān)閉流
bos.close();
fos.close();
}
}
結(jié)果文檔
示例二 加載、操作和保存WPS文字(Word文檔)
我將上示例的結(jié)果文檔保存至本地,接著用以下代碼加載文檔,并給文檔正文第一段設(shè)置背景顏色,最后再保存為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 {
//通過(guò)流加載WPS文字文檔
FileInputStream inputStream = new FileInputStream(new File("C:\\Users\\Test1\\Desktop\\CreateWpsWord.wps"));
Document document = new Document();
document.loadFromStream(inputStream, FileFormat.Doc);
//獲取文檔的第一個(gè)節(jié)
Section section = document.getSections().get(0);
//獲取該節(jié)中第二個(gè)段落
Paragraph paragraph = section.getParagraphs().get(1);
//給該段落設(shè)置背景顏色
paragraph.getFormat().setBackColor(Color.pink);
//將結(jié)果文檔保存到流
ByteArrayOutputStream bos = new ByteArrayOutputStream();
document.saveToStream(bos, FileFormat.Doc);
//將流寫(xiě)入WPS文檔
FileOutputStream fos = new FileOutputStream("Output/LoadEditAndSaveWpsWord.wps");
fos.write(bos.toByteArray());
//關(guān)閉流
bos.close();
fos.close();
}
}
設(shè)置效果
聲明:本站所有文章資源內(nèi)容,如無(wú)特殊說(shuō)明或標(biāo)注,均為采集網(wǎng)絡(luò)資源。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系本站刪除。