Sub Process_Globals
Private fx As JFX
End Sub
Sub AppStart (Form1 As Form, Args() As String)
GenerateTable
End Sub
Sub GenerateTable
Dim docX As JavaObject
docX.InitializeNewInstance("org.apache.poi.xwpf.usermodel.XWPFDocument", Null)
docX.RunMethod("createTable",Array(2,2))
Dim tables As List = docX.RunMethod("getTables", Null)
If tables.IsInitialized Then
For Each table As JavaObject In tables
table.RunMethod("setWidthType",Array("DXA")) 'set to 1/20 of a point
'bodyelement.RunMethod("setWidth",Array(7000)) 'whole table
Dim columnwidth(2) As Int
columnwidth(0)=2200
columnwidth(1)=3800
Dim nativeMe As JavaObject=Me
nativeMe.RunMethod("setTableColumnsWidth", Array(table,columnwidth))
Next
End If
Dim out As OutputStream = File.OpenOutput("C:\Work", "table.docx", False)
docX.RunMethod("write", Array(out))
out.Close
End Sub
#If java
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import java.math.BigInteger;
public static void setTableColumnsWidth(XWPFTable table, int... widths) throws Exception {
CTTblGrid grid = table.getCTTbl().addNewTblGrid();
int col=0;
for (int w : widths) {
grid.addNewGridCol().setW(BigInteger.valueOf(w));
CTTblWidth tblWidth = table.getRow(0).getCell(col).getCTTc().addNewTcPr().addNewTcW();
tblWidth.setW(BigInteger.valueOf(w));
col++;
}
}
#End If