/* This sample starts up a copy of Excel, * makes it visible, * populates in with data, * and creates a chart from cells B1:D5. * Leaves Excel running for the user. */ import excel.*; import java.io.*; import ezjcom.*; public class ExcelSample4 { public static void main(String args[]) { try { // Instantiate an Excel App Global global = new Global(); // Retrieve the "_Application" interface, and set it visible _Application app = (_Application) global.get_Global().getApplication().get_Application(); app.setVisible( true ); // Add an initial workbook _Workbook wb = app.getWorkbooks().Add().get_Workbook(); // Retrieve the active "Worksheet" _Worksheet wks = (_Worksheet) app.getActiveSheet(); // Retrieve a Range that refers to the cell A1 Range range = wks.getRange( new JComVariant( "A1" )); // Set the value of the cell to "33" range.setValue( new JComVariant( 33 )); // Retrieve Ranges that refer to the cell B1, B2 Range b1 = wks.getRange( new JComVariant( "B1" )); Range b2 = wks.getRange( new JComVariant( "B2" )); // Put formulae in B1, B2 b1.setValue( new JComVariant( "=A1+1" )); b2.setValue( new JComVariant( "=B1+1" )); // Copy b1 to c1:f1 and copy b2 to b2:f5 Range c1f1 = wks.getRange( new JComVariant( "C1:F1" )); Range b2f5 = wks.getRange( new JComVariant( "B2:F5" )); b1.Copy( new JComVariant( c1f1 )); b2.Copy( new JComVariant( b2f5 )); // Now add a chart. ChartObjects chartObjects = (ChartObjects) wks.ChartObjects(); ChartObject chartObject = chartObjects.Add( 50.0, 70.0, 200.0, 140.0 ); _Chart chart = chartObject.getChart().get_Chart(); chart.ChartWizard( new JComVariant( (Range) wks.getRange( new JComVariant( "B2:F5" )))); // Note: There are multiple overloads of "ChartWizards", that can // be used to customize the charts. // // The "chart" object can also be directly used to customize the charts. // E.g. to add a title to the chart: // // chart.setHasTitle( true ); // ChartTitle ct = chart.getChartTitle(); // ct.setText( "My Chart Title" ); // } catch (Exception ex) { ex.printStackTrace(); } finally { // Shut down COM connections and release all resources. ezjcom.JComObject.JComShutdown(); } } }