The operation of calling macros in Excel is implemented through Java code.

Source: Internet
Author: User
Tags assert

The most recent project is to automate the operation of the Fund's trading contracts. Then you'll want to use Java code to manipulate macro buttons in Excel, and then create a word version of the contract.

The specific Java code is as follows:

import Org.junit.assert;import org.junit.Test; Public classExceltoolsservicetest {PrivateExceltoolsservice Service; @Test Public voidTestmacro () {Try{String path="c:/users/admin.admin-pc.000/desktop/vb/merged template/contract template. xlsm";Service =NewExceltoolsservice (); Service.execmacro (Path,"text substitution. Generating a document"); } Catch(Exception e)            {E.printstacktrace ();        Assert.fail (); }    }}


Import com.jacob.activeX.ActiveXComponent;
Import Com.jacob.com.ComThread;
Import Com.jacob.com.Dispatch;
Import com.jacob.com.Variant;

public class Exceltoolsservice {

public void Execmacro (string excelpath, String macroName, Variant ... params) throws Exception {


Activexcomponent excel = null;
Dispatch workbooks = null;
Dispatch workbook = null;
Long start = System.currenttimemillis ();
System.out.println (start);

try {
Comthread.initsta ();

Excel = new Activexcomponent ("Excel.Application");
workbooks = Excel.getproperty ("workbooks"). Todispatch ();
Workbook = Dispatch.call (workbooks, "Open", Excelpath). Todispatch ();
if (null = = Params | | Params.length < 1) {
Dispatch.call (Excel, "Run", New Variant (MacroName));
} else {
Dispatch.call (Excel, "Run", New Variant (MacroName), params);
}
Dispatch.call (Workbook, "Save");
} catch (Exception e) {
E.printstacktrace ();
} finally {
if (null! = Workbook) {//Close document
Dispatch.call (Workbook, "Close", new Variant (false));
}
if (null! = Workbooks) {
Dispatch.call (workbooks, "Close");
}
if (null! = Excel) {
Excel.invoke ("Quit", new variant[] {});
}

Close the Excel process
Comthread.release ();

Long end = System.currenttimemillis ();
SYSTEM.OUT.PRINTLN ("Processing file [{}] macro [{}], time spent {}s" + Excelpath + macroName
+ ((End-start)/1000 + 1));
}
}

}

This is achieved by automating the creation of the Fund's contract through Excel macros.

The operation of calling macros in Excel is implemented through Java code.

Related Article

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.