java word转成html,java word转html

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.ConnectException;

import java.util.Date;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

import com.artofsolving.jodconverter.DocumentConverter;

import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;

import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;

/**

* 将Word文档转换成html字符串的工具类

*

* @author MZULE

*

*/

public class Doc2Html {

public static void main(String[] args) {

File importFile = new File("D:\\wuji\\333.doc");

System.out.println(toHtmlString(importFile, "D:/test"));

}

/**

* 将word文档转换成html文档

*

* @param docFile

* 需要转换的word文档

* @param filepath

* 转换之后html的存放路径

* @return 转换之后的html文件

*/

public static File convert(File docFile, String filepath) {

// 创建保存html的文件

File htmlFile = new File(filepath + "/" + new Date().getTime()

+ ".html");

if(htmlFile.exists()){

System.out.println(htmlFile.getName());

}else{

try {

htmlFile.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 创建Openoffice连接

OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);

try {

// 连接

con.connect();

} catch (ConnectException e) {

System.out.println("获取OpenOffice连接失败...");

e.printStackTrace();

}

// 创建转换器

//DocumentConverter converter = new StreamOpenOfficeDocumentConverter(con);

DocumentConverter converter = new OpenOfficeDocumentConverter(con);

// 转换文档问html

converter.convert(docFile, htmlFile);

// 关闭openoffice连接

con.disconnect();

return htmlFile;

}

/**

* 将word转换成html文件,并且获取html文件代码。

*

* @param docFile

* 需要转换的文档

* @param filepath

* 文档中图片的保存位置

* @return 转换成功的html代码

*/

public static String toHtmlString(File docFile, String filepath) {

// 转换word文档

File htmlFile = convert(docFile, filepath);

// 获取html文件流

/*StringBuffer htmlSb = new StringBuffer();

try {

BufferedReader br = new BufferedReader(new InputStreamReader(

new FileInputStream(htmlFile)));

while (br.ready()) {

htmlSb.append(br.readLine());

}

br.close();

// 删除临时文件

htmlFile.delete();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// HTML文件字符串

String htmlStr = htmlSb.toString();

// 返回经过清洁的html文本

return clearFormat(htmlStr, filepath);*/

return "";

}

/**

* 清除一些不需要的html标记

*

* @param htmlStr

* 带有复杂html标记的html语句

* @return 去除了不需要html标记的语句

*/

protected static String clearFormat(String htmlStr, String docImgPath) {

// 获取body内容的正则

String bodyReg = "

";

Pattern bodyPattern = Pattern.compile(bodyReg);

Matcher bodyMatcher = bodyPattern.matcher(htmlStr);

if (bodyMatcher.find()) {

// 获取BODY内容,并转化BODY标签为DIV

htmlStr = bodyMatcher.group().replaceFirst("

.replaceAll("", "

");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用Apache POI和docx4j等库来读取Word文档,并使用Jsoup等库将文档内容换为HTML格式。在换过程中,可以将Word文档中的图片保存为本地文件,然后在HTML中引用这些图片。 下面是一个简单的示例代码,用于将Word文档换为HTML格式并带有图片: ```java import java.io.*; import org.apache.poi.xwpf.usermodel.*; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; import org.jsoup.Jsoup; import org.jsoup.nodes.*; import org.jsoup.select.Elements; public class WordToHtmlConverter { public static void main(String[] args) throws Exception { // 读取Word文档内容 XWPFDocument docx = new XWPFDocument(new FileInputStream("example.docx")); XWPFWordExtractor ex = new XWPFWordExtractor(docx); String text = ex.getText(); // 将Word文档内容换为HTML格式 WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File("example.docx")); HtmlExporter exporter = new HtmlExporterNG2(); StringWriter stringWriter = new StringWriter(); exporter.html(wordMLPackage, stringWriter); // 处理HTML中的图片 Document htmlDoc = Jsoup.parse(stringWriter.toString()); Elements imgs = htmlDoc.select("img"); for (Element img : imgs) { String src = img.attr("src"); if (src.startsWith("embedded:")) { // 从Word文档中提取图片 BinaryPart binaryPart = (BinaryPart) wordMLPackage.getParts().get(new org.docx4j.openpackaging.parts.PartName("/word/" + src.substring(9))); byte[] bytes = binaryPart.getBytes(); FileOutputStream fos = new FileOutputStream("image.png"); fos.write(bytes); fos.close(); // 替换HTML中的图片链接 img.attr("src", "image.png"); } } // 保存为HTML文件 FileOutputStream out = new FileOutputStream("example.html"); out.write(htmlDoc.html().getBytes()); out.close(); } } ``` 请注意,上述示例代码只是一个简单的示例,实际应用中可能会有更多的细节需要处理。例如,需要处理Word文档中的表格、段落样式等内容,以确保换结果符合预期。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值