
在excel选择 数据->导入外部数据->导入数据->选择那个txt文件
在导入向导中选择分割符号->下一步->勾选“空格”->下一步->完成。
具体格式需要你去尝试。
如果在win可以用com组件读取:
// 建立一个指向新COM组件的索引
$word = new COM("wordapplication") 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:\adoc");
//读取文档内容
$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/comtestdoc");
// 关闭与COM组件之间的连接
$word->Quit();
linux可用antiword插件去实现
将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)\OpenOfficeorg 3\program>soffice -headless -accept="socket,port=8100;urp;"
5、打开eclipse
6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib 下得jar包。
commons-io
jodconverter
juh
jurt
ridl
slf4j-api
slf4j-jdk14
unoil
xstream
8、Coding
查看代码
package commzuledoc2htmlutil;
import javaioBufferedReader;
import javaioFile;
import javaioFileInputStream;
import javaioFileNotFoundException;
import javaioIOException;
import javaioInputStreamReader;
import javanetConnectException;
import javautilDate;
import javautilregexMatcher;
import javautilregexPattern;
import comartofsolvingjodconverterDocumentConverter;
import comartofsolvingjodconverteropenofficeconnectionOpenOfficeConnection;
import comartofsolvingjodconverteropenofficeconnectionSocketOpenOfficeConnection;
import comartofsolvingjodconverteropenofficeconverterOpenOfficeDocumentConverter;
/
将Word文档转换成html字符串的工具类
@author MZULE
/
public class Doc2Html {
public static void main(String[] args) {
Systemout
println(toHtmlString(new File("C:/test/testdoc"), "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 {
// 连接
conconnect();
} catch (ConnectException e) {
Systemoutprintln("获取OpenOffice连接失败");
eprintStackTrace();
}
// 创建转换器
DocumentConverter converter = new OpenOfficeDocumentConverter(con);
// 转换文档问html
converterconvert(docFile, htmlFile);
// 关闭openoffice连接
condisconnect();
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 (brready()) {
htmlSbappend(brreadLine());
}
brclose();
// 删除临时文件
htmlFiledelete();
} catch (FileNotFoundException e) {
eprintStackTrace();
} catch (IOException e) {
eprintStackTrace();
}
// HTML文件字符串
String htmlStr = htmlSbtoString();
// 返回经过清洁的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 = Patterncompile(bodyReg);
Matcher bodyMatcher = bodyPatternmatcher(htmlStr);
if (bodyMatcherfind()) {
// 获取BODY内容,并转化BODY标签为DIV
htmlStr = bodyMatchergroup()replaceFirst("<BODY", "<DIV")
replaceAll("</BODY>", "</DIV>");
}
// 调整地址
htmlStr = htmlStrreplaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath
+ "/");
// 把<P></P>转换成</div></div>保留样式
// content = contentreplaceAll("(<P)([^>]>)(<\\/P>)",
// "<div$2</div>");
// 把<P></P>转换成</div></div>并删除样式
htmlStr = htmlStrreplaceAll("(<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;
}
}
以上就是关于如何把多个html导入到同一个word文件里面全部的内容,包括:如何把多个html导入到同一个word文件里面、php 怎么实现读取word文档内容,显示到html上面能给个案例最好了,谢谢!、如何利用openoffice读取word文档,并输出在页面上(HTML)等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)