作者:乔山办公网日期:
返回目录:office365
使用Java操作word的方法有几个,列出来:IText,Apache的POI包,tm-extractors包,jacob包,java2word包。
IText包比较大,但是功能比较全。而且比较成熟,用的人也多。
POI包是Apache的一个项目,不过现在已经停止更新了,而且我看了里面的Example,感觉调用起来很麻烦。用过之后再也不想用了。
tm-extractors包的API比较简单,调用起来很方便,代码量也少。
jacob包我觉得是最强大的,可以操作word,excel。因为调用的是微软底层的dll文件,需要在system32目录下放置jacob.dll,然后在classpath里面添加jacob.jar文件。推荐使用1.9版本,之前1.8版本调用起来就算你的代码没有写错也会出现一些莫名其妙的异常。
java2word也是调用微软的底层dll文件,有安装包,也有解压包,安装包会将jacob.dll文件装在system32目录下。之后将java2word.jar加到classpath就可以使用了。感觉如果只是操作word文档的话,比jacob用起来要简单和舒服。e69da5e6ba90e799bee5baa6e997aee7ad94364代码量也少,很清晰。
另外,使用jacob和java2word的话,会在调用WINWORD进程,如果多人并发访问服务器的word文件的话,会造成内存不足。
java中有调用外部程序的类,Runtime.getRuntime().exec("./**.exe");此函数返回的是一个Process 对象,该对象在创建后,可以对它进行 destroy () 杀掉子进程、 waitFor () 导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。 如下例子可以杀掉已经打开的文本进程:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test
{
e799bee5baa6e79fa5e98193e58685e5aeb9331public static void main(String[] args)
{
try
{
String[] cmd =
{ "notepad.exe"};
Process proc = Runtime.getRuntime().exec(cmd);//开启文本
BufferedReader in = new BufferedReader(new InputStreamReader(proc
.getInputStream()));
String string_Temp = in.readLine();
while (string_Temp != null)
{
System.out.println(string_Temp);
if(string_Temp.indexOf("notepad.exe")!=-1)
Runtime.getRuntime().exec("Taskkill /IM notepad.exe");
string_Temp = in.readLine();
}
}
catch (Exception e)
{
//结束进程
}
}
结果如下:
给些伪代码你:7a686964616fe59b9ee7ad94333
class MyFrame extends JFrame
{
public static void main(String[] args)
{
MyFrame myFrame = new MyFrame();
Panel onePanel = new FontPanel();
myFrame.add(ontPanel);
myFrame.setVisible(true);
}
}
class FontPanel extends Panel
{
Color color;
Font font;
String input;
eventCatch(e)
{
if (e==changeColorEvent)
changeColor();
if (e==changeSizeEvent)
changeSize();
if(e==hasInput)
input = whatIsInput();
repaint();
}
void update(Graphics g)
{
g.setColor(color);
g.setFont(font);
g.drawString(input);
}
}
jacob有很百多缺点,效率低下,而且服务器端自度动化Office会导致服务器工作不稳定,微软知明确指出不建议道在服务器端自动化Office,参考版
如果你用SOAOffice中间件操作word,就不用担心服务权器端word死进程了