Java 如何使用ApachePOI对XSSFTable列启用排序/筛选?

Java 如何使用ApachePOI对XSSFTable列启用排序/筛选?,java,excel,apache-poi,xssf,excel-tables,Java,Excel,Apache Poi,Xssf,Excel Tables,我正在开发一个应用程序,它可以获取数据库记录并根据这些数据创建excel文档 excel文档生成良好,所有数据可读;在本论坛的上一个答案中,表格也被适当地生成(即使我滚动过标题行,标题行仍然可见,因此表格肯定存在)。然而,我曾期望,一旦我有了一个表,我就能够像在excel中“插入->表”一样对列进行排序和过滤,但是当我打开文档时,没有这样的选项 在XSSFTable或XSSFTableColumn类中,我没有看到setFitlerable或setSortable或类似的内容。。。如何对表列启用排

我正在开发一个应用程序,它可以获取数据库记录并根据这些数据创建excel文档

excel文档生成良好,所有数据可读;在本论坛的上一个答案中,表格也被适当地生成(即使我滚动过标题行,标题行仍然可见,因此表格肯定存在)。然而,我曾期望,一旦我有了一个表,我就能够像在excel中“插入->表”一样对列进行排序和过滤,但是当我打开文档时,没有这样的选项

在XSSFTable或XSSFTableColumn类中,我没有看到setFitlerable或setSortable或类似的内容。。。如何对表列启用排序/筛选

表格创建代码如下(如果有用):

//Create table
CellReference topLeft = new CellReference(sheet.getRow(3).getCell(0));
CellReference bottomRight = new CellReference(sheet.getRow(nextRow-1).getCell(3));
AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
XSSFTable dataTable = sheet.createTable(tableArea);

dataTable.setName("TableData" + EXCEL_OBJECT_NUMBER);
dataTable.setDisplayName("TableData" + EXCEL_OBJECT_NUMBER);

XSSFTableColumn column = dataTable.getColumns().get(0);
column.setId(1);
column.setName("COLUMN1");

column = dataTable.getColumns().get(1);
column.setId(2);
column.setName("COLUMN2");

column = dataTable.getColumns().get(2);
column.setId(3);
column.setName("COLUMN3");

column = dataTable.getColumns().get(3);
column.setId(4);
column.setName("COLUMN4");

如果
dataTable
XSSFTable
并且
tableArea
是该
XSSFTable
AreaReference
,那么下面的代码将自动筛选设置到表头中,就像
Excel
一样:

dataTable.getCTTable().addNewAutoFilter().setRef(tableArea.formatAsString());
完整示例:

import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.*;
import org.apache.poi.ss.util.AreaReference;
import org.apache.poi.ss.util.CellReference;

import java.util.GregorianCalendar;

class CreateExcelTable {

 public static void main(String[] args) throws Exception {

  Object[][] data = new Object[][] {
   new Object[] {"Text", "Date", "Number", "Boolean"},
   new Object[] {"Text 1", new GregorianCalendar(2020, 0, 1), 1234d, true},
   new Object[] {"Text 2", new GregorianCalendar(2020, 1, 15), 5678d, true},
   new Object[] {"Text 3", new GregorianCalendar(2020, 2, 1), 90.1234, false},
   new Object[] {"Text 4", new GregorianCalendar(2020, 3, 15), 567.89, false}
  };

  try (XSSFWorkbook workbook = new XSSFWorkbook();
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   XSSFCellStyle dateCellStyle = workbook.createCellStyle();
   dateCellStyle.setDataFormat(14);

   XSSFSheet sheet = workbook.createSheet();
   XSSFRow row = sheet.createRow(0);
   XSSFCell cell = row.createCell(0);
   cell.setCellValue("Lorem ipsum");
   row = sheet.createRow(1);
   cell = row.createCell(0);
   cell.setCellValue("semit dolor");

   int nextRow = 3;
   int nextCol = 0;
   for (Object[] dataRow : data) {
    row = sheet.createRow(nextRow++);
    nextCol = 0;
    for (Object value : dataRow) {
     cell = row.createCell(nextCol++);
     if (value instanceof String) cell.setCellValue((String)value);
     else if (value instanceof GregorianCalendar) {
      cell.setCellValue((GregorianCalendar)value);
      cell.setCellStyle(dateCellStyle);
     }
     else if (value instanceof Double) cell.setCellValue((Double)value);
     else if (value instanceof Boolean) cell.setCellValue((Boolean)value);
    }
   }

   CellReference topLeft = new CellReference(sheet.getRow(3).getCell(0));
   CellReference bottomRight = new CellReference(sheet.getRow(nextRow-1).getCell(3));
   AreaReference tableArea = workbook.getCreationHelper().createAreaReference(topLeft, bottomRight);
   XSSFTable dataTable = sheet.createTable(tableArea);
   //dataTable.setName("Table1");
   dataTable.setDisplayName("Table1");

   //this styles the table as Excel would do per default
   dataTable.getCTTable().addNewTableStyleInfo();
   XSSFTableStyleInfo style = (XSSFTableStyleInfo)dataTable.getStyle();
   style.setName("TableStyleMedium2");
   style.setShowColumnStripes(false);
   style.setShowRowStripes(true);

   //this sets auto filters
   dataTable.getCTTable().addNewAutoFilter().setRef(tableArea.formatAsString());

   workbook.write(fileout);
  }

 }
}

我闻到了后续问题的味道。所有这些都已经包含在我的完整示例代码中。