Moyosoft Products | Services | Download | Contact us    
Java Excel Connector
Products  >  Java Excel Connector  >  Example source code    
package com.moyosoft.samples.excel;

import java.awt.*;
import java.io.*;

import com.moyosoft.connector.com.*;
import com.moyosoft.connector.exception.*;
import com.moyosoft.connector.ms.excel.*;

public class OpenWorkbook
{
    public static void main(String[] args)
    {
        try
        {
            FileDialog fileDialog = new FileDialog(new Frame());
            fileDialog.show();
            
            String selectedFile = fileDialog.getFile();
            
            if(selectedFile != null)
            {
                // Create the Excel application object
                Excel excel = new Excel();

                try
                {
                    // Open the selected Excel document
                    Workbook workbook = excel.openWorkbook(new File(fileDialog.getDirectory(), selectedFile));
                    
                    // Print the workbook name
                    System.out.println("The workbook name is: " + workbook.getName());
                    
                    // Show the Excel workbook 
                    excel.setVisible(true);
                }
                finally
                {
                    // Dispose the library
                    excel.dispose();
                }
            }
        }
        catch (ComponentObjectModelException ex)
        {
            System.out.println("COM error has occured: ");
            ex.printStackTrace();
        }
        catch (LibraryNotFoundException ex)
        {
            // If this error occurs, verify the file 'moyocore.dll' is present
            // in java.library.path
            System.out.println("The Java Excel Library hasn't been found.");
            ex.printStackTrace();
        }
    }
}