i have a class to create formatted table using the code below :
public class formatAsTable {
public static void main(String[] args)
throws FileNotFoundException, IOException {
Workbook wb = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet) wb.createSheet();
XSSFTable my_table = sheet.createTable();
CTTable cttable = my_table.getCTTable();
/* Let us define the required Style for the table */
CTTableStyleInfo table_style = cttable.addNewTableStyleInfo();
table_style.setName("TableStyleMedium9");
table_style.setShowColumnStripes(false); //showColumnStripes=0
table_style.setShowRowStripes(true); //showRowStripes=1
AreaReference my_data_range = wb.getCreationHelper().createAreaReference(new CellReference(0, 0), new CellReference(4, 2));
cttable.setRef(my_data_range.formatAsString());
cttable.setDisplayName("MYTABLE");
cttable.addNewAutoFilter();
cttable.setName("Test");
cttable.setId(1L);
CTTableColumns columns = cttable.addNewTableColumns();
columns.setCount(3L); //define number of columns
for (int i = 0; i < 3; i++) {
CTTableColumn column = columns.addNewTableColumn();
column.setName("Column" + i);
column.setId(i + 1);
}
/* Add And Show TotelRow */
cttable.setTotalsRowShown(true);
for (int x = 0; x < 3; x++) {
cttable.getTableColumns().getTableColumnArray(x).setId(x + 1);
switch (x) {
case 0 ->
cttable.getTableColumns().getTableColumnArray(x).setTotalsRowLabel("Totales: ");
default ->
cttable.getTableColumns().getTableColumnArray(x).setTotalsRowFunction(org.openxmlformats.schemas.spreadsheetml.x2006.main.STTotalsRowFunction.SUM);
}
}
for (int i = 0; i <= 4; i++) //we have to populate 4 rows
{
XSSFRow row = sheet.createRow(i);
for (int j = 0; j < 3; j++) //Three columns in each row
{
XSSFCell localXSSFCell = row.createCell(j);
if (i == 0) {
localXSSFCell.setCellValue("Heading" + j);
} else {
localXSSFCell.setCellValue(i + j);
}
}
}
FileOutputStream fileOut = new FileOutputStream("Excel_Format_As_Table.xlsx");
wb.write(fileOut);
fileOut.close();
}
}
it working fine but even if i set cttable.setTotalsRowShown(true); it is not showing the total row !
And i Must open the file and Chick on Total Row in the table Design to make is visible :
I changed the name and content and tried to add different formats, but no result
