在之前的文章 中,我们介绍了如何使用spire.xls创建excel命名区域,这篇文章将介绍如何对现有的excel局部命名区域和全局命名区域进行修改、隐藏名称和删除名称等操作。
1、修改命名区域
局部命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//获取工作表中的指定命名区域
inamedrange namedrange = sheet.names.getbyname("rangename");
//修改命名区域的名称和引用单元格区域
namedrange.name = "new name";
namedrange.referstorange = sheet.range["b2:c2"];
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取工作表中的指定命名区域
dim namedrange as inamedrange = sheet.names.getbyname("rangename")
'修改命名区域的名称和引用单元格区域
namedrange.name = "new name"
namedrange.referstorange = sheet.range("b2:c2")
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
全局命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//获取文档中的指定命名区域
inamedrange namedrange = workbook.nameranges.getbyname("rangename");
//修改命名区域的名称和引用单元格区域
namedrange.name = "new name";
namedrange.referstorange = workbook.worksheets[0].range["b2:c2"];
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取文档中的指定命名区域
dim namedrange as inamedrange = workbook.nameranges.getbyname("rangename")
'修改命名区域的名称和引用单元格区域
namedrange.name = "new name"
namedrange.referstorange = workbook.worksheets(0).range("b2:c2")
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
2、隐藏命名区域的名称
局部命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//获取工作表中的指定命名区域
inamedrange namedrange = sheet.names.getbyname("rangename");
//隐藏命名区域的名称
namedrange.visible = false;
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取工作表中的指定命名区域
dim namedrange as inamedrange = sheet.names.getbyname("rangename")
'隐藏命名区域的名称
namedrange.visible = false
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
全局命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//获取文档中的指定命名区域
inamedrange namedrange = workbook.nameranges.getbyname("rangename");
//隐藏命名区域的名称
namedrange.visible = false;
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取文档中的指定命名区域
dim namedrange as inamedrange = workbook.nameranges.getbyname("rangename")
'隐藏命名区域的名称
namedrange.visible = false
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
3、删除命名区域的名称
局部命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//删除工作表中的指定命名区域的名称
sheet.names.remove("rangename");
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'删除工作表中的指定命名区域的名称
sheet.names.remove("rangename")
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
全局命名区域
c#
//创建workbook实例
workbook workbook = new workbook();
//加载excel文档
workbook.loadfromfile(@"input.xlsx");
//删除文档中的指定命名区域的名称
workbook.nameranges.remove("rangename");
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'加载excel文档
workbook.loadfromfile("input.xlsx")
'删除文档中的指定命名区域的名称
workbook.nameranges.remove("rangename")
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)