04-17.eri-test 在Java中将过滤器添加到Excel

当ExcËl工作表中的数据很大时,可以在选定区域中添加“文本过滤器”,“数字过滤器”,“颜色过滤器”或“日期过滤器”以过滤数据。筛选后的工作表将显示您需要的数据,并隐藏其余的数据。

在下面显示的工作表中,您可以在“类别”列中添加文本过滤器,以使工作表仅显示数据的一种类别。您可以在“数量”列中添加数字过滤器,以使工作表显示包含80-100之间数字的行;您可以添加滤色器以显示具有背景色的行;您还可以在“日期”列中添加日期过滤器,以显示特定月份内的数据。

首先,将Spire。Xls。jar文件导入Java应用程序。

AddSpire。Xls。jar作为依赖

方法1:DownloadFreeSpire。XLSforJavapack,unzipitandyou’ll得到Spire。文件。“lib”文件夹中的jar文件。将jar文件作为依赖项导入项目中。

方法2:如果要创建Maven项目,则可以通过将以下配置添加到pom。xml来轻松添加jar依赖项。

<仓库><资料库><ID>com。e-冰蓝</ID><名称>e-冰蓝</名称><网址>http://repo。e-冰蓝。com/nexus/content/groups/public/</网址></资料库></仓库><依存关系><依赖><groupId>e-冰蓝</groupId><artifactId>spire。xls。自由</artifactId><版>2。2。0</版></依赖></依存关系>

Example 1。 文字过滤器

//创建一个工作簿对象工作簿 b =  工作簿();//加载示例Excel文件bloadFromFile“ C:\\ Users \\ Administrator \\ Desktop \\ sample。xlsx”);//获取第一个工作表工作表  = b得到Work片s()。得到0);//获取AutoFiltersCollection对象AutoFiltersCollection 过滤器 = 得到AutoFilters();//设置要添加过滤条件的单元格范围过滤器setRange得到CellRange22165));//将过滤器添加到所选范围的第二列并设置过滤条件,即文本“ C”过滤器addFilter1 “C”);//执行过滤功能过滤器过滤();//保存到文件bsaveToFile“ TextFilter。xlsx” Excel版本2013版);

Example 2。 号码过滤器

//创建一个工作簿对象工作簿 b =  工作簿();//加载示例Excel文件bloadFromFile“ C:\\ Users \\ Administrator \\ Desktop \\ sample。xlsx”);//获取第一个工作表工作表  = b得到Work片s()。得到0);//获取AutoFiltersCollection对象AutoFiltersCollection 过滤器 = 得到AutoFilters();//设置要添加过滤条件的单元格范围过滤器setRangegetCellRange22165));//Add a custom 过滤 to the third column of the selected range and set the 过滤 criteria, which is to select the number greater than or equal to 80 and less than or equal to 100过滤器customFilter2 FilterOperatorType大于等于 80 真正 FilterOperatorType小于等于 100);//执行过滤功能过滤器过滤();//保存到文件bsaveToFile“ NumberFilter。xlsx” Excel版本2013版);

Example 3。 彩色滤光片

//创建一个工作簿对象工作簿 b =  工作簿();//加载示例Excel文件bloadFromFile“ C:\\ Users \\ Administrator \\ Desktop \\ sample。xlsx”);//获取第一个工作表工作表  = bget工作表s()。get0);//获取AutoFiltersCollection对象AutoFiltersCollection 过滤器 = getAutoFilters();//设置要添加过滤条件的单元格范围过滤器setRange.getCellRange22165));//Add a color filter to the first column of the selected range and set the filter criteria, that is, the background color of cell B4过滤器.addFillColorFilter0 .getCellRange“ B4”)。getStyle()。getColor());//执行过滤功能过滤器.filter();//保存到文件b.saveToFile“ ColorFilter.xlsx” Excel版本.2013版);

Alt Text

Example 4. 日期筛选

//创建一个工作簿对象工作簿 b =  工作簿();//加载示例Excel文件b.loadFromFile“ C:\\ Users \\ Administrator \\ Desktop \\ sample.xlsx”);//获取第一个工作表工作表  = b.getWork片s()。get0);//获取AutoFiltersCollection对象AutoFiltersCollection 过滤器 = .getAutoFilters();//设置要添加过滤条件的单元格范围过滤器.setRange.getCellRange22165));//Add a date filter in the fourth column of the selected range, using April 2019年年 as the filter criteria过滤器.addDateFilter3 DateTimeGroupingType. 2019 4 1 0 0 0 );
//执行过滤功能
过滤器.filter();
//保存到文件
b.saveToFile“ DateFilter.xlsx” Excel版本.2013版);

Alt Text

from: https://dev.to//eiceblue/add-filters-to-excel-in-java-cfb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值