/* This sample starts up a copy of Excel, * DOES NOT make it visible, * puts the value "33" in cell A1, * saves to file C:\JavaExcelSample.xls and * exits the running copy of Excel. */ import excel.*; import java.io.*; import ezjcom.*; public class ExcelSample2 { public static void main(String args[]) { _Application app = null; try { // Instantiate an Excel App Global global = new Global(); // Retrieve the "Application", DO NOT set it visible app = (_Application) global.get_Global().getApplication().get_Application();; app.setVisible( false ); // 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 )); // Print the value of the cell System.out.println( range.getValue()); // Save as C:\JavaExcelSample.xls new java.io.File( "C:\\JavaExcelSample.xls" ).delete(); wks.SaveAs( "C:\\JavaExcelSample.xls" ); System.out.println( "Saved C:\\JavaExcelSample.xls" ); } catch (Exception ex) { ex.printStackTrace(); } finally { // Quit the application if ( app != null ) try { app.Quit(); } catch (Exception ex) { // The catch is to make sure that if app.Quit throws an exception, // we still call JComShutdown! } // Shut down COM connections and release all resources. ezjcom.JComObject.JComShutdown(); } } }