我们可以在excel表格的最下方添加一个汇总行来快速汇总数据。本文将介绍如何使用spire.xls给excel表格添加汇总行。
示例excel文档如下:
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文件
workbook.loadfromfile("input.xlsx");
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//使用指定的数据范围创建表格
ilistobject table = sheet.listobjects.create("table", sheet.range["a1:d4"]);
//显示汇总行
table.displaytotalrow = true;
//添加汇总行
table.columns[0].totalsrowlabel = "total";
table.columns[1].totalscalculation = exceltotalscalculation.sum;
table.columns[2].totalscalculation = exceltotalscalculation.sum;
table.columns[3].totalscalculation = exceltotalscalculation.sum;
//保存文件
workbook.savetofile("addtotalrow.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文件
workbook.loadfromfile("input.xlsx")
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'使用指定的数据范围创建表格
dim table as ilistobject = sheet.listobjects.create("table", sheet.range("a1:d4"))
'显示汇总行
table.displaytotalrow = true
'添加汇总行
table.columns(0).totalsrowlabel = "total"
table.columns(1).totalscalculation = exceltotalscalculation.sum
table.columns(2).totalscalculation = exceltotalscalculation.sum
table.columns(3).totalscalculation = exceltotalscalculation.sum
'保存文件
workbook.savetofile("addtotalrow.xlsx", excelversion.version2013)
效果图: