如何在php下实现word转换成HTML,都进来看看吧

html-css06

如何在php下实现word转换成HTML,都进来看看吧,第1张

你好,试试我这个吧

刚刚测试完的。

<?php

/*

@author axgle [email protected]>

直接用php把word文档转化成HTML文件

适用于windows和安装了word的环境

*/

function word2html($wfilepath)

{

$word=new COM("Word.Application") or die("无法打开 MS Word")

$word->visible = 1

$word->Documents->Open($wfilepath)or die("无法打开这个文件")

$htmlpath= substr($wfilepath,0,-4)

$word->ActiveDocument->SaveAs($htmlpath,8)

$word->quit(0)

}

$w="G:/www/test.doc"

word2html($w)

print( "Word转html完成!" )

?>

实现代码如下:

public class Word2Html { public static void main(String argv[]) { try { //word 路径 html输出路径 convert2Html("D:/doctohtml/1.doc","D:/doctohtml/1.html") } catch (Exception e) { e.printStackTrace() } } public static void writeFile(String content, String path) { FileOutputStream fos = null BufferedWriter bw = null try { File file = new File(path) fos = new FileOutputStream(file) bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8")) bw.write(content) } catch (FileNotFoundException fnfe) { fnfe.printStackTrace() } catch (IOException ioe) { ioe.printStackTrace() } finally { try { if (bw != null) bw.close() if (fos != null) fos.close() } catch (IOException ie) { } } } public static void convert2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException { HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName))//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile)) WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter( DocumentBuilderFactory.newInstance().newDocumentBuilder() .newDocument()) wordToHtmlConverter.setPicturesManager( new PicturesManager() { public String savePicture( byte[] content, PictureType pictureType, String suggestedName, float widthInches, float heightInches ) { //html 中 图片标签中 显示的图片路路径 <img src="d:/test/0.jpg"/>return "d:/doctohtml/"+suggestedName} } ) wordToHtmlConverter.processDocument(wordDocument) //save pictures List pics=wordDocument.getPicturesTable().getAllPictures() if(pics!=null){ for(int i=0i<pics.size()i++){ Picture pic = (Picture)pics.get(i) System.out.println() try { //word中图片的存储路径 pic.writeImageContent(new FileOutputStream("D:/doctohtml/" + pic.suggestFullFileName())) } catch (FileNotFoundException e) { e.printStackTrace() } } } Document htmlDocument = wordToHtmlConverter.getDocument() ByteArrayOutputStream out = new ByteArrayOutputStream() DOMSource domSource = new DOMSource(htmlDocument) StreamResult streamResult = new StreamResult(out) TransformerFactory tf = TransformerFactory.newInstance() Transformer serializer = tf.newTransformer() serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8") serializer.setOutputProperty(OutputKeys.INDENT, "yes") serializer.setOutputProperty(OutputKeys.METHOD, "html") serializer.transform(domSource, streamResult) out.close() writeFile(new String(out.toByteArray()), outPutFile) }}

1、打开word文档,单击菜单栏中的“文件”,找到“另存为” 或“另存为网页”。

2、单击另存为” 或“另存为网页”,弹出"另存为" 窗口。

3、在"另存为" 窗口的“保存类型”,下拉菜单中找到“网页”。

4、在“文件名”一栏中可以修改我们想要的文件名,如图我改成了“经验”。

5、点“保存”,就把一篇word文档保存成网页格式了。我保存在桌面了。

6、这时候在桌面就可以找到保存的网页,然后用浏览器会打开并显示网页的内容。