用Spire.Doc,可以輕松地將HTML插入到Word文檔中。下面是一個示例代碼:
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.fields.DocPicture;
import com.spire.doc.fields.HtmlLayoutType;
import com.spire.doc.fields.TextRange;
import java.io.*;
public class HtmlToWord {
public static void main(String[] args) throws Exception {
//定義文檔對象
Document doc = new Document();
//添加一個段落
Section section = doc.addSection();
TextRange tr = section.addParagraph().appendText("下面是插入HTML到Word的示例:");
tr.getCharacterFormat().setBold(true);
tr.getCharacterFormat().setFontSize(16f);
//定義HTML內容
String htmlContent = "<html><body><h1>Hello, World!</h1><p>This is an example of inserting HTML into a Word document using Spire.Doc.</p></body></html>";
//插入HTML到Word
DocPicture htmlPicture = section.addParagraph().appendPicture(
htmlContent.getBytes(), HtmlLayoutType.Inline);
htmlPicture.setHorizontalPosition(0);
htmlPicture.setVerticalPosition(30);
htmlPicture.setWidth(520);
htmlPicture.setHeight(520 * 9 / 16);
//設置段落格式
section.getParagraphFormat().setHorizontalAlignment(HorizontalAlignment.Center);
//保存為Word文檔
doc.saveToFile("HtmlToWord.docx", FileFormat.Docx);
System.out.println("Word文檔已生成。");
}
}
在上面的代碼中,我們首先定義一個文檔對象并添加一個段落。然后定義HTML內容,并使用 appendPicture 方法將HTML插入到Word文檔中。 HtmlLayoutType 枚舉類型可用于指定HTML的布局方式。最后,我們設置了段落格式并將文檔保存為Word格式。在輸出語句中,我們打印了一條消息以通知用戶生成文檔的完成。
必不少朋友都會碰到批量修改word文檔內容、壓縮文檔圖片、文件格式轉換等重復性文檔處理工作的需要,今天胖胖老師就推薦給大家一個免費工具XCLWinKits,一站搞定你所有的需要。
XCLWinKits是一個非常實用的文檔處理工具合集,里面包含了許多常用的小功能。支持在電腦沒有安裝Office等軟件的情況下,對Word文檔、Excel文檔、Pdf文檔、HTML文檔、Txt文檔等內容的批量修改與替換;同時支持對所有文件的名稱和擴展名的批量修改;支持對Word文檔、Excel文檔、Pdf文檔、HTML文檔、Txt文本文檔的格式相互批量轉換,也支持將圖片批量轉為Word/Pdf/Excel等格式,非常方便。
目前最新版本是2.1.0。安裝包大小是54MB,支持win10。軟件是綠色軟件,無需安裝,直接運行exe即可,win7需要安裝vc2010++。
批量修改文件名:支持對所有文件的文件名批量修改,同時可以自定義多個修改規則。
批量修改文件內容:不需要安裝Office/Pdf查看器等軟件,支持對Word文檔/Excel文檔/Pdf文檔/Txt文檔的內容進行批量修改與替換,同時可以自定義多個修改規則。
批量修改文件擴展名:支持對所有文件的文件擴展名批量修改,同時可以自定義多個修改規則。
Word文檔格式批量轉換為doc、dot、docx、docm、dotx、dotm、xml、rtf、pdf、xps、xaml、swf、svg、html、ps、epub、odt、ott、txt、tiff、png、bmp、emf、jpg、gif等格式。
Excel文檔批量轉換為csv、xls、xlsx、xlsm、xltx、xltm、xlam、tsv、txt、html、pdf、ods、xml、xlsb、xps、tiff、svg、dif、numbers等格式。
Pdf文檔批量轉換為doc、xps、html、xml、tex、docx、svg、xlsx、epub、pptx等格式。
圖片文件批量轉換為Word/Excel/Pdf文檔。
批量調整圖片質量及圖片尺寸大小。
文本文件批量轉換為Word/Excel/Pdf文檔。
正則表達式測試小工具:可以實時匹配,程序員必備。
字符串轉js常量工具:對經常寫js代碼的人會有很大的幫助。
加密與解密:提供MD5的加密和其它方式的解密。
編碼轉換:提供常用的字符串編碼轉換功能。
日期時間差:一個小小的工具,可以計算指定的兩個時間的間隔。
我的IP地址:顯示當前電腦的內網及公網IP地址。
批量修改文件名稱/批量修改文件內容/批量修改文件擴展名
無需打開文檔進行批量內容替換
支持word文檔轉EPUB、圖片格式。
支持excel轉換pdf
支持pdf轉換word、epub
支持圖片批量修改和壓縮
近有一個業務是前端要上傳word格式的文稿,然后用戶上傳完之后,可以用瀏覽器直接查看該文稿,并且可以在富文本框直接引用該文稿,所以上傳word文稿之后,后端保存到db的必須是html格式才行,所以涉及到word格式轉html格式。
通過調查,這個word和html的處理,有兩種方案,方案1是前端做這個轉換。方案2是把word文檔上傳給后臺,后臺轉換好之后再返回給前端。至于方案1,看到大家的反饋都說很多問題,所以就沒采用前端轉的方案,最終決定是后端轉化為html格式并返回給前段預覽,待客戶預覽的時候,確認格式沒問題之后,再把html保存到后臺(因為word涉及到的格式太多,比如圖片,visio圖,表格,圖片等等之類的復雜元素,轉html的時候,可能會很多格式問題,所以要有個預覽的過程)。
對于word中普通的文字,問題倒不大,主要是文本之外的元素的處理,比如圖片,視頻,表格等。針對我本次的文章,只處理了圖片,處理的方式是:后臺從word中找出圖片(當然引入的jar包已經帶了獲取word中圖片的功能),上傳到服務器,拿到絕對路徑之后,放入到html里面,這樣,返回給前端的html內容,就可以直接預覽了。
maven引入相關依賴包如下:
<poi-scratchpad.version>3.14</poi-scratchpad.version>
<poi-ooxml.version>3.14</poi-ooxml.version>
<xdocreport.version>1.0.6</xdocreport.version>
<poi-ooxml-schemas.version>3.14</poi-ooxml-schemas.version>
<ooxml-schemas.version>1.3</ooxml-schemas.version>
<jsoup.version>1.11.3</jsoup.version>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi-scratchpad.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi-ooxml.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi-ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>${ooxml-schemas.version}</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>${jsoup.version}</version>
</dependency>
word轉html,對于word2003和word2007轉換方式不一樣,因為word2003和word2007的格式不一樣,工具類如下:
使用方法如下:
public String uploadSourceNews(MultipartFile file) {
String fileName = file.getOriginalFilename();
String suffixName = fileName.substring(fileName.lastIndexOf("."));
if (!".doc".equals(suffixName) && !".docx".equals(suffixName)) {
throw new UploadFileFormatException();
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
String dateDir = formatter.format(LocalDate.now());
String directory = imageDir + "/" + dateDir + "/";
String content = null;
try {
InputStream inputStream = file.getInputStream();
if ("doc".equals(suffixName)) {
content = wordToHtmlUtil.Word2003ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);
} else {
content = wordToHtmlUtil.Word2007ToHtml(inputStream, imageBucket, directory, Constants.HTTPS_PREFIX + imageVisitHost);
}
} catch (Exception ex) {
logger.error("word to html exception, detail:", ex);
return null;
}
return content;
}
關于doc和docx的一些存儲格式介紹:
docx 是微軟開發的基于 xml 的文字處理文件。docx 文件與 doc 文件不同, 因為 docx 文件將數據存儲在單獨的壓縮文件和文件夾中。早期版本的 microsoft office (早于 office 2007) 不支持 docx 文件, 因為 docx 是基于 xml 的, 早期版本將 doc 文件另存為單個二進制文件。
DOCX is an XML based word processing file developed by Microsoft. DOCX files are different than DOC files as DOCX files store data in separate compressed files and folders. Earlier versions of Microsoft Office (earlier than Office 2007) do not support DOCX files because DOCX is XML based where the earlier versions save DOC file as a single binary file.
可能你會問了,明明是docx結尾的文檔,怎么成了xml格式了?
很簡單:你隨便選擇一個docx文件,右鍵使用壓縮工具打開,就能得到一個這樣的目錄結構:
所以你以為docx是一個完整的文檔,其實它只是一個壓縮文件。
參考:
https://www.cnblogs.com/ct-csu/p/8178932.html
*請認真填寫需求信息,我們會在24小時內與您取得聯系。