JCom(Java-COM bridge)



Sample1:basic usage

import jp.ne.so_net.ga2.no_ji.jcom.excel8.*;
import jp.ne.so_net.ga2.no_ji.jcom.*;
import java.io.File;
import java.util.Date;

/* sample program using Excel lapper */
class testExcel {
    public static void main(String[] args) throws Exception {
        ReleaseManager rm = new ReleaseManager();
        try {
            System.out.println("EXCEL startup...");
            // if already started, open new window
            ExcelApplication excel = new ExcelApplication(rm);
            excel.Visible(true);
            // display any information
            System.out.println("Version="+excel.Version());
            System.out.println("UserName="+excel.UserName());
            System.out.println("Caption="+excel.Caption());
            System.out.println("Value="+excel.Value());

            ExcelWorkbooks xlBooks = excel.Workbooks();
            ExcelWorkbook xlBook = xlBooks.Add();   // create new book
            
            // enumurate all files
            System.out.println("set infomation of files in current directory to cell ...");
            ExcelWorksheets xlSheets = xlBook.Worksheets();
            ExcelWorksheet xlSheet = xlSheets.Item(1);
            ExcelRange xlRange = xlSheet.Cells();

            xlRange.Item(1,1).Value("filename" );
            xlRange.Item(2,1).Value("size" );
            xlRange.Item(3,1).Value("last modified time");
            xlRange.Item(4,1).Value("is directory");
            xlRange.Item(5,1).Value("is file");
            xlRange.Item(6,1).Value("can read");
            xlRange.Item(7,1).Value("can write");

            File path = new File("./");
            String[] filenames = path.list();
            for(int i=0; i<filenames.length; i++) {
                File file = new File(filenames[i]);
                System.out.println(file);
                xlRange.Item(1,i+2).Value( file.getName() );                // filename(no path)
                xlRange.Item(2,i+2).Value( (int)file.length() );            // filesize
                xlRange.Item(3,i+2).Value( new Date(file.lastModified()) ); // last modified time
                xlRange.Item(4,i+2).Value( file.isDirectory()?"Yes":"No" ); // directory ?
                xlRange.Item(5,i+2).Value( file.isFile()?"Yes":"No" );      // file ?
                xlRange.Item(6,i+2).Value( file.canRead()?"Yes":"No" );     // can read ?
                xlRange.Item(7,i+2).Value( file.canWrite()?"Yes":"No" );    // can write ?
            }
            String expression = "=Sum(B2:B"+(filenames.length+1)+")";
            System.out.println("embed equation, calculate sum of filesize: "+expression);
            xlRange.Item(1,filenames.length+2).Value("sum");
            xlRange.Item(2,filenames.length+2).Formula(expression);
            xlRange.Columns().AutoFit();    // fit columns

            // comment out, if print out.
            // output default printer.
//          System.out.println("print out...");
//          xlSheet.PrintOut();

            // comment out, if book save to file.
            // if no path, save to(My Documents)
//          System.out.println("save to file... (My Documents)\\testExcel.xls");
//          xlBook.SaveAs("testExcel.xls");

            xlBook.Close(false,null,false);
            excel.Quit();

            System.out.println("thank you .");
        }
        catch(Exception e) { e.printStackTrace(); }
        finally { rm.release(); }
    }
}

Sample2:generate estimate with Excel

It is assumed that there is a system which makes a certain estimate automatically. Although the system itself is constructed by Java, only the portion of an estimate presupposes that it needs to make from Excel. Excel can make the high sheet of quality and the printing function of Excel is also used this time.

First, please see 'estimate.xls' . This is equivalent to the stencil file of an estimate. Required expression is also embedded and a sushi and the printing range are also setting ending now. This file is once copied by another name, and value is put into a required part to it. And it prints and saves and ends.

All the copied files shall leave. Therefore a file name is made into unique value. Make it better that this is in agreement with an estimate No. They are held until an estimated effective term passes. However, with this sample, it does not mount to there.

With a sample, although not printed in fact, if the following lines are removed from a comment, it will print.
When it comes to actual employment, sending to facsimile or mail instead of a printer is also considered. This sample is contained in the file after JCom Ver2.01. It operates by Excel 97 and 2000.

Sample3:generate XML-file from Excel

Required data are extracted from the estimate of the shoes created with a sample 2, and it outputs to XML. First, Excel file in a current directory is opened, and an assumption example is a reverse method and data are acquired from an applicable cell. Back makes XML file using PrintWriter. Since it is only the output of XML, XML parser is not required of this sample. However, probably, processing of XML->EXCEL is also possible if there is a XML parser.

  • how to run

    Sample4:It is the sample that COM made by VB is called.

    (jcom)/demo/vbcom/

    back