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

import java.util.*;

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

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

            try
            {
                // Create a new Excel workbook and get the active worksheet
                Worksheet worksheet = excel.createWorkbook().getActiveWorksheet();

                Random random = new Random();
                
                // Fill the first column with numbers
                for(int row=0; row<20; row++)
                {
                    worksheet.getCell(row, 0).setValue(random.nextInt(100000));
                }

                // Create a new chart sheet
                Chart chart = new Chart(excel);

                // Set the first column as a source for the chart's data
                chart.setSourceData(worksheet.getRange("A1:A20"));

                // Show the created 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();
        }
    }
}