用户登录
用户注册

分享至

php读取word格式文件内容 php读取word文件

  • 作者: 您的用户名未注册
  • 来源: 51数据库
  • 2020-04-15

php读取word格式文件内容

怎样用PHP读取一个word文档内容并在浏览器中显示出来?

目前程序编译语言有很多种,其中php是最为常见的一种编程语言。

php读取word文档是很多朋友都想了解的,下面就由达内的老师为大家介绍一下。

Version}"; //把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真) $word->Visible = 1; //---------------------------------读取Word内容操作 START----------------------------------------- //打开一个word文档 $word->Documents->Open($url.$wordname); //将filename.doc转换为html格式,并保存为html文件 $word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8); //获取htm文件内容并输出到页面 (文本的样式不会丢失) $content = file_get_contents($url.$htmlname); echo $content; //获取word文档内容并输出到页面(文本的原样式已丢失) $content= $word->ActiveDocument->content->Text; echo $content; //关闭与COM组件之间的连接 $word->Documents->close(true); $word->Quit(); $word = null; unset($word); //---------------------------------新建立Word文档操作 START-------------------------------------- //建立一个空的word文档 $word->Documents->Add(); //写入内容到新建word $word->Selection->TypeText("$content"); //保存新建的word文档 $word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname); //关闭与COM组件之间的连接 $word->Quit(); } php_Word("tesw.doc","filename.html","写入word的内容");?> 展开

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

//将文档中需要换的变量更换一下 $test=str_replace("";$word-> // 关闭与COM组件之间的连接 OPen("d:\a;br>);$test"); // 显示目前正在使用的Word的版本号 //echo "//读取文档内容 $test= $word->$word-&gt.application") or die(" echo " //打?一个文档 br&gt:/myweb/comtest.doc"这是变量" $word->Documents->Add(); // 在新文档中添加文字 $word->Selection->TypeText("Version}SaveAs(" $word->" echo $test;Can'Loading Word, v;Visible = 0;// to open the application in the forefront;//把文档保存在目录中 &nbsp.doc"); ActiveDocument->content->Text;,$test)," //$word->"t start Word!"); echo $test, use 1 (true) Documents->// 把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真) &nbsp如果在win可以用com组件读取: // 建立一个指向新COM组件的索引 $word = new COM("word;Quit(); &nbsp ...

PHP实现对word文档的读取

传就传吧,后台传什么文件都可以,原样保留在文件夹下面就行了,天台的直接指向它,只要前台安装了相应的程序就能打开,例如指向一个WORD的代码可以是:你可能觉得这样很不好,前台会弹出WORD窗口打开文件。

但是没有别的完美的办法,因为把WORD转换为HTML都会丢失重要信息的:文件内部链接会丢失、文件打印格式会丢失、文件显示格式也不完全支持,甚至会变得很难看。

一句话,你的后台最好不要对WORD文件进行处理,尽管有方法(就是使用DCOM调用WORD打开文件,然后另存为网页格式)

php应该怎么读取word和Excel文件的内容

/** 必须将 php.ini 中的 com.allow_dcom 设为 TRUE*/function php_Word($wordname,$htmlname,$content){//获取链接地址$url = $_SERVER['HTTP_HOST'];$url = ";$url = $url.$_SERVER['PHP_SELF'];$url = dirname($url)."/";//建立一个指向新COM组件的索引$word = new COM("word.application") or die("Unable to instanciate Word");//显示目前正在使用的Word的版本号echo "Loading Word, v. {$word->Version}";//把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)$word->Visible = 1;//---------------------------------读取Word内容操作 START-----------------------------------------//打开一个word文档$word->Documents->Open($url.$wordname);//将filename.doc转换为html格式,并保存为html文件$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$htmlname,8);//获取htm文件内容并输出到页面 (文本的样式不会丢失)$content = file_get_contents($url.$htmlname);echo $content;//获取word文档内容并输出到页面(文本的原样式已丢失)$content= $word->ActiveDocument->content->Text;echo $content;//关闭与COM组件之间的连接$word->Documents->close(true);$word->Quit();$word = null;unset($word);//---------------------------------新建立Word文档操作 START--------------------------------------//建立一个空的word文档$word->Documents->Add();//写入内容到新建word$word->Selection->TypeText("$content");//保存新建的word文档$word->Documents[1]->SaveAs(dirname(__FILE__)."/".$wordname);//关闭与COM组件之间的连接$word->Quit();}php_Word("tesw.doc","filename.html","写入word的内容");?>

PHP导出的word,在打开时提示无法打开,修复后可打开

你好,导出的这个格式,你是指网页上使用PHP代码来导出的么估计还是这个格式方面的问题,所以出现这个有错误的这个能恢复文档的内容那么还是可以的啊,主要是看看你的PHP怎么导出的应该是在导出的过程中出现的问题了,只能是使用修复工具来修复吧

如何利用openoffice读取word文档,并输出在页面上(HTML)

将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); // 转换文档问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); } /** * 清除一些不需要的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("", ""); } // 调整图片地址 htmlStr = htmlStr.replaceAll("转换成保留样式 // content = content.replaceAll("(]*>.*?)()", // ""); // 把转换成并删除样式 htmlStr = htmlStr.replaceAll("(]*)(>.*?)()", ""); // 删除不需要的标签 htmlStr = htmlStr .replaceAll( "]*?>", ""); // 删除不需要的属性 htmlStr = htmlStr .replaceAll( "]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>", ""); return htmlStr; }}

转载请注明出处51数据库 » php读取word格式文件内容

软件
前端设计
程序设计
Java相关