關(guān)于Java實(shí)現(xiàn)word(docx、doc)轉(zhuǎn)html的完美解決方案
最近在研究一些關(guān)于文檔轉(zhuǎn)換格式的方法,因?yàn)樾枰迷陂_(kāi)發(fā)的一個(gè)項(xiàng)目上,所以投入了一些時(shí)間,給大家聊下這塊邏輯及解決方案。
一、關(guān)于word轉(zhuǎn)換html大致都有哪些方法?
(1)使用 Microsoft Word 導(dǎo)出
其實(shí)該方法就是使用word本身導(dǎo)出方案
操作步驟:
- 在 Microsoft Word 中打開(kāi)文檔。
- 點(diǎn)擊 文件 > 另存為 或 導(dǎo)出。
- 選擇保存類(lèi)型為 網(wǎng)頁(yè)(.html, .htm)。
- 保存文件后,會(huì)生成一個(gè) HTML 文件(有時(shí)會(huì)附帶一個(gè)文件夾用于存放圖片等資源)。
優(yōu)點(diǎn):
- 保留了文檔的大部分格式。
- 操作簡(jiǎn)單,無(wú)需其他工具。
缺點(diǎn):
- 導(dǎo)出的 HTML 文件代碼較冗余,包含許多與 Word 相關(guān)的樣式和標(biāo)簽。
(2)使用第三方工具或在線(xiàn)轉(zhuǎn)換工具
一般常見(jiàn)的有SmallPDF、Zamzar、Convertio、LibreOffice等在線(xiàn)工具或軟件進(jìn)行轉(zhuǎn)換
優(yōu)點(diǎn):
- 方便快捷,適合大多數(shù)人使用。
- 有些工具可以清理冗余代碼,生成更簡(jiǎn)潔的 HTML。
缺點(diǎn):
- 在線(xiàn)工具可能存在隱私和安全風(fēng)險(xiǎn)。
- 某些工具可能無(wú)法完全保留復(fù)雜文檔的格式。
(3)使用編程實(shí)現(xiàn)自動(dòng)化轉(zhuǎn)換
常見(jiàn)的編程實(shí)現(xiàn)有:
- Python:
- 使用
python-docx
庫(kù)讀取 .docx 文件,再用自定義邏輯生成 HTML。 - 使用
mammoth
庫(kù),專(zhuān)門(mén)將 .docx 轉(zhuǎn)為干凈的 HTML(推薦)。 - 使用
pywin32
調(diào)用 Windows COM 接口操作 Microsoft Word。
- 使用
- Java:
- 使用 Apache POI 的
XWPF
模塊解析 .docx 文件并輸出 HTML。
- 使用 Apache POI 的
- Node.js:
- 使用
officegen
或mammoth.js
轉(zhuǎn)換 .docx 文件。
- 使用
- C#:
- 使用 OpenXML SDK 或 Interop.Word 來(lái)操作 Word 文件并轉(zhuǎn)換為 HTML。
本此講解的就是通過(guò)java的poi內(nèi)的模塊進(jìn)行解析輸出html
二、docx轉(zhuǎn)換html
示例代碼如下:
public static void docxtoHtml(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { long startTime = System.currentTimeMillis(); XWPFDocument document = new XWPFDocument(new FileInputStream(fileName)); // 用于存儲(chǔ)目錄內(nèi)容 StringBuilder toc = new StringBuilder(); toc.append("<div id='toc'>\n<ul>\n"); // 直接從 <ul> 開(kāi)始,表示目錄 // 遍歷文檔中的段落,查找目錄項(xiàng) List<XWPFParagraph> paragraphs = document.getParagraphs(); int tocLevel = 0; // 目錄的當(dāng)前級(jí)別,1代表一級(jí)目錄,2代表二級(jí)目錄,3代表三級(jí)目錄 boolean tocStarted = false; // 標(biāo)記目錄是否開(kāi)始 for (XWPFParagraph paragraph : paragraphs) { String style = paragraph.getStyle(); // 獲取段落樣式 String text = paragraph.getText(); // 獲取段落文本 // 根據(jù)段落的樣式級(jí)別來(lái)識(shí)別目錄項(xiàng),假設(shè)標(biāo)題樣式為 Heading 1, Heading 2, Heading 3 if (style != null) { if (style.equals("Heading 1")) { // 一級(jí)標(biāo)題 if (tocStarted) { toc.append("</ul>\n"); // 關(guān)閉上一級(jí)目錄 } toc.append("<ul>\n"); // 開(kāi)始一個(gè)新的無(wú)序列表 toc.append("<li><a href='#" + text.hashCode() + "'>" + text + "</a></li>\n"); tocLevel = 1; tocStarted = true; } else if (style.equals("Heading 2")) { // 二級(jí)標(biāo)題 if (tocLevel == 1) { toc.append("<ul>\n"); // 開(kāi)始二級(jí)目錄 } toc.append("<li><a href='#" + text.hashCode() + "'>" + text + "</a></li>\n"); tocLevel = 2; } else if (style.equals("Heading 3")) { // 三級(jí)標(biāo)題 if (tocLevel == 2) { toc.append("<ul>\n"); // 開(kāi)始三級(jí)目錄 } toc.append("<li><a href='#" + text.hashCode() + "'>" + text + "</a></li>\n"); tocLevel = 3; } } // 在目錄項(xiàng)前插入錨點(diǎn) if (style != null && (style.equals("Heading 1") || style.equals("Heading 2") || style.equals("Heading 3"))) { String anchor = "<a name='" + text.hashCode() + "'></a>"; String modifiedText = anchor + text; // 在目錄項(xiàng)文本前添加錨點(diǎn) // 更新段落中的文本 for (XWPFRun run : paragraph.getRuns()) { run.setText(modifiedText, 0); // 更新段落內(nèi)容 } } } // 如果目錄結(jié)束后,確保關(guān)閉所有的<ul>標(biāo)簽 if (tocLevel > 0) { toc.append("</ul>\n"); } toc.append("</ul>\n</div>\n"); // 關(guān)閉最外層的 <ul> 和 <div> // 設(shè)置XHTMLOptions XHTMLOptions options = XHTMLOptions.create().indent(4); File imageFolder = new File(tempPath); // 圖片臨時(shí)文件夾路徑 options.setExtractor(new FileImageExtractor(imageFolder)); options.URIResolver(new FileURIResolver(imageFolder)); // 使用 `XHTMLConverter` 進(jìn)行 DOCX 到 HTML 的轉(zhuǎn)換 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); XHTMLConverter.getInstance().convert(document, byteArrayOutputStream, options); System.out.println("Generate " + outPutFile + " with " + (System.currentTimeMillis() - startTime) + " ms."); // 獲取轉(zhuǎn)換后的HTML內(nèi)容 String htmlContent = new String(byteArrayOutputStream.toByteArray(), "UTF-8"); // 將TOC插入到HTML的開(kāi)頭 htmlContent = toc + htmlContent; // 處理分頁(yè)符:將分頁(yè)符添加到HTML中 htmlContent = htmlContent.replaceAll("<!-- PAGE_BREAK -->", "<div class='page-break'></div>"); // 添加表格樣式(邊框) htmlContent = htmlContent.replaceAll("<table>", "<table style='border: 1px solid black !important; border-collapse: collapse; width: 100%;'>"); htmlContent = htmlContent.replaceAll("<td>", "<td style='border: 1px solid black !important; padding: 5px; text-align: left;'>"); htmlContent = htmlContent.replaceAll("<th>", "<th style='border: 1px solid black !important; padding: 5px; text-align: left;'>"); htmlContent = htmlContent.replaceAll("<tr>", "<tr style='border: 1px solid black !important;'>"); htmlContent = htmlContent.replaceAll("<thead>", "<thead style='border: 1px solid black !important;'>"); htmlContent = htmlContent.replaceAll("<tbody>", "<tbody style='border: 1px solid black !important;'>"); htmlContent = htmlContent.replaceAll("<tfoot>", "<tfoot style='border: 1px solid black !important;'>"); // 增加全局CSS樣式(確保表格和目錄樣式正確) String style = "<style>\n" + "table { border: 1px solid black !important; border-collapse: collapse; width: 100%; }\n" + "td, th { border: 1px solid black !important; padding: 5px; text-align: left; }\n" + "tr { border: 1px solid black !important; }\n" + "ul { list-style-type: none; padding: 0; }\n" + // 去掉默認(rèn)的列表樣式 "li { margin: 5px 0; }\n" + // 設(shè)置目錄項(xiàng)的間距 "</style>\n"; htmlContent = style + htmlContent; // 將最終的HTML內(nèi)容寫(xiě)入文件 writeFile(htmlContent, outPutFile); }
該方法功能實(shí)現(xiàn):
- 將
.docx
文件轉(zhuǎn)換為 HTML 文件。 - 自動(dòng)生成基于文檔標(biāo)題的目錄 (TOC)。
- 為標(biāo)題添加錨點(diǎn)鏈接,支持 HTML 頁(yè)面內(nèi)跳轉(zhuǎn)。
- 處理分頁(yè)符,將其轉(zhuǎn)換為 HTML 的
<div>
元素。 - 增強(qiáng)表格樣式,添加邊框和對(duì)齊(有時(shí)原表格css樣式轉(zhuǎn)換后會(huì)被覆蓋掉)。
- 為 HTML 頁(yè)面添加全局 CSS 樣式,保證視覺(jué)效果統(tǒng)一。
三、doc轉(zhuǎn)換html
示例代碼如下:
public static void doctoHtml(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { // 開(kāi)始計(jì)時(shí) long startTime = System.currentTimeMillis(); // 讀取 Word 文檔 HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName)); // 創(chuàng)建 Word 轉(zhuǎn) HTML 轉(zhuǎn)換器 WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); // 圖片保存路徑設(shè)置 String imageFolderPath = tempPath + "images" + File.separator; // 存儲(chǔ)圖片的絕對(duì)路徑 // 設(shè)置圖片管理器,處理圖片保存邏輯 wordToHtmlConverter.setPicturesManager(new PicturesManager() { public String savePicture(byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches) { String picturePath = imageFolderPath + suggestedName; // 圖片保存路徑 File imageFolder = new File(imageFolderPath); if (!imageFolder.exists()) { boolean created = imageFolder.mkdirs(); // 創(chuàng)建圖片文件夾 if (created) { System.out.println("在以下位置創(chuàng)建圖片文件夾:" + imageFolder.getAbsolutePath()); } else { System.out.println("創(chuàng)建圖片文件夾失敗"); } } try { File pictureFile = new File(picturePath); try (FileOutputStream fos = new FileOutputStream(pictureFile)) { fos.write(content); // 寫(xiě)入圖片數(shù)據(jù) System.out.println("圖片保存路徑" + pictureFile.getAbsolutePath()); } } catch (IOException e) { e.printStackTrace(); } return picturePath; // 返回圖片路徑 } }); // 處理文檔內(nèi)容,轉(zhuǎn)換為 HTML wordToHtmlConverter.processDocument(wordDocument); // 獲取生成的 HTML 文檔 Document htmlDocument = wordToHtmlConverter.getDocument(); // 自定義分頁(yè)符處理:查找文檔中的分頁(yè)符并插入到 HTML 中 NodeList bodyNodes = htmlDocument.getElementsByTagName("body"); if (bodyNodes.getLength() > 0) { Node bodyNode = bodyNodes.item(0); // 獲取 HTML 中的 <body> 節(jié)點(diǎn) NodeList paragraphs = bodyNode.getChildNodes(); for (int i = 0; i < paragraphs.getLength(); i++) { Node paragraph = paragraphs.item(i); if (paragraph.getNodeType() == Node.ELEMENT_NODE && paragraph.getNodeName().equals("p")) { String innerText = paragraph.getTextContent(); if (innerText.contains("\f")) { // 檢查是否包含分頁(yè)符(\f) // 創(chuàng)建自定義分頁(yè)符 HTML 元素 Element pageBreak = htmlDocument.createElement("div"); pageBreak.setAttribute("class", "page-break"); // 設(shè)置 class 屬性,方便樣式控制 pageBreak.appendChild(htmlDocument.createTextNode(" ")); // 在分頁(yè)符前插入自定義分頁(yè)符標(biāo)記 bodyNode.insertBefore(pageBreak, paragraph); } } } } // 將 HTML 文檔輸出為字節(jié)流 ByteArrayOutputStream out = new ByteArrayOutputStream(); DOMSource domSource = new DOMSource(htmlDocument); StreamResult streamResult = new StreamResult(out); // 使用 Transformer 進(jìn)行 HTML 格式化輸出 TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); // 設(shè)置編碼為 UTF-8 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); // 格式化輸出 serializer.setOutputProperty(OutputKeys.METHOD, "html"); // 輸出格式為 HTML serializer.transform(domSource, streamResult); out.close(); // 將字節(jié)流轉(zhuǎn)換為字符串 String htmlContent = new String(out.toByteArray()); // 處理特殊標(biāo)記符,例如去掉目錄標(biāo)記(根據(jù)需要調(diào)整) htmlContent = htmlContent.replaceAll("TOC \\\\o \"1-3\" \\\\h \\\\z \\\\u", ""); // 將生成的 HTML 內(nèi)容寫(xiě)入文件 writeFile(htmlContent, outPutFile); // 輸出生成文件的信息及用時(shí) System.out.println("Generate " + outPutFile + " with " + (System.currentTimeMillis() - startTime) + " ms."); }
該方法功能實(shí)現(xiàn):
- 將
.doc
格式的 Word 文檔轉(zhuǎn)換為 HTML 文件。 - 提取并保存文檔中的圖片到指定路徑,并在 HTML 中插入圖片引用。
- 處理分頁(yè)符,將分頁(yè)符(
\f
)替換為自定義的 HTML 標(biāo)記。 - 格式化生成的 HTML 文件,便于閱讀和使用。
到此這篇關(guān)于關(guān)于java實(shí)現(xiàn)word(docx、doc)轉(zhuǎn)html的解決方案的文章就介紹到這了,更多相關(guān)java word轉(zhuǎn)html內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
- Java實(shí)現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
- Java實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線(xiàn)預(yù)覽
- Java實(shí)現(xiàn)HTML轉(zhuǎn)為Word的示例代碼
- Java 將Word轉(zhuǎn)為HTML的方法
- Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼
- java實(shí)現(xiàn)在線(xiàn)預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
相關(guān)文章
深扒Java中POJO、VO、DO、DTO、PO、BO、AO、DAO的概念和區(qū)別以及如何應(yīng)用
po vo bo dto dao 和 pojo 是軟件開(kāi)發(fā)中經(jīng)常使用的一些概念,用于設(shè)計(jì)和實(shí)現(xiàn)對(duì)象模型,下面將分別解釋這些概念的含義及其在開(kāi)發(fā)中的應(yīng)用,這篇文章主要給大家介紹了關(guān)于Java中POJO、VO、DO、DTO、PO、BO、AO、DAO的概念和區(qū)別以及如何應(yīng)用的相關(guān)資料,需要的朋友可以參考下2024-08-08一文搞清楚Java中Comparable和Comparator的區(qū)別
Java中的Comparable和Comparator都是用于集合排序的接口,但它們有明顯的區(qū)別,文中通過(guò)一些實(shí)例代碼詳細(xì)介紹了Java中Comparable和Comparator的區(qū)別,感興趣的同學(xué)跟著小編一起學(xué)習(xí)吧2023-05-05Java中的Spring Security配置過(guò)濾器
這篇文章主要介紹了Java中的Spring Security配置過(guò)濾器,文章通過(guò)圍繞文章主題的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05Java找出兩個(gè)大數(shù)據(jù)量List集合中的不同元素的方法總結(jié)
本文將帶大家了解如何快速的找出兩個(gè)相似度非常高的List集合里的不同元素。主要通過(guò)Java API、List集合雙層遍歷比較不同、借助Map集合查找三種方式,需要的可以參考一下2022-10-10