本文将介绍如何使用spire.xls for .net删除excel工作表中的重复行。
原excel文档如下:
c#
using spire.xls;
using system.linq;
namespace removeduplicaterows
{
class program
{
static void main(string[] args)
{
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile("test.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//指定需要删除重复行的单元格范围
var range = sheet.range["a1:a" sheet.lastrow];
//获取重复行的行号
var duplicatedrows = range.rows
.groupby(x => x.columns[0].displayedtext)
.where(x => x.count() > 1)
.selectmany(x => x.skip(1))
.select(x => x.columns[0].row)
.tolist();
//删除重复行和空白行
for (int i = 0; i < duplicatedrows.count; i )
{
sheet.deleterow(duplicatedrows[i] - i);
}
//保存结果文档
workbook.savetofile("output.xlsx", excelversion.version2013);
}
}
}
vb.net
imports spire.xls
imports system.linq
namespace removeduplicaterows
class program
private shared sub main(byval args as string())
dim workbook as workbook = new workbook()
workbook.loadfromfile("test.xlsx")
dim sheet as worksheet = workbook.worksheets(0)
dim range = sheet.range("a1:a" & sheet.lastrow)
dim duplicatedrows = range.rows.groupby(function(x) x.columns(0).displayedtext).where(function(x) x.count() > 1).selectmany(function(x) x.skip(1)).[select](function(x) x.columns(0).row).tolist()
for i as integer = 0 to duplicatedrows.count - 1
sheet.deleterow(duplicatedrows(i) - i)
next
workbook.savetofile("output.xlsx", excelversion.version2013)
end sub
end class
end namespace