在之前的文章 C# 创建 Excel 命名区域 中,我们介绍了如何使用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)