C # Operation Excel named ranges (NamedRange): create, change, delete named ranges

Create a named range refers to a selected cell or multiple ranges set the name, the purpose is to help us elsewhere in the document referenced or cited equation can be simplified to facilitate data management of the range of cells. The following describes a specific example of code. Created here named area into two cases, each corresponding to a different method of adding the named region and a reference range, that is:

  1. Universal Naming zone (Method: workbook.NameRanges.Add ())
  2. Local named range (Method: sheet.Names.Add ())

 

At the same time , for existing Excel spreadsheet named range, we can also be modified or deleted, including

  • Change the name of the region
  • Specifies the name of the hidden named range
  • Delete the specified region name

Use tools : Free Spire.XLS for .NET (Free)

 

Note: Download Free Spire.XLS, extract the installation. After installation, when editing the code, note that adding a reference Spire.Xls.dll (below) in the program, dll files Bin file in the installation folder path acquisition.



 

 

Code Example (for reference)

 

[Example 1] create Excel named ranges

 

Spire.Xls the using; 
the using Spire.Xls.Core; 

namespace CreateNamedRange_XLS 
{ 
    class Program 
    { 
        static void the Main (String [] args) 
        { 
            // Create instance Workbook 
            Workbook Workbook Workbook new new = (); 
            // loading Excel files 
            workbook.LoadFromFile ( "test.xlsx"); 

            // Get a first sheet 
            the worksheet sheet workbook.Worksheets = [0]; 

            // global name defines a first zone 
            INamedRange NamedRange1 = workbook.NameRanges.Add ( "first batch storage amount "); // named range defined in this way, when the reference is applicable to the entire workbook 
            // global name defined in the second zone 
            // INamedRange NamedRange1 = sheet.Names.Add (" first batch storage amount "); // this method defined named area, when referencing applies only to the area where the worksheet named
            = Sheet.Range NamedRange1.RefersToRange [ "G3: the G7"];

            INamedRange NamedRange2 = workbook.NameRanges.Add ( "second batch storage amount"); 
            // INamedRange NamedRange2 = sheet.Names.Add ( "second batch storage amount"); 
            NamedRange2.RefersToRange = sheet.Range [ "for G8: G12"]; 

            // specified cell write text 
            sheet.Range [ "A14"] text = " total amount of storage."; 

            // references a named area in the formula 
            sheet.Range [ "B14"]. Formula = "= SUM (storage amount of the first batch, the second batch the amount of storage)"; 

            // save the document 
            workbook.SaveToFile ( "result.xlsx", ExcelVersion.Version2013); 
            the System.Diagnostics.Process.Start ( "result.xlsx"); 
        } 
    } 
}

 

Create effects:


 

 

[Example 2] to change the Excel named ranges

 

using Spire.Xls;
using Spire.Xls.Core;

namespace ModifyNameRange_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建文档,加载测试文件
            Workbook wb = new Workbook();
            wb.LoadFromFile("sample.xlsx");

            //获取第一张工作表
            Worksheet sheet = wb.Worksheets[0];

            //获取表格中的指定命名区域
            INamedRange namedRange = wb.NameRanges.GetByName("第1批次入库量");//适用于全局命名区域(整个工作簿)
            //INamedRange namedRange = sheet.Names.GetByName("第2批次入库量");//适用于局部命名区域(指定工作表)

            //修改命名区域的名称和引用单元格区域
            namedRange.Name = "newrange";
            namedRange.RefersToRange = wb.Worksheets[0].Range["G3:G7"];//适用于全局命名区域(整个工作簿)
            //namedRange.RefersToRange = sheet.Range["D3:D8"];//适用于局部命名区域(指定工作表)

            //获取指定命名区域,并设置区域名称隐藏
            namedRange = wb.NameRanges.GetByName("第2批次入库量");
            namedRange.Visible = false;

            ////删除文档中的指定命名区域的名称
            //wb.NameRanges.Remove("RangeName");//适用于全局命名区域(整个工作簿)
            ////sheet.Names.Remove("RangeName");//适用于局部命名区域(指定工作表)

            //保存文档
            wb.SaveToFile("newresult.xlsx", FileFormat.Version2010);
            System.Diagnostics.Process.Start("newresult.xlsx");
        }
    }
}

 测试文档:


 

修改结果:


 

(本文完)

 

 

Guess you like

Origin www.iteye.com/blog/miaonly-2443753