請問如何讀取msword檔案的內容 |
尚未結案
|
hypptz
一般會員 ![]() ![]() 發表:1 回覆:1 積分:0 註冊:2003-05-21 發送簡訊給我 |
|
axsoft
版主 ![]() ![]() ![]() ![]() ![]() ![]() 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
引言: 如何用java或jsp讀取word當案的資料?使用COM Object試試 來源:http://www.alphaworks.ibm.com/forum/olebridge.nsf/archived/326C7A24514B7679899C062AA87A787B?OpenDocument Here are some listings of some classes I made when I was evaluating OLEBridge. You probobly need to view them with Netscape as MSIE doesn't seem to be able to show the text as preformatted. Here's a little GUI for trying out method calls on COM objects (it's hard coded to work with Word, but I've used it for playing with Lotus WordPro, too). Apologies for the UI and the lack of comments, but I found it quite useful. No introspection on the COM objects (as far as I could tell) means you have to find out method and property names some other way. the OLEView is not totally generic, but it can do some useful things, and I'm sure it could be augmented to be totally generic, if someone wants to put in the time. import com.ibm.olebridge.*; import com.ibm.olebridge.oletypes.*; import java.awt.*; import java.awt.event.*; import java.util.*; public class OLEView { private Hashtable objectTable=new Hashtable(); private Hashtable methodNameTable=new Hashtable(); private TextField javaNameInput=new TextField(20); private TextField javaTypeInput=new TextField(20); private Button createObjButton=new Button("Create"); private Choice selector=new Choice(); private Choice methodSelector=new Choice(); private Object currentObj; private Hashtable oleObjTable=new Hashtable(); private Vector oleFunList=new Vector(); private Vector olePropList=new Vector(); private Vector oleAppList=new Vector(); private Choice oleObjectSelector=new Choice(); private TextField oleFunctionNameInput=new TextField(20); private Choice oleFunctionNameSelector=new Choice(); private Choice olePropNameSelector=new Choice(); private Choice oleAppNameSelector=new Choice(); private TextField oleObjectName=new TextField(20); private TextField paramInput=new TextField(5); private Button executeOperation=new Button("Invoke OLE"); private Button getProperty=new Button("Get/Set Property"); private Button createObj=new Button("Create App"); { createObj.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { String appName=oleFunctionNameInput.getText().intern(); OleObject appObj=oleServer.createOleObject(appName,10000); if(!oleAppList.contains(appName)) { oleAppList.addElement(appName); oleAppNameSelector.add(appName); oleAppNameSelector.setSize(olePropNameSelector.getPreferredSize()); frame.validate(); frame.repaint(); } oleObjTable.put(appName,appObj); oleObjectSelector.add(appName); oleObjectSelector.setSize(oleObjectSelector.getPreferredSize()); frame.validate(); frame.repaint(); }catch(Exception e) { System.out.println(e); } } }); getProperty.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { OleObject obj=(OleObject)oleObjTable.get(oleObjectSelector.getSelectedItem()); String propName=oleFunctionNameInput.getText().intern(); String parm=paramInput.getText().intern(); Variant[] parms; if(parm!=null && parm.length()>0) { parms=new Variant[1]; try { parms[0]=new Variant(Integer.parseInt(parm)); }catch (Exception e) { try { parms[0]=new Variant(Boolean.getBoolean(parm)); }catch(Exception e2) { parms[0]=new Variant(parm); } } System.out.println("Trying to set property "+propName+" to "+parm+" on "+oleObjectSelector.getSelectedItem()+" ("+obj+")"); obj.setProperty(propName,parms); if(!olePropList.contains(propName)) { olePropList.addElement(propName); olePropNameSelector.add(propName); olePropNameSelector.setSize(olePropNameSelector.getPreferredSize()); frame.validate(); frame.repaint(); } System.out.println("Propety set"); }else { parms=new Variant[0]; System.out.println("Trying to get property "+propName+" from "+oleObjectSelector.getSelectedItem()+" ("+obj+")"); Variant caught=obj.getProperty(propName); if(!olePropList.contains(propName)) { olePropList.addElement(propName); olePropNameSelector.add(propName); olePropNameSelector.setSize(olePropNameSelector.getPreferredSize()); frame.validate(); frame.repaint(); } System.out.println("Got property :"+caught+" It's of type "+caught.getOleTypeName()); System.out.print("Trying longValue: "); try { System.out.println(caught.longValue()); } catch(Exception e) { System.out.println("didn't work"); } System.out.print("Trying intValue: "); try { System.out.println(caught.intValue()); } catch(Exception e) { System.out.println("didn't work"); } System.out.print("Trying StringValue: "); try { System.out.println(caught.StringValue()); }catch(Exception e) { System.out.println("didn't work"); } String catchName=oleObjectName.getText().intern(); oleObjTable.put(catchName,caught.getOleObject()); oleObjectSelector.add(catchName); oleObjectSelector.setSize(oleObjectSelector.getPreferredSize()); frame.validate(); frame.repaint(); } }catch(Exception e) { System.out.println(e); } } }); executeOperation.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { OleObject obj=(OleObject)oleObjTable.get(oleObjectSelector.getSelectedItem()); String methName=oleFunctionNameInput.getText().intern(); String parm=paramInput.getText(); Variant[] parms; if(parm!=null && parm.length()>0) { parms=new Variant[1]; try { parms[0]=new Variant(Integer.parseInt(parm)); }catch (Exception e) { parms[0]=new Variant(parm); } }else { parms=new Variant[0]; } System.out.println("About to invoke "+methName+" with param "+parm+" on "+oleObjectSelector.getSelectedItem()+" ("+obj+")"); Variant caught=obj.invoke(methName,parms); if(!oleFunList.contains(methName)) { oleFunList.addElement(methName); oleFunctionNameSelector.add(methName); oleFunctionNameSelector.setSize(oleFunctionNameSelector.getPreferredSize()); frame.validate(); frame.repaint(); } System.out.println("Got property :"+caught+" It's of type "+caught.getOleTypeName()); System.out.print("Trying longValue: "); try { System.out.println(caught.longValue()); } catch(Exception e) { System.out.println("didn't work"); } System.out.print("Trying intValue: "); try { System.out.println(caught.intValue()); } catch(Exception e) { System.out.println("didn't work"); } System.out.print("Trying StringValue: "); try { System.out.println(caught.StringValue()); }catch(Exception e) { System.out.println("didn't work"); } String catchName=oleObjectName.getText().intern(); oleObjTable.put(catchName,caught.getOleObject()); oleObjectSelector.add(catchName); oleObjectSelector.setSize(oleObjectSelector.getPreferredSize()); frame.validate(); frame.repaint(); }catch(Exception e){ System.out.println(e); } } }); oleFunctionNameSelector.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if(ie.getStateChange()==ItemEvent.SELECTED) { oleFunctionNameInput.setText(oleFunctionNameSelector.getSelectedItem()); }else { System.out.println(ie); } } }); olePropNameSelector.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if(ie.getStateChange()==ItemEvent.SELECTED) { oleFunctionNameInput.setText(olePropNameSelector.getSelectedItem()); }else { System.out.println(ie); } } }); } { createObjButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { Object newObj=Class.forName(javaTypeInput.getText()).newInstance(); objectTable.put(javaNameInput.getText().intern(),newObj); selector.add(javaNameInput.getText().intern()); selector.setSize(selector.getPreferredSize()); frame.validate(); frame.repaint(); }catch(Exception e) { System.out.println(e); } } }); selector.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if(ie.getStateChange()==ItemEvent.SELECTED) { currentObj=objectTable.get(ie.getItem()); Class cls=currentObj.getClass(); java.lang.reflect.Method[] meths=cls.getMethods(); String[] names=new String[meths.length]; methodNameTable.clear(); methodSelector.removeAll(); for(int i=0;i< meths.length;i++) { names[ i ]=meths[ i ].getName().intern(); methodSelector.add(names[ i ]); methodNameTable.put(names[ i ],meths[ i ]); } methodSelector.setSize(methodSelector.getPreferredSize()); frame.validate(); frame.repaint(); }else{ System.out.println(ie); } } }); methodSelector.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent ie) { if(ie.getStateChange()==ItemEvent.SELECTED) { System.out.println("Going to call "+ie.getItem()+" on object "+selector.getSelectedItem()); java.lang.reflect.Method meth=(java.lang.reflect.Method)methodNameTable.get(ie.getItem()); java.lang.Object obj=currentObj; try { System.out.println("Returned: "+meth.invoke(obj,new Object[0])); }catch(Exception e) { System.out.println(e); } }else{ System.out.println(ie); } } }); } private Frame frame=new Frame("OLEViewer"); { frame.setLayout(new FlowLayout()); frame.add(new Label("Object name:")); frame.add(javaNameInput); frame.add(new Label("Object type:")); frame.add(javaTypeInput); frame.add(createObjButton); frame.add(new Label("Select existing object:")); frame.add(selector); frame.add(new Label("Select method to call:")); frame.add(methodSelector); frame.add(new Label("OLE Functionality follows:")); frame.add(new Label("Select Object:")); frame.add(oleObjectSelector); frame.add(new Label("Enter function/property/application name:")); frame.add(oleFunctionNameInput); frame.add(new Label("Or select function name:")); frame.add(oleFunctionNameSelector); frame.add(new Label("Or select property name:")); frame.add(olePropNameSelector); frame.add(new Label("Or select application name:")); frame.add(oleAppNameSelector); frame.add(new Label("Name to catch return value into:")); frame.add(oleObjectName); frame.add(new Label("Enter a parameter")); frame.add(paramInput); frame.add(executeOperation); frame.add(getProperty); frame.add(createObj); frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { try { wordAppObj.invoke("Quit",new Variant[0]); }catch(Exception e) { System.out.println(e); } System.exit(0); } }); frame.setSize(640,480); } private OleServer oleServer; private OleObject wordAppObj; public OLEView() throws Exception{ oleServer=OleServer.getOleServer(); wordAppObj=oleServer.createOleObject("Word.Application.8",10000); oleObjTable.put("Word.Application.8",wordAppObj); oleObjectSelector.add("Word.Application.8"); oleObjectSelector.setSize(oleObjectSelector.getPreferredSize()); frame.validate(); frame.show(); } public static void main(String[] args) throws Exception{ new OLEView(); } } |
hypptz
一般會員 ![]() ![]() 發表:1 回覆:1 積分:0 註冊:2003-05-21 發送簡訊給我 |
|
neoart
版主 ![]() ![]() ![]() ![]() ![]() 發表:22 回覆:582 積分:425 註冊:2003-05-09 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |