使單元格或列自動調整寬度
在實際應用中
package com
import org
import org
import org
import org
import org
import org
import org
import org
import org
publicclass AutofitShell {
publicstaticvoid main(String[] args) {
new AutofitShell()
}
publicvoid open()
{
Display display = Display
Shell shell = new Shell();
shell
shell
shell
createExcelPart(shell);
shell
while(!shell
if(!display
display
display
}
privatestaticfinalintSHEET_ID =
privatestaticfinalintCELL_ID =
privatestaticfinalintCELL_VALUE_ID =
privatevoid createExcelPart(Shell shell)
{
OleFrame frame = new OleFrame(shell
OleClientSite clientSite = new OleClientSite(frame
clientSite
OleAutomation workbook = new OleAutomation(clientSite);
OleAutomation worksheet = workbook
OleAutomation cellA
cellA
autoFitWidth(cellA
// autoFitWidth(getColumnOfCell(cellA
}
publicstaticfinalintAUTO_FIT_RANGE =
/**
*自適應寬度
*@paramautomation
*/
privatevoid autoFitWidth(OleAutomation automation)
{
//如果使用automation
automation
}
publicstaticfinalintCOLUMN_OF_CELL =
/**
*獲得單元格所在的列
*@paramcell
*@return
*/
private OleAutomation getColumnOfCell(OleAutomation cell)
{
return cell
}
}
運行效果(自動調整A
運行效果(去掉createExcelPart()方法中的autoFitWidth(getColumnOfCell(cellA
說明
調整列寬在本例中是通過autoFitWidth(OleAutomation automation)方法
在autoFitWidth(OleAutomation automation)中調用了OleAutomation的invoke()方法
autoFitWidth(OleAutomation automation)的參數是一個Range對象
獲得一個單元格區域的的所在列的方法在Range中是Range* EntireColumn()
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25849.html