Using OLE in SWT to manipulate Excel (ii): Assigning values to A1 cells in Excel

Source: Internet
Author: User
Tags final ole win32

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 ());


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.