当excel中的数据较多时,我们可以在某列或所有列添加筛选器,对数据进行筛选来获取有用的信息。本文将介绍如何使用spire.xls在excel中添加筛选器、设置筛选项目并执行筛选。
c#
//创建workbook对象
workbook workbook = new workbook();
//加载现有的excel文档
workbook.loadfromfile(@"c:\users\administrator\desktop\sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//获取筛选器对象
autofilterscollection filters = sheet.autofilters;
//设置筛选器的添加位置:第二列
filters.range = sheet.range[1, 2, sheet.lastrow, 2];
//添加筛选项目
filters.addfilter(0, "测试");
//执行筛选
filters.filter();
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2010);
vb.net
'创建workbook对象
dim workbook as new workbook()
'加载现有的excel文档
workbook.loadfromfile("c:\users\administrator\desktop\sample.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取筛选器对象
dim filters as autofilterscollection = sheet.autofilters
'设置筛选器的添加位置:第二列
filters.range = sheet.range(1, 2, sheet.lastrow, 2)
'添加筛选项目
filters.addfilter(0, "测试")
'执行筛选
filters.filter()
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2010)