Java 創建、加載、操作和保存WPS文字(Word文檔)
在日常工作、學習和生活中,我們經常會使用WPS文字(Word文檔)這一款辦公軟件來對文檔進行編輯處理。本文主要想介紹如何在后端通過使用Java代碼的方式來創建、加載、操作和保存WPS文字(Word文檔)。
使用工具:Free .Doc for Java(免費版)
Jar包導入:在安裝配置好JDK和 IDEA之后,需將以上使用工具中的Jar包導入IDEA。導入方式有兩種:其一,在E-中文官網上獲取產品包,解壓后在lib文件夾下找到.Pdf.jar,然后手動導入IDEA;其二,在IDEA中創建項目,然后在Pom.xml文件下鍵入以下代碼,最后點擊“ ”即可。
com.e-iceblue
http://repo.e-iceblue.cn/repository/maven-public/
e-iceblue
spire.doc.free
3.9.0
代碼示例
示例一 創建WPS文字(Word文檔)
在新建WPS文字時,Free .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();
}
}
設置效果
聲明:本站所有文章資源內容,如無特殊說明或標注,均為采集網絡資源。如若本站內容侵犯了原著者的合法權益,可聯系本站刪除。