文件管理 · 2023年11月24日

javaword2html|java 如何将html转换为word

① java怎么操作OpenOffice创建word文档并向其设置内容

将Word转Html的原理是这样的:1、客户上传Word文档到服务器2、服务器调用OpenOffice程序打开上传的Word文档3、OpenOffice将Word文档另存为Html格式4、Over至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。下面就是规规矩矩的实现。1、下载OpenOffice,2、下载Jodconverter 这是一个开启OpenOffice进行格式转化的第三方jar包。3、泡杯热茶,等待下载。4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"5、打开eclipse6、喝杯热茶,等待eclipse打开。7、新建eclipse项目,导入Jodconverter/lib 下得jar包。* commons-io* jodconverter* juh* jurt* ridl* slf4j-api* slf4j-jdk14* unoil* xstream 8、Coding…查看代码package com.mzule.doc2html.util;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;/*** 将Word文档转换成html字符串的工具类** @author MZULE**/public class Doc2Html {public static void main(String[] args) {System.out.println(toHtmlString(new File("C:/test/test.doc"), "C:/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");// 创建Openoffice连接OpenOfficeConnection con = new SocketOpenOfficeConnection(8100);try {// 连接con.connect();} catch (ConnectException e) {System.out.println("获取OpenOffice连接失败…");e.printStackTrace();}// 创建转换器DocumentConverter converter = new OpenOfficeDocumentConverter(con);// 转换文档问htmlconverter.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);}/*** 清除一些不需要的html标记** @param htmlStr* 带有复杂html标记的html语句* @return 去除了不需要html标记的语句*/protected static String clearFormat(String htmlStr, String docImgPath) {// 获取body内容的正则String bodyReg = "<BODY .*</BODY>";Pattern bodyPattern = Pattern.compile(bodyReg);Matcher bodyMatcher = bodyPattern.matcher(htmlStr);if (bodyMatcher.find()) {// 获取BODY内容,并转化BODY标签为DIVhtmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV").replaceAll("</BODY>", "</DIV>");}// 调整图片地址htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath+ "/");// 把<P></P>转换成</div></div>保留样式// content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",// "<div$2</div>");// 把<P></P>转换成</div></div>并删除样式htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");// 删除不需要的标签htmlStr = htmlStr.replaceAll("<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>","");// 删除不需要的属性htmlStr = htmlStr.replaceAll("<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>","<$1$2>");return htmlStr;}}

② java中html转word出现的问题

两种方式:1、纯Java,用POI来做2、用JNA调用word接口,根据office api来做第一种呢对于java开发来说相对简单,但是需要学POI,而且估计有些格式控制不好。第二种要学习JNA,而且需要边做变差word的office api。

③ JAVA+JS如何在HTML页面上显示WORD文档内容ActiveX只能兼容IE不考虑!

package com.cectsims.util; import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; 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; /** * doc docx格式转换 */ public class DocConverter { private static final int environment = 1;// 环境 1:windows 2:linux private String fileString;// (只涉及pdf2swf路径问题) private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public DocConverter(String fileString) { ini(fileString); } /** * 重新设置file * * @param fileString */ public void setFile(String fileString) { ini(fileString); } /** * 初始化 * * @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(".")); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /** * 转为PDF * * @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); } catch (java.net.ConnectException e) { e.printStackTrace(); System.out.println("****swf转换器异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /** * 转换成 swf */ @SuppressWarnings("unused") private void pdf2swf() throws Exception { Runtime r = Runtime.getRuntime(); if (!swfFile.exists()) { if (pdfFile.exists()) { if (environment == 1) {// windows环境处理 try { // Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9"); Process p = r.exec("C:/Program Files (x86)/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (IOException e) { e.printStackTrace(); throw e; } } else if (environment == 2) {// linux环境处理 try { Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已经存在不需要转换****"); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ((ptr = in.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /** * 转换主方法 */ @SuppressWarnings("unused") public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); pdf2swf(); } catch (Exception e) { e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } /** * 返回文件路径 * * @param s */ public String getswfPath() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /** * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } } }转换为PDF,然后转换为SWF,网上很多这个过程的代码,你可以看看

④ java 如何将html转换为word

两种方式:1、纯来Java,用POI来做自2、用JNA调用word接口,根据office api来做第一种呢对于java开发来说相对简单,但是需要学POI,而且估计有些格式控制不好。第二种要学习JNA,而且需要边做变差word的office api。

⑤ Java程序调用 openoffice,将doc文件转Html文件,但转换完格式都变成居左边

1、到官网下载Jacob,2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择BuildPath—>AddtoBuildPath);3、将Jacob.dll放至当前项目所用到的“jrein”下面(比如Eclipse正在用的Jre路径是C:Javajdk1.7.0_17jrein)。Ps:按照上面的步骤配置的,基本没有问题,但是有些电脑可能还会报错,比如:java.lang.UnsatisfiedLinkError:nojacobinjava.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWSSYSTEM32”下面。Java代码:publicclassJacobUtil{//8代表word保存成htmlpublicstaticfinalintWORD_HTML=8;publicstaticvoidmain(String[]args){Stringdocfile="C:\Users\无名\Desktop\xxx.doc";Stringhtmlfile="C:\Users\无名\Desktop\xxx.html";JacobUtil.wordToHtml(docfile,htmlfile);}/***WORD转HTML*@paramdocfileWORD文件全路径*@paramhtmlfile转换后HTML存放路径*/publicstaticvoidwordToHtml(Stringdocfile,Stringhtmlfile){//启动word应用程序(MicrosoftOfficeWord2003)ActiveXComponentapp=newActiveXComponent("Word.Application");System.out.println("*****正在转换…*****");try{//设置word应用程序不可见app.setProperty("Visible",newVariant(false));//documents表示word程序的所有文档窗口,(word是多文档应用程序)Dispatchdocs=app.getProperty("Documents").toDispatch();//打开要转换的word文件Dispatchdoc=Dispatch.invoke(docs,"Open",Dispatch.Method,newObject[]{docfile,newVariant(false),newVariant(true)},newint[1]).toDispatch();//作为html格式保存到临时文件Dispatch.invoke(doc,"SaveAs",Dispatch.Method,newObject[]{htmlfile,newVariant(WORD_HTML)},newint[1]);//关闭word文件Dispatch.call(doc,"Close",newVariant(false));}catch(Exceptione){e.printStackTrace();}finally{//关闭word应用程序app.invoke("Quit",newVariant[]{});}System.out.println("*****转换完毕********");}}

⑥ java中的jacob将word文档转化为HTML文件问题

1、到官网下载Jacob,2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);3、将Jacob.dll放至当前项目所用到的“jre\bin”下面(比如Eclipse正在用的Jre路径是C:\Java\jdk1.7.0_17\jre\bin)。Ps:按照上面的步骤配置的,基本没有问题,但是有些电脑可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWS\SYSTEM32”下面。Java代码:public class JacobUtil { // 8 代表word保存成html public static final int WORD_HTML = 8; public static void main(String[] args) { String docfile = "C:\\Users\\无名\\Desktop\\xxx.doc"; String htmlfile = "C:\\Users\\无名\\Desktop\\xxx.html"; JacobUtil.wordToHtml(docfile, htmlfile); } /** * WORD转HTML * @param docfile WORD文件全路径 * @param htmlfile 转换后HTML存放路径 */ public static void wordToHtml(String docfile, String htmlfile) { // 启动word应用程序(Microsoft Office Word 2003) ActiveXComponent app = new ActiveXComponent("Word.Application"); System.out.println("*****正在转换…*****"); try { // 设置word应用程序不可见 app.setProperty("Visible", new Variant(false)); // documents表示word程序的所有文档窗口,(word是多文档应用程序) Dispatch docs = app.getProperty("Documents").toDispatch(); // 打开要转换的word文件 Dispatch doc = Dispatch.invoke( docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); // 作为html格式保存到临时文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(WORD_HTML) }, new int[1]); // 关闭word文件 Dispatch.call(doc, "Close", new Variant(false)); }catch (Exception e){ e.printStackTrace(); }finally{ //关闭word应用程序 app.invoke("Quit", new Variant[] {}); } System.out.println("*****转换完毕********"); } }

⑦ java操作word或者html关键是定位操作对象。

package com.cectsims.util;import java.io.BufferedInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; 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; /** * doc docx格式转换 */ public class DocConverter { private static final int environment = 1;// 环境 1:windows 2:linux private String fileString;// (只涉及pdf2swf路径问题) private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置 private String fileName; private File pdfFile; private File swfFile; private File docFile; public DocConverter(String fileString) { ini(fileString); } /** * 重新设置file * * @param fileString */ public void setFile(String fileString) { ini(fileString); } /** * 初始化 * * @param fileString */ private void ini(String fileString) { this.fileString = fileString; fileName = fileString.substring(0, fileString.lastIndexOf(".")); docFile = new File(fileString); pdfFile = new File(fileName + ".pdf"); swfFile = new File(fileName + ".swf"); } /** * 转为PDF * * @param file */ private void doc2pdf() throws Exception { if (docFile.exists()) { if (!pdfFile.exists()) { OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100); try { connection.connect(); DocumentConverter converter = new OpenOfficeDocumentConverter(connection); converter.convert(docFile, pdfFile); // close the connection connection.disconnect(); System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****"); } catch (java.net.ConnectException e) { e.printStackTrace(); System.out.println("****swf转换器异常,openoffice服务未启动!****"); throw e; } catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) { e.printStackTrace(); System.out.println("****swf转换器异常,读取转换文件失败****"); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } } else { System.out.println("****已经转换为pdf,不需要再进行转化****"); } } else { System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****"); } } /** * 转换成 swf */ @SuppressWarnings("unused") private void pdf2swf() throws Exception { Runtime r = Runtime.getRuntime(); if (!swfFile.exists()) { if (pdfFile.exists()) { if (environment == 1) {// windows环境处理 try { // Process p = r.exec("D:/Program Files/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9"); Process p = r.exec("C:/Program Files (x86)/SWFTools/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.out.print(loadStream(p.getInputStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (IOException e) { e.printStackTrace(); throw e; } } else if (environment == 2) {// linux环境处理 try { Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9"); System.out.print(loadStream(p.getInputStream())); System.err.print(loadStream(p.getErrorStream())); System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****"); if (pdfFile.exists()) { pdfFile.delete(); } } catch (Exception e) { e.printStackTrace(); throw e; } } } else { System.out.println("****pdf不存在,无法转换****"); } } else { System.out.println("****swf已经存在不需要转换****"); } } static String loadStream(InputStream in) throws IOException { int ptr = 0; in = new BufferedInputStream(in); StringBuffer buffer = new StringBuffer(); while ((ptr = in.read()) != -1) { buffer.append((char) ptr); } return buffer.toString(); } /** * 转换主方法 */ @SuppressWarnings("unused") public boolean conver() { if (swfFile.exists()) { System.out.println("****swf转换器开始工作,该文件已经转换为swf****"); return true; } if (environment == 1) { System.out.println("****swf转换器开始工作,当前设置运行环境windows****"); } else { System.out.println("****swf转换器开始工作,当前设置运行环境linux****"); } try { doc2pdf(); pdf2swf(); } catch (Exception e) { e.printStackTrace(); return false; } if (swfFile.exists()) { return true; } else { return false; } } /** * 返回文件路径 * * @param s */ public String getswfPath() { if (swfFile.exists()) { String tempString = swfFile.getPath(); tempString = tempString.replaceAll("\\\\", "/"); return tempString; } else { return ""; } } /** * 设置输出路径 */ public void setOutputPath(String outputPath) { this.outputPath = outputPath; if (!outputPath.equals("")) { String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf(".")); if (outputPath.charAt(outputPath.length()) == '/') { swfFile = new File(outputPath + realName + ".swf"); } else { swfFile = new File(outputPath + realName + ".swf"); } } } }转换为PDF,然后转换为SWF,网上很多这个过程的代码,你可以看看

⑧ java html导出成word文档 文本框怎么解决

两种方式: 1、纯Java,用POI来做 2、用JNA调用word接口,根据office api来做 第一种呢对于java开发来说相对简单,但是需要学POI,而且估计有些格式控制不好。第二种要学习JNA,而且需要边做变差word的office api。

⑨ 请问java中用jacob将html转word中文乱码怎么解决

有中文乱码一般都是字符编码的问题,那你就是设置一下字符编码看看能不能解决这个问题。

⑩ java打印word

步骤:1,用word编辑你的模板2,模板保存为html格式3,在代码中把你要填的值动态拼接到那个html中,最后byte[] bytes= bf.toString().getBytes("GBK"); //bf.tostring()得到的拼接后的字符串 response.reset(); response.setContentType("application/msword"); response.setHeader("Content-disposition", "inline; filename=case_print.doc"); response.setCharacterEncoding("GBK"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close();