在java中使用apache poi库给excel文件设置首行筛选。
问题:
如何使用poi给excel文件的首行设置筛选?
立即学习“Java免费学习笔记(深入)”;
解决方案:
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; public class ExcelFilterFirstRow { public static void main(String[] args) { try ( Workbook wb = new HSSFWorkbook(); // or new XSSFWorkbook(); OutputStream fileOut = Files.newOutputStream(Paths.get("workbook.xls")) ) { Sheet sheet = wb.createSheet(); // 设定区域的自动筛选 sheet.setAutoFilter(CellRangeAddress.valueOf("C5:F200")); // 区域:C5~F200 wb.write(fileOut); } catch (IOException e) { throw new RuntimeException(e); } } }
运行截图:
[图片]