/* This sample starts up a copy of Excel, * makes it visible, * opens the file C:\JavaExcelSample.xls, * puts a formula in B1 and one in B2, * and copies the formulae to C1:F1 and B2:D5. * Leaves Excel running for the user. */ import excel.*; import java.io.*; import ezjcom.*; public class ExcelSample3 { 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 ); // Open a previously saved file _Workbook wb = app.getWorkbooks().Open( "C:\\JavaExcelSample.xls" ).get_Workbook(); // Retrieve the worksheet _Worksheet wks = (_Worksheet) app.getActiveSheet(); // 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 )); } catch (Exception ex) { ex.printStackTrace(); } finally { // Shut down COM connections and release all resources. ezjcom.JComObject.JComShutdown(); } } }