Using jcom to implement imitation Excel programming in Java

Source: Internet
Author: User

Use jcom and JXL in Java to note the main points:

(1) You must have Jdom-1.0.jar,jxl-2.5.5.jar,jcom-2.2.4.jar,jcom.dll under your lib.

(2) to put the Jcom.dll in your JDK's Bin directory or on the system disk under Windows (recommended to the JDK under the bin)

After putting all the jar packs in place, start writing the utility class in the application (stating that the IDispatch object is in the jcom and that the following throws a Jcomexception exception)

(1) First of all, you should determine whether a pass in the path is word or Excel or other dongdong.

if(path.endsWith(".doc")){
......
}

Note: If the Excel suffix is ". xls";

(2) Then you need to have two objects:

ReleaseManager rm = new ReleaseManager();
IDispatch xlsApp=null;

Note: Releasemanager is equivalent to a container that interacts with all jcom on your machine, and according to the parameters you pass on, he will look for all the jcom that you can do on your machine.

IDispatch can be understood as an object, all the Dongdong are objects;

(3) You have to let him know that you want to interact with Excel.

Xlsapp = new IDispatch (rm, "Excel.Application");

(4) next to get a workbooks (work thin)

IDispatch Excel = (IDispatch) xlsapp.get ("workbooks");

(5) To set whether the current object is visible

Xlsapp.put ("Visible", new Java.lang.Boolean (false));

(6) To open after getting the workbook

IDispatch workbook = (IDispatch) excel.method ("Open", new object[] {FilePath (path that Excel stores)});

(7) Determine if the file exists if it exists then delete

File f = new File(outPath);
if (f.exists())
f.delete();

(8) Save the workbook as

Workbook.method ("SaveAs", new object[] {outpath, new Integer (9)});

(9) Get all worksheets under a workbook (workbook) (Sheets)

IDispatch sheets = (IDispatch) workbook.get ("sheets");

Note: The obtained is an array;

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.