Using OLE in SWT to manipulate Excel (iv): Automatically adjust the width of a cell or column

Source: Internet
Author: User
Tags ole win32

In practical applications, often encounter the value of the cell is relatively long and is obscured, users have to manually adjust the width, if the program will automatically adjust the width will be very convenient. In fact, by OleView.exe This tool query to know the range has a AutoFit method, its ID is 0x000000ed, then if you get a reference to range, as long as the AutoFit this method, you can automatically adjust the width. See the code and example effects below:

package com.jrkui.example.excel;


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.widgets.Display;


import Org.eclipse.swt.widgets.Shell;


publicclass Autofitshell {


publicstaticvoid Main (string[] args) {


new Autofitshell (). open ();


} 


publicvoid Open ()


{


Display display = Display.getdefault ();


Shell shell = new Shell ();


Shell.settext ("Auto fit");


shell.setsize (700, 300);


shell.setlayout (New Filllayout ());


Createexcelpart (shell);


Shell.open ();


while (!shell.isdisposed ())


if (!display.readanddispatch ())


Display.sleep ();


Display.dispose ();


}


privatestaticfinalintsheet_id = 0x000001e5;


privatestaticfinalintcell_id =0x000000c5;


privatestaticfinalintcell_value_id = 0x00000006;


privatevoid Createexcelpart (shell shell)


{


oleframe frame = new Oleframe (SHELL,SWT. NONE);


oleclientsite clientsite = new Oleclientsite (FRAME,SWT. NONE, "Excel.Sheet");


Clientsite.doverb (OLE. Oleiverb_show);


Oleautomation workbook = new Oleautomation (clientsite);


oleautomation worksheet = workbook.getproperty (sheet_id,new variant[]{new Variant (1)}). Getautomation ();


oleautomation cellA3 = Worksheet.getproperty (cell_id,new variant[]{new Variant ("A3")}). Getautomation ();


Cella3.setproperty (cell_value_id, New Variant ("If you don ' t fit the width of the CELL, you couldn ' t-all."));


autofitwidth (cellA3);


//Autofitwidth (Getcolumnofcell (cellA3));


}


publicstaticfinalintauto_fit_range = 0x000000ed;


/**


* Adaptive Width


* @paramautomation


*/


privatevoid autofitwidth (oleautomation automation)


{


//If using Automation.getproperty (Auto_fit_range) is the same effect


Automation.invoke (Auto_fit_range);


}


Publicstaticfinalintcolumn_of_cell = 0x000000f6;


/**


* Get the column that contains the cell


* @paramcell


* @return


*/


private oleautomation Getcolumnofcell (oleautomation cell)


{


return Cell.getproperty (Column_of_cell). Getautomation ();


}


}

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.