skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 6, 2012

Run Excel Macro from Java

Posted by Raju Gupta at 12:34 AM – 0 comments
 
This is a java code snippet which will run VB script (.vbs file) which in turn will run an excel macro.

1. The excel macro called "Macro3" is embedded in "Raj.xls". This macro will find the first empty row and print "Java Tutorial" for 10 times.

2. There is a vb script file callef 'Raj_vbs.vbs' which will open the excel file and run the macro embedded in it.

3. This vb script will be executed from java using the command 'Runtime.getRuntime().exec("cmd /c Raj_vbs.vbs");'


How to use?
------------------
1. Save the excel file in your desktop.
2. Save the vbs file in your desktop (open the vbs and make sure the path of the excel file is right).
3. Save the Java file in your desktop.
4. Compile the java class
5. Execute the java class.
6. Open the excel file and check whether the macro got executed.

import java.lang.Runtime;
import java.lang.Process;
import java.util.*;

public class CallExcelMacro {

 public static void main(String[] args) {
  try{
          Runtime.getRuntime().exec("cmd /c Raj_vbs.vbs");
  }catch(Exception e)
  { System.out.println(e);}
 }
}

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger