Solved

JAVA and Excel

Posted on 1998-06-25
2
526 Views
Last Modified: 2008-03-10
How do i go about retrieving information from an excel table using java?
I would like to try an application using servlets, for I am attempting to run it over an intranet.
The data returned after the excel operation will be presented in HTML.
0
Comment
Question by:n1ck
2 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1223770
It is more common to retrieve the table information from Database Management Systems for which you can use JDBC.  Any chance you can save the same info in MS Access instead of Excel?
0
 

Accepted Solution

by:
williacw earned 100 total points
ID: 1223771
You may manipulate EXCEL through its COM interface.
There is no sense in reinventing the wheel, so I'll point you to an appropriate article at the Microsoft Knowledge Base. It is titled Automating EXCEL with Java. It has instructions and sample source code.

Enjoy!
http://support.microsoft.com/support/kb/articles/q169/7/96.asp

In case the link is not working, I've been receiving SERVER BUSY errors at the Microsoft Site, I've attached the text below.

HOWTO: Automate Excel from Java
Last reviewed: January 29, 1998
Article ID: Q169796  
The information in this article applies to:
Microsoft Win32 Virtual Machine for Java
Visual J++ 1.0, 1.1,
SDK for Java 2.0
Microsoft Win32 Virtual Machine for Java
Internet Explorer 3.x,4.0.


SUMMARY
This article illustrates how to call a COM object like Excel from Java. It provides code samples that show how you can make an Excel application visible and open an existing Excel file. There are two code samples. One illustrates how to invoke Excel 7.0 from Java and the other shows how to invoke Excel 8.0 from Java. You will find the classes and interfaces to be quite different in Excel 8.0.



MORE INFORMATION
Before using code samples 1 and 2, follow these steps below:


Use the Java Applet Wizard to create a default Applet or Application.

Run JAVATLB or JACTIVEX(utility similar to JAVATLB and supported by the SDK for Java 2.0)on the Microsoft Excel7.0 Object Library if you are using Excel 7.0 or the Microsoft Excel 8.0 Object Library if you are using Excel 8.0. This will create Java descriptions of the Excel Object.
NOTE: If you are using JACTIVEX, then you will need to build this sample with the JVC.EXE that ships with the SDK2.0 for Java. You can download the SDK for Java 2.0 from http://www.microsoft.com/java/.


Use the import statement to import the classes of the Excel Object Library into your Java project.

Insert either one of the two code snippets in your Java Project to automate Excel from Java.

Code Sample 1

   import xl5en32.*;
   import com.ms.com.*;
   public void TestExcel()
   {
      _Global gbl = (_Global) new _ExcelApplication();
      Variant param = gbl.Application();
      Application app = (Application) param.getDispatch();
      param.putInt(xl5en32.Constants.xlVisible);
      app.putVisible(param);

      Variant vtemp = new Variant();
      vtemp.noParam();

      Variant vtWorkbooks = Dispatch.get(gbl,"Workbooks");
      Workbooks wbs = (Workbooks)vtWorkbooks.getDispatch();

      Variant vtName = new Variant();
      VtName.putStringRef("C:\\book1.xls");

      Variant vtEmpty = new Variant();
      vtEmpty.noParam();
      wbs.Open(vtName,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty,
               vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty,vtEmpty);
   }

Code Sample 2

   import excel8.*;
   import com.ms.com.*;

   _Global globXL=null;
   _Application appXL=null;
   Workbooks books=null;
   _Workbook book = null;

   try{
      globXL = (_Global)new Global();
      appXL = (_Application)globXL.getApplication();
      appXL.putVisible(0,true);
      books = (Workbooks)appXL.getWorkbooks();

      Variant vTemp = new Variant();
      vTemp.putString("c:\\book1.xls");

      Variant vOptional = new Variant();
      vOptional.noParam();
      book =
(_Workbook)books.Open("c:\\book1.xls",vOptional,vOptional,vOptional,vOption al,vOptional,vOptional,vOptional,vOptional,vOptional,vOptional,vOptional,vO ptional,0);

      }
      catch(ComFailException e)
      {
         System.out.println(e.getMessage());
      }

The above code will make the Excel application visible, and it will open up an existing Excel file.


REFERENCES
For the latest Knowledge Base articles and other support information on Visual J++ and the SDK for Java, see the following page on the Microsoft Technical Support site:


   http://support.microsoft.com/support/visualj/
   http://support.microsoft.com/support/java/

Keywords          : kbcode kbinterop kbprg JCOM
Technology        : kbInetDev
Platform          : WINDOWS
Issue type        : kbhowto
 


0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 81
advertisement module in core php 4 159
Java JRE greater than 1.6 5 62
iterator/ListIterator approach 17 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now