java把word转html或txt
最近项目中需要在页面中预览word文件,虽说word本身就可以在页面中打开,但是有两个弊端,1是可客户端必须安装word, 2是客户端的环境以及office版本有差异,会造成预览不稳定。在网上找了一下,发现poi可以把word装换成txt,但是格式都丢了,只有光秃秃的文本,又搜jacob, 网友们众说纷纭, 最后还是自己sourceforge上下载jacob并阅读doc搞定了.
1 goto http://sourceforge.net/projects/jacob-project/ and download latest library of jacob.
下载的zip文件结构如下:
2 intel cpu的机器拷贝jacob-1.15-M3-x86.dll到%JAVA_HOME%/jre/bin, AMD cpu的机器拷贝jacob-1.15-M3-x64.dll. 不过请确保jre目录是你正在使用的jre, 因为现在很多eclipse版本自己带jre. 这个在eclipse windows-> preferences -> installed jres可以查看.
3 拷贝jacob.jar到你项目目录的lib下面并确保加入到了classpath.
准备工作完毕, 现在就写程序了.
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Test {
public static void main(String[] args) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch doc1 = app.getProperty("Documents").toDispatch();
//打开aaaa.doc
Dispatch doc2 = Dispatch.invoke(
doc1,
"Open",
Dispatch.Method,
new Object[]{"e:\\aaaa.doc", new Variant(false), new Variant(true)},
new int[1]
).toDispatch();
//另存为aaaa.html
Dispatch.invoke(
doc2,
"SaveAs",
Dispatch.Method,
new Object[]{
"c:\\aaaa.html",
new Variant(8)//7为txt格式, 8保存为html格式
},
new int[1]
);
Variant f = new Variant(false);
Dispatch.call(doc2, "Close", f);
}
}
使用起来很简单.
当然jacob不光可以做word to html, 还可以做很多事情:
Jacob is a Java library that lets Java applications communicate with Microsoft Windows DLLs or COM libraries. It does this through the use of a custom DLL that the Jacob Java classes communicate with via JNI. The library and dll isolate the Java developer from the underlying windows libraries so that
相关文档:
得啦,前面那个谜题是有点棘手,但它是有关整除的,每个人都知道整除是很麻烦的。那么下面的程序只涉及加法,它又会打印出什么呢?
public class Elementary{
public static void main(String[] args){
System.out.println(12345+5432l);
}
}
从表面上看,这像是一个很简单的谜题——简单到不需要纸和 ......
双引号:"或"
单引号:'或'(IE实体名无效)
&符号:&或&
<小于:<或<
>大于:>或>
空格: 或 
£英磅:£或£
¥元:¥或¥
¦分隔符:¦或& ......
using System.Text.RegularExpressions;
string ohtml = this.TextBox1.Text;
System.Text.RegularExpressions.MatchCollection m;
//提取字符串的图片
......
Dim objReg,objMatches,objMatch
Set objReg=new RegExp
objReg.Global=True
objReg.IgnoreCase=True
objReg.Pattern="<('[^']*'|""[^""]*""|[^'"">])*?>"
Set objMatches=objReg.Execute(字符串)
For Each objMatch In objMatches
找到的HTML :objMatch.value
Next
Set objMatches=Nothing
Set objRe ......
对于"&# 24038;&# 36793;"这种&#开始的字符,应该为html unicode编码类型,解码方法如下:
s="&# 24038;& # 36793;"
s="左边"
import re
_=re.compile('&#(x)?([0-9a-fA-F]+);')
to_str=lambda s,charset='utf-8':_.sub(lambda result:unichr(int(result.group(2),result.group ......