能不能帮我看看我的代码:
在classpath中配置jacob.jar;
在system32中添加了dll文件;import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;public class FileExtracter { private ActiveXComponent MsWordApp = null;
private Dispatch document = null; public static void main(String[] args) {
String filepath = "c:/test.doc";
FileExtracter word = new FileExtracter();
word.openWord(true); word.openDocument(filepath);
} public void openWord(boolean makeVisible) { if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
} Dispatch.put(MsWordApp, "Visible",
new Variant(makeVisible));
} public void openDocument(String path) { MsWordApp.setProperty("Visible", new Variant(true));
Dispatch odocuments = MsWordApp.getProperty("Documents").toDispatch();
document = Dispatch.call(odocuments, "Open", path).toDispatch(); }}
在cmd下运行命令出错:
/*D:\jdk142\bin>nmake -f FileExtracter.javaMicrosoft (R) Program Maintenance Utility Version 6.00.8168.0
Copyright (C) Microsoft Corp 1988-1998. All rights reserved.FileExtracter.java(1) : fatal error U1034: syntax error : separator missing
Stop.
*/
请帮忙看看!
如果在jbuilder下运行则报错:不过word的那个窗口能出来了/*
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at testr.FileExtracter.openDocument(FileExtracter.java:59)
at testr.FileExtracter.main(FileExtracter.java:18)
*/
在classpath中配置jacob.jar;
在system32中添加了dll文件;import java.io.File;
import com.jacob.com.*;
import com.jacob.activeX.*;public class FileExtracter { private ActiveXComponent MsWordApp = null;
private Dispatch document = null; public static void main(String[] args) {
String filepath = "c:/test.doc";
FileExtracter word = new FileExtracter();
word.openWord(true); word.openDocument(filepath);
} public void openWord(boolean makeVisible) { if (MsWordApp == null) {
MsWordApp = new ActiveXComponent("Word.Application");
} Dispatch.put(MsWordApp, "Visible",
new Variant(makeVisible));
} public void openDocument(String path) { MsWordApp.setProperty("Visible", new Variant(true));
Dispatch odocuments = MsWordApp.getProperty("Documents").toDispatch();
document = Dispatch.call(odocuments, "Open", path).toDispatch(); }}
在cmd下运行命令出错:
/*D:\jdk142\bin>nmake -f FileExtracter.javaMicrosoft (R) Program Maintenance Utility Version 6.00.8168.0
Copyright (C) Microsoft Corp 1988-1998. All rights reserved.FileExtracter.java(1) : fatal error U1034: syntax error : separator missing
Stop.
*/
请帮忙看看!
如果在jbuilder下运行则报错:不过word的那个窗口能出来了/*
com.jacob.com.ComFailException: A COM exception has been encountered:
At Invoke of: Documents
Description: An unknown COM error has occured.
at com.jacob.com.Dispatch.invokev(Native Method)
at com.jacob.activeX.ActiveXComponent.getProperty(ActiveXComponent.java)
at testr.FileExtracter.openDocument(FileExtracter.java:59)
at testr.FileExtracter.main(FileExtracter.java:18)
*/
解决方案 »
- 有错误,请指点。
- 关于一个聊天程序中的消息处理。
- 关于Process process = Runtime.getRuntime().exec问题,很棘手
- 有关CLOB的问题:循环多次插入大对象字段的问题,希望用过大对象的朋友能帮助我解决,谢谢!~
- 如何把application改编成applet??请高手赐教!!!给分
- JSP里调用自定义类的问题
- 谁给我讲讲,关于多线称使用变量的问题(看看我的理论对不对)!
- 什么情况下必须使用applet访问数据库?
- 关于继承的一个小问题
- towhile:plus loop
- 比较经典:怎么解析HTML文件?请高手赐教!!万分感谢!
- 我从网上下的Java游戏怎么运行不了?怎么会没有main()?
import com.jacob.activeX.*;
import com.jacob.com.Dispatch;public class ExcelTest {
public ExcelTest() {
} private static ActiveXComponent xl;
private static Object workbooks = null;
private static Object workbook = null;
private static Object sheet = null;
private static String filename = null;
private static boolean readonly = false; public static void main(String[] args) { String file = "c:\\test.xls";
OpenExcel(file, true); //false为不显示打开Excel
//SetValue("A1", "Value", "2");
// System.out.println(GetValue("A3"));
CloseExcel(false);
} //打开Excel文档
private static void OpenExcel(String file, boolean f) {
try {
filename = file;
xl = new ActiveXComponent("Excel.Application");
xl.setProperty("Visible", new Variant(f));
workbooks = xl.getProperty("Workbooks").toDispatch();
Dispatch.put(xl, "Visible", new Variant(true));
//以下语句出错
workbook = Dispatch.invoke(workbooks,
"Open",
Dispatch.Method,
new Object[] {filename,
new Variant(false),
new Variant(readonly)}, //是否以只读方式打开
new int[1]).toDispatch(); } catch (Exception e) {
e.printStackTrace();
}
} //关闭Excel文档
private static void CloseExcel(boolean f) {
try {
//以下语句出错
Dispatch.call(workbook, "Save");
Dispatch.call(workbook, "Close", new Variant(f));
} catch (Exception e) {
e.printStackTrace();
} finally {
xl.invoke("Quit", new Variant[] {});
}
}//写入值
private static void SetValue(String position, String type, String value) {
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
Object cell = Dispatch.invoke(sheet, "Range",
Dispatch.Get,
new Object[] {position},
new int[1]).toDispatch();
Dispatch.put(cell, type, value);
}//读取值
private static String GetValue(String position) {
//以下语句出错
Object cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] {position}, new int[1]).
toDispatch();
String value = Dispatch.get(cell, "Value").toString(); return value;
}
}
http://qingyuan18.spaces.live.com/?_c11_blogpart_blogpart=blogview&_c=blogpart&partqs=cat%3d%25e8%25ae%25a1%25e7%25ae%2597%25e6%259c%25ba%25e4%25b8%258e%2bInternet这里面有操作excel的包和api,我用的jxl