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