
Java如何操作Word?Java如何操作Word,Excel,
Java Excel API 文档 http://www。
andykhan。com/jexcelapi/ 1、一个jacob操作Word的例子,其他操作excel,pdf的sample里都有 import java。
io。File; import com。
jacob。com。
*; import com。jacob。
activeX。*; public class WordTest { public static void main(String[] args) { WordBean word=new WordBean(); word。
openWord(true); word。createNewDocument(); word。
insertText("Hello word。"); } } import com。
jacob。activeX。
*; import com。 jacob。
com。*; public class WordBean extends java。
awt。Panel { private ActiveXComponent MsWordApp = null; private Dispatch document = null; public WordBean() { super(); } public void openWord(boolean makeVisible) { //Open Word if we've not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent("Word。
Application"); } //Set the visible property as required。 Dispatch。
put(MsWordApp, "Visible", new Variant(makeVisible)); } public void createNewDocument() { //Find the Documents collection object maintained by Word Dispatch documents = Dispatch。 get(MsWordApp,"Documents")。
toDispatch(); //Call the Add method of the Documents collection to create //a new document to edit document = Dispatch。 call(documents,"Add")。
toDispatch(); } public void insertText(String textToInsert) { // Get the current selection within Word at the moment。 If // a new document has just been created then this will be at // the top of the new doc Dispatch selection = Dispatch。
get(MsWordApp,"Selection")。toDispatch(); //Put the specified text at the insertion point Dispatch。
put(selection,"Text",textToInsert); } public void saveFileAs(String filename) { Dispatch。 call(document,"SaveAs",filename); } public void printFile() { //Just print the current document to the default printer Dispatch。
call(document,"PrintOut"); } public void closeDocument() { // Close the document without saving changes // 0 = wdDoNotSaveChanges // -1 = wdSaveChanges // -2 = wdPromptToSaveChanges Dispatch。 call(document, "Close", new Variant(0)); document = null; } public void closeWord() { Dispatch。
call(MsWordApp,"Quit"); MsWordApp = null; document = null; } }。
java操作word 的有哪几种方式
java读取word文档时,虽然网上介绍了很多插件poi、java2Word、jacob、itext等等,poi无法读取格式(新的API估
计行好像还在处于研发阶段,不太稳定,做项目不太敢用);java2Word、jacob容易报错找不到注册,比较诡异,我曾经在不同的机器上试过,操作
方法完全一致,有的机器不报错,有的报错,去他们论坛找高人解决也说不出原因,项目部署用它有点玄;itxt好像写很方便但是我查了好久资料没有见到过关
于读的好办法。经过一番选择还是折中点采用rtf最好,毕竟rtf是开源格式,不需要借助任何插件,只需基本IO操作外加编码转换即可。rtf格式文件表
面看来和doc没啥区别,都可以用word打开,各种格式都可以设定。
----- 实现的功能:读取rtf模板内容(格式和文本内容),替换变化部分,形成新的rtf文档。
----- 实现思路:模板中固定部分手动输入,变化的部分用$info$表示,只需替换$info$即可。
1、采用字节的形式读取rtf模板内容
2、将可变的内容字符串转为rtf编码
3、替换原文中的可变部分,形成新的rtf文档
主要程序如下:
public String bin2hex(String bin) {
char[] digital = "0123456789ABCDEF".toCharArray();
StringBuffer sb = new StringBuffer("");
byte[] bs = bin.getBytes();
int bit;
for (int i = 0; i
bit = (bs[i] & 0x0f0)
>>4;
sb.append("\\'");
sb.append(digital[bit]);
bit = bs[i] & 0x0f;
sb.append(digital[bit]);
}
return sb.toString();
}
public String readByteRtf(InputStream ins, String path){
String sourcecontent =
"";
try{
ins = new
FileInputStream(path);
byte[] b
= new byte[1024];
if (ins == null) {
System.out.println("源模板文件不存在");
}
int bytesRead = 0;
while (true) {
bytesRead = ins.read(b, 0, 1024); // return final read bytes
counts
if(bytesRead == -1) {// end of InputStream
System.out.println("读取模板文件结束");
break;
}
sourcecontent += new String(b, 0, bytesRead); // convert to string
using bytes
}
}catch(Exception e){
e.printStackTrace();
}
Java初学者入门需掌握的30个基本概念是什么
21.class类 object类中的getclass方法返回ckass类型的一个实例,程序启动时包含在main方法的类会被加载,虚拟机要加载他需要的所有类,每一个加载的类都要加载它需要的类。
22.class类为编写可动态操纵java代码的程序提供了强大的功能反射,这项功能为JavaBeans特别有用,使用反射Java能支持VB程序员习惯使用的工具。 能够分析类能力的程序叫反射器,Java中提供此功能的包叫Java.lang.reflect反射机制十分强大。
1.在运行时分析类的能力。 2.在运行时探察类的对象。
3.实现通用数组操纵代码。 4.提供方法对象。
而此机制主要针对是工具者而不是应用及程序。 反射机制中的最重要的部分是允许你检查类的结构.用到的API有: java.lang.reflect.Field 返回字段。
java.reflect.Method 返回方法。 java.lang.reflect.Constructor 返回参数。
方法指针:java没有方法指针,把一个方法的地址传给另一个方法,可以在后面调用它,而接口是更好的解决方案。 23.接口(Interface)说明类该做什么而不指定如何去做,一个类可以实现一个或多个interface。
学习JAVA需要哪些基础知识
说Java方面的话。
你需要精通java。还要学习struts,spring,ajax,jdbc,herbinate,javascript,XML,UML,最好再熟悉linux操作系统。
因为好多好点的公司都会去用安全实用的linux,而不是windows。熟悉它不是很困难的。
我介绍的那些课程你都要接触,象jdbc(是与数据库连接的)。说到数据库。
你最好去学下oracle和sqlserver对你都有好处。数据库结构差不多,所以学起来很容易的。
而其他的都是些工具,不过公司都在使用这些东西。所以你一定要去搞懂它。
有基础是很好学的,自学能力强的话,就可以自学的哈,不用非要培训中心的,如果有C的基础最好,没有,白手学java也可以。我就是白手学java 我就给你介绍这么多,希望能对你有帮助, 祝你好运。
转载请注明出处51数据库 » java入门word
我-停止了心跳