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

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

public class CreateWorkbook
{
    public static void main(String[] args)
    {
        try
        {
            // Create the Excel application object
            Excel excel = new Excel();

            try
            {
                // Create a new Excel workbook
                Workbook workbook = excel.getWorkbooks().add();

                // Get the active worksheet in the Excel workbook
                Worksheet worksheet = workbook.getActiveWorksheet();

                // Get the first cell (A1 cell)
                Range cell = worksheet.getRange("A1");
                
                // Put "Hello world" in the fist cell
                cell.setValue("Hello world");
                
                // Fill the cells with numbers
                for(int row=1; row<5; row++)
                {
                    for(int column=0; column<10; column++)
                    {
                        worksheet.getCell(row, column).setValue(row * column);
                    }
                }
    
                // Show the created Excel worksheet 
                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();
        }
    }
}