Jacob operates Word and Excel

Source: Internet
Author: User

jacob's official document: http://danadler.com/jacob/ Microsoft's javasdk document: http://f2.grp.yahoofs.com/v1/IIwuQ7rQyGCn-b-f4biL9n_xAwI_Oh3ieuWVDkL4:PaKBmRUjeUzAtCy5oljOZR1Mrzr0Mzrzr1MrzrzrzrMrZrZrGr /f1.grp.yahoofs.com/v1/IIwuQzpwMrin-b-fbkPTfyGj7CXbfLb0h3lkvGL1GKMwCNpXXRKiKDwe3cx9aUVjO3KWdb7cPJWmqAXUCY_As6XeyLDKoGLYdkezdz.g/s

Note: This compressed package is split. Use type sdkdocs.zip.aa sdkdocs.ab> sdkdocs.zip in the console to merge.
JACOB is a JAVA-COM Bridge that allows you to call COM Automation components from Java. This is the original sentence of the jacob official website, I will not say more. The implementation of jacob is a bit like a collection of jni calls that encapsulate the com function and a container that hosts the com object. Jacob author Dan Adler has written a number of libraries in C ++ to implement reference / bearing / calling to COM, and then uses Java's JNI technology to call these libraries to implement the JAVA-COM Bridge. For how the author encapsulates it, please refer to http://danadler.com/jacob/. Its methods and classes are consistent with Microsoft's javasdk documentation. Interested friends can look. Jacob is a bridge between Java and Microsoft's COM components. By using the DLL dynamic link library that comes with Jacob, the program on the Sun Java platform is called to COM through JNI!
  Download address: http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368 The available version has many functions: it can realize the operation of word and pdf and excel, and conversion! Configuration instructions: Unzip the file: jacob_1.11.1.zip, copy the jacob.jar file to the classpath, copy jacob.dll, place it in the java jdk bin directory, copy template.doc to the web-inf instance 1: (replace text, read The value of the word tag and the UserName property of the application) public static void main (String [] args) {// Start word and generate an ActivexComponent object ActiveXComponent app = new ActiveXComponent ("Word.Application"); // word file to be converted String inFile = "D: //project//java_word//java_word//wordtemplet.doc"; // The target file to be saved String tpFile = "D: //project//java_word//java_word//wordtemplet3.doc "; boolean flag = false; Dispatch xlo = app.getObject (); String oldText =" 1234 "; String newText =" test "; boolean visible = false; String bookMarkKey =" LB_KJGG "; try {// Set word not visible app.setProperty ("Visible", new Variant (visible)); //log.info("Successfully set word invisible! "); System.out.println (" Successfully set word invisible! "); Dispatch docs = app.getProperty ("Documents"). toDispatch (); Dispatch doc = Dispa tch.invoke (docs, "Open", Dispatch.Method, new Object [] {inFile, new Variant (false), new Variant (true)}, new int [1]). toDispatch (); // Open the word file // The application has the UserName attribute in the word2003 vba document. String userName = app.getPropertyAsString ("UserName"); System.out.println ("User name:" + userName); Dispatch selection = app.getProperty ("Selection"). ToDispatch (); // Get a component System. out.println ("Selection"); Dispatch find = app.call (selection, "Find"). toDispatch (); // Find what text Dispatch.put (find, "Text", oldText); // Replace text Dispatch .call (find, "ClearFormatting"); Dispatch.put (find, "Text", oldText); Dispatch.call (find, "Execute"); Dispatch.put (selection, "Text", newText); // Dispatch .call (app, "SaveAs", inFile); System.out.println ("replace"); // Set the specified value to the specified label. Dispatch activeDocument = app.getProperty ("ActiveDocument"). toDispatch ( ); System.out.println ("activedocument"); Dispatch bookMarks = app.call (activeDocument, "Bookmarks"). ToDispatch (); System.out.println ("bookmarks"); boolean bookMarkExist1 = Dispatch.call (bookMarks , "Exists", bookMarkKey) .toBoolean (); if (bookMarkExist1 == true) {System.out.println ("exists bookmark!"); Dispatch rangeItem = Dispatch.call (bookMarks, "Item", bookMarkKey). ToDispatch (); System.out.println ("range item!"); Dispatch range = Dispatch.call (rangeItem, "Range"). ToDispatch (); System. out.println ("range!"); // Get the value of the label String bookMarkValue = Dispatch.get (range, "Text"). toString (); bookMarkValue = "test"; if (bookMarkValue! = null) {Dispatch. put (range, "Text", new Variant (bookMarkValue));}} else {System.out.println ("not exists bookmark!");} // Save the file Dispatch.invoke (doc, "SaveAs", Dispatch. Method, new Object [] {tpFile, new Variant (0)}, new int [1]); // Save to target file as word format Variant f = new Variant (false); Dispatch.call (doc, "Close" , f); flag = true;} catch (Exception e) {e.printStackTrace ();} finally {app.invoke ("Quit", new Variant [] {});} Example 2: (.word->. html) Because Microsoft does not disclose the source code of word, the direct result of reading the word using java stream is garbled. So you have to go through the middle bridge of jacob. Of course, you can also use poi to read. Let me talk about the method of reading with poi. If you use poi to read, you must download tm-extractors-0.4.jar on Baidu to find it. The code is as follows: import org.textmining.text.extraction.WordExtractor; try {FileInputStream fileinputstream = new FileInputStream (filepath); WordExtractor extractor = new WordExtractor (); temp = extractor.extractText (fileinputstream); System.out.println (temp + " == temp "); fileinputstream.close ();} catch (Exception ex) {System.out.println (" FileNotFoundException error "+ ex.getMessage ());} filepath is the word document path and returns a temp string. It is not garbled, but the effect is not satisfactory. Because the word format is lost.
     Besides using jacob. First go to the official website to download: http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=118368 jacob.zip. After downloading, unzip and put jacob.jar in the project / web-inf / lib below. Put jacob .dll under c: / windos / system32 / and java / jdk *. * / Jre / bin. This completes the configuration. Say the code: import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public boolean ChageFormat (String FolderPath, String FileName) {String FileFormat = ""; System.out .println (FolderPath); FileFormat = FileName.substring (FileName.length ()-4, FileName.length ()); System.out.println (FileFormat); if (FileFormat.equalsIgnoreCase (". doc")) {String DocFile = FolderPath + "//" + FileName; System.out.println ("word file path:" + DocFile); // full path of word file String HtmlFile = DocFile.substring (0, (DocFile.length ()- 4)) + ".htm"; System.out.println ("htm file e); // full path of html file ActiveXComponent app = new ActiveXComponent ("Word.Application"); // start word try {app.setProperty ("Visible", new Variant (false)); // Set word program non Visualize Dispatch docs = app.getProperty ("Documents"). ToDispatch (); Dispatch doc = Dispatch.invoke (docs, "Open", Dispatch.Method, new Object [] {DocFile, new Variant (false), new Variant (true)}, new int [1]). toDispatch (); // Open the word file Dispatch.invoke (doc, "SaveAs", Dispatch.Method, new Object [] {HtmlFile, new Variant (8)}, new int [1]); // Save the file as htm Dispatch.call (doc, "Close", new Variant (false)); // Close the file} catch (Exception e) {e.printStackTrace ();} finally { app.invoke ("Quit", new Variant [] {}); // Exit the word program} // Return true after conversion;} return false;} FolderPath is the word storage path. FileName is the word name. In this way, the word file is converted into an htm file. At this time, you can use the stream to read the htm file, and it is not garbled. And with formatting. It should also be emphasized that jacob is related to jdk and windows versions. So the versions must match. Otherwise, an error will be reported. Issues of the version will be tried one by one. There are no shortcuts. Example 2: (copy word content to new word file) import java.io.File; import com.jacob.com. *; Import com.jacob.activeX. *; Public class FileExtracter {public static void main (String [] args) {ActiveXComponent app = new ActiveXComponent ("Word.Application"); String inFile = "c: //test.doc"; String tpFile = "c: //temp.htm"; String otFile = "c: // temp.xml "; boolean flag = false; try {app.setProperty (" Visible ", new Variant (false)); Object docs = app.getProperty (" Documents "). toDispatch (); Object doc = Dispatch.invoke ( docs, "Open", Dispatch.Method, new Object [] {inFile, new Variant (false), new Variant (true)}, new int [1]). toDispatch (); Dispatch.invoke (doc, "SaveAs" , Dispatch.Method, new Object [] {tpFile, new Variant (8)}, new int [1]); Variant f = new Variant (false); Dispatch.call (doc, "Close", f); flag = true;} catch (Exception e) {e.printStackTrace ();} finally {app.invoke ("Quit", new Variant [] {});}}} Example 3: (Generate the form dynamically and fill out the form content and settings Header and footer) import ja va.sql.Timestamp; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob. com.Variant; public class WordExtractor {// Create a component. private ActiveXComponent MsWordApp = null; // create two dispaths. private Dispatch document = null; private Dispatch selection = null; // constructor public WordExtractor () {super ();} / ** * // ** * open word File * @param makeVisible * show or not show (true: show; false: do not show) * / public void openWord (boolean makeVisible) {try {// open word (if word is not open) if (MsWordApp == null ) {MsWordApp = new ActiveXComponent ("Word.Application");} // Set word to be visible or invisible (true: display; false: not display) Dispatch.put (MsWordApp, "Visible", new Variant (makeVisible)) ;} catch (RuntimeException e) {// TODO Auto-generated catch block e.printStackTrace ();}} / ** * // ** * create text content of word * * / public void createNewDocument () {// create A Dispatch object Dispatch documents = Dispatch.get (MsWordApp, "Documents"). ToDispatch (); document = Dispatch.call (documents, "Add"). ToDispatch ();} / ** * // ** * Format Time string * @param date_str * raw time string * @return Modified string * / public String ReplaceDateStr (String date_str) {String str = ""; String [] date_str_arr = Tool.splitString (date_str, "-"); // String [] date_str_arr = null; if (date_str_arr. length> 0) {str = date_str_arr [0] "/" date_str_arr [1] "/" date_str_arr [2];} else {str = date_str;} return str;} / ** * // ** * Write data To word * @param title * article title * @param textToInsertarr * collection of content array to write * / public void insertText (String title, ArrayList textToInsertarr) {selection = Dispatch.get (MsWordApp, "Selection"). ToDispatch ( ); // Objects required for input Dispatch alignment = Dispatch.get (selection, "ParagraphFormat") .toDispatch (); // Objects required for row and column formatting Dispatch font = Dispatch.get (selection, "Font"). ToDispatch (); // Objects needed for font formatting Dispatch font1 = Dispatch.get (selection, "Font"). ToDispatch (); // Objects needed for font formatting // Dispatch image = Dispatch.get (selection, // "InLineShapes"). ToDispatch (); // Put into the graph Objects needed // String mm = // "D:" File.separator "IRMAS_COMBINE" File.separator "images" File.separator "mis_login.jpg"; // // Image source path // Formatting time java.util .Date ddate = new Date (); SimpleDateFormat sdf2 = new SimpleDateFormat ("dd-MMMonth-yy", java.util.Locale.TRADITIONAL_CHINESE); // oracle time format Timestamp ts1 = new Timestamp (ddate.getTime ( )); String date_str = (ts1.toString ()). Substring (0, 10); String result_str = ReplaceDateStr (date_str); // Formatted time 2008/04/27 // File title // Dispatch.call (selection, "TypeParagraph"); // empty line of paragraph Dispatch.put (alignment, "Alignment", "1"); // (1: centered 2: right3: left) Dispatch.put (font1, "Bold", "1"); // font rent Dispatch.put (font1, "Color", "1,0,0,0"); // font color (1,0,0,0 = > Red // 1,1,0,0 => brown) // Dispatch.put (font, "Italic", "1"); // font italic Dispatch.call (selection, "TypeText", title); // write header content // header line Dispatch.call (selection, "TypeParagraph"); // empty Dispatch.put (alignment, "Alignment", "3"); // (1: centered 2: right 3: left) Dispatch.put (selection, "Text", ""); Dispatch.call (selection, "MoveDown"); // cursor next line // insert image / ** // * * Dispatch.call (selection, "TypeParagraph"); Dispatch.put (alignment, * "Alignment", "2" ); // (1: centered 2: right3: left) Dispatch.call (image, * "AddPicture", mm); // Write picture * / / ** // * ----- --- When not entering the form ------------ // main content (i.e. the value in the parameter array) for (int i = 0; i
——

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.