1

Does anyone know what could be wrong with the following filter I am trying to set for a report generated with apache POI?

I'm using this code:

sheet.setAutoFilter(CellRangeAddress.valueOf("A4:A6"));

To get this result:

Filters applied

However, when I click the arrow to filter my results I also get the value "average" as a possible result in the dropdown list.

Filter list

Is there something I'm missing here?

4

1 回答 1

2

只需在数据区域和页脚行之间添加一个空行。数据区域可以由空行和列/标题限制。

虽然你选择了A4:A6,只是A4被标记为自动过滤器……我不确定,但我认为,你只能通过 VBA 实现水平自动过滤器。所以setAutoFilter应该是这样的A4:B4,因为你只标记标题而不是数据区域。

(使用 POI 3.9、Libre Office 4.0 测试)

import java.io.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.*;

public class Autofilter {
    public static void main(String[] args) throws Exception {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        sheet.createRow(3).createCell(0).setCellValue("Protocol Number");
        sheet.createRow(4).createCell(0).setCellValue("DEMONNUMBER1");
        sheet.createRow(5).createCell(0).setCellValue("DEMONNUMBER2");
        sheet.createRow(6).setZeroHeight(true);
        sheet.createRow(7).createCell(0).setCellValue("Average");
        sheet.setAutoFilter(CellRangeAddress.valueOf("A4:B4"));
        FileOutputStream fos = new FileOutputStream("autofilter.xls");
        wb.write(fos);
        fos.close();
    }
}
于 2013-05-26T20:17:40.150 回答