Code:
Packagecom.jrkui.example.excel;
import Java.io.File;
import Org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import Org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import Org.eclipse.swt.ole.win32.OleClientSite;
import Org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.Variant;
import Org.eclipse.swt.program.Program;
import Org.eclipse.swt.widgets.Display;
import Org.eclipse.swt.widgets.Menu;
import Org.eclipse.swt.widgets.Shell;
public class Excelshell {
public static void Main (string[] args) {
new Excelshell (). open ();
}
public void Open ()
{
Display display = Display.getdefault ();
Shell shell = new Shell ();
shell.setsize (600,400);
shell.settext ("Excel window");
shell.setlayout (New Filllayout ());
//Make Excel's menu bar display
Shell.setmenubar (New Menu, SHELL,SWT. BAR));
Createexcelpart (shell);
Shell.open ();
while (!shell.isdisposed ()) {
if (!display.readanddispatch ())
Display.sleep ();
}
Display.close ();
}
/**
* Enables Excel to embed in the shell
* @param Shell
*/
private void Createexcelpart (shell shell)
{
//oleframe is actually a composite that is used to place OLE controls
oleframe oleframe = new Oleframe (SHELL,SWT. NONE);
Oleclientsite provides a place for embedding OLE objects into containers, where "Excel.Sheet" represents an OLE object that is Excel
oleclientsite clientsite = new Oleclientsite (OLEFRAME,SWT. NONE, "Excel.Sheet");
Setvaluefora1cell (Clientsite);
//oleclientsite The action that is done when the OLE object is displayed, the action here is oleiverb_show, showing
Clientsite.doverb (OLE. Oleiverb_show);
}
/**
* Sheet ID
*/
private static final int sheet_id = 0x000001e5;
/**
* Cell ID
*/
private static final int cell_id = 0X000000C5;
/**
* Cell Value ID
*/
private static final int cell_value_id = 0x00000006;
/**
* Assigns a value to the A1 cell of the first sheet page
* @param clientsite
*/
private void Setvaluefora1cell (Oleclientsite clientsite)
{
//Get Excel Workbook Object
oleautomation workbook = new Oleautomation (clientsite);
//Get Workbook's first sheet page
oleautomation sheet = workbook.getproperty (sheet_id,new variant[]{new Variant (1)}). Getautomation ();
//Get the A1 cell of the sheet page
Variant cella1variant = Sheet.getproperty (cell_id, New Variant[]{new variant ("A1")});
oleautomation cellA1 = Cella1variant.getautomation ();
//is assigned to A1 cell
Cella1.setproperty (cell_value_id, New Variant ("Hello ole!");
//Gets the value of the A1 cell and outputs it to the console
System.out.println (cella1variant.getstring ());
}
}