PHP读取word文档内容的方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP读取word文档内容的方法相关的知识,希望对你有一定的参考价值。

如题,有这样的类最好。
或者通过php将word文档转成html文档的方法也行。

觉得分不够请明说,还可以加。
不只是打开doc文档,我是想要获得doc文档里面的内容。
我想要的东东有点类似phpExcelReader这样的。
哪位朋友有的请贴出来让大家分享一下。

"wqwq2598"给出来的是直接COPY过来的,这东东好像没用。

感谢 "玩转Office" 的回答,“SOAOffice中间件”这个好像要安装一些东西,感觉还不如金格的全文批注系统好用,不过相信通过这中间件可以读得word文档的内容。
不知道到底有没有比较简单的办法去读取word文档。

jeyyu的代码中浏览器中执行时需要降低浏览器的安全设置,不方便,而且取得的内容只是到剪贴板

你可以试试SOAOffice中间件,这个是专门读写word的类,联系科翰索要php代码示例

你说的是端口问题吧,最近SOAOffice中间件推出了免端口版本。个人体会,SOAOffice编程调用上,要比金格的全文批注简单得多
参考技术A <?
// 建立一个指向新COM组件的索引
$word = new COM(”word.application”) or die(”Can't start Word!”);
// 显示目前正在使用的Word的版本号
//echo “Loading Word, v. $word->Version<br>”;
// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)
// to open the application in the forefront, use 1 (true)
//$word->Visible = 0;
//打?一个文档
$word->Documents->OPen(”d:\myweb\muban.doc”);
//读取文档内容
$test= $word->ActiveDocument->content->Text;
echo $test;
echo “<br>”;
//将文档中需要换的变量更换一下
$test=str_replace(”<变量>”,”这是变量”,$test);
echo $test;
$word->Documents->Add();
// 在新文档中添加文字
$word->Selection->TypeText(”$test”);
//把文档保存在目录中
$word->Documents[1]->SaveAs(”d:/myweb/comtest.doc”);
// 关闭与COM组件之间的连接
$word->Quit();
?>

本文来自: 脚本之家(www.jb51.net) 详细出处参考:http://www.jb51.net/article/14103.htm
参考技术B 如果仅仅是读取的话,不用调用com
我给您一个完整的代码

[Copy to clipboard] [ - ]
CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">;
<html style='width:350px; Height: 140px;'>;
<head>;
<title>;Open A Word& Document</title>;
<style>; html, body, button, div, input, select font-family: MS Shell Dlg; font-size: 8pt;; </style>;

<SCRIPT DEFER>;
<!--
function _CloseOnEsc() if (event.keyCode == 27) window.close(); return;
document.body.onkeypress = _CloseOnEsc;
//-->;
</SCRIPT>;
<SCRIPT>;
function openword ()
var pathlength = document.all.filepath.value.length - 4;
var pathisdoc = document.all.filepath.value.lastIndexOf(".doc");
if (pathlength !== pathisdoc)
alert ("您选择的不是一个 Microsoft Word 文档.\n它的扩展名必须是 .doc");

else
var file = unescape( document.set.filepath.value )
var word
try
word=new ActiveXObject('Word.Application.9') // word 2k
catch (e)
try
word=new ActiveXObject('Word.Application.8') // word 97
catch (e)
try
word=new ActiveXObject('Word.Application.7') // word 95
catch (e)



//WordPad.Document
//word.Visible=true;
word.Documents.Open(file);
word.Documents(file).Range(0).Copy();

//word.ActiveDocument.SaveAs("H:\Mydoc.doc", 4);
//word.Options.PrintBackground = false;
//word.ActiveDocument.PrintOut();
word.Quit();
window.close();


</SCRIPT>;
</head>;

<body style="background: threedface; color: windowtext;" margin: 30px; >;
<FORM METHOD="post" NAME="set" ID="set">;

;&&<IMG SRC="../images/ed_word.gif" ALT="" WIDTH="18" HEIGHT="18" BORDER="0" ALIGN="absmiddle">;打开一个 Word& 文档
<DIV ALIGN="center">;<INPUT TYPE="file" NAME="filepath" SIZE="30">;
<P>;<INPUT TYPE="button" NAME="btnOK" VALUE="Open" onclick="openword();">;&&<INPUT TYPE="button" NAME="Cancel" VALUE="退出" onclick="window.close();">;</DIV>;
</FORM>;
</body>;
</html>;
参考技术C 重起

使用java读取word文档中的内容。帮帮举个了例子。 谢谢大家帮助。

参考技术A 第一步:下载tm-extractors-0.4.jar下载地址:http://download.csdn.net/download/zcq87642231/1060382并把它放到你的classpath路径下面。第二步:简单的程序.(WordReader .java) import java.io.File;
import java.io.FileInputStream;

import org.textmining.text.extraction.WordExtractor;
public class WordReader
public static String readDoc(String doc) throws Exception
// 创建输入流读取doc文件
FileInputStream in = new FileInputStream(new File(doc));
WordExtractor extractor = null;
String text = null;
// 创建WordExtractor
extractor = new WordExtractor();
// 对doc文件进行提取
text = extractor.extractText(in);
return text;

/**
* @param args
*/
public static void main(String[] args)
// TODO Auto-generated method stub
try
//读取文件
String text = WordReader.readDoc("D:/tt/tt.doc");
//得到数据后打印出来(也可用一个流写到txt文件中)
System.out.println(text);
catch(Exception ex)
ex.printStackTrace();


参考技术B 使用流就可以读呀
String lineSeparator = (String) java.security.AccessController.doPrivileged(new sun.security.action.GetPropertyAction("line.separator"));
FileInputStream fis = null; FileOutputStream fos = null;
fis = new FileInputStream("e:/topo.svg"); fos = new FileOutputStream("e:/a.txt");
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fis));
StringBuffer sb = new StringBuffer();
String str = null;
while ((str = bufferReader.readLine()) != null)
sb.append(str + lineSeparator);


OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.write(sb.toString());
writer.flush();
writer.close();
参考技术C 转换器·····

以上是关于PHP读取word文档内容的方法的主要内容,如果未能解决你的问题,请参考以下文章

用php 读取word 文档内容 比如:word文档为试题等等

PHP实现对word文档的读取

php 怎么实现读取word文档内容,显示到html上面?能给个案例最好了,谢谢!

PHP在linux读取word文档

使用PHP对word文档进行操作的方法

怎么把python输出为word