创建访问并复制已命名区域
介绍
通常,列和行标签用于引用单个单元格。可以创建描述性名称来表示单元格、单元格范围、公式或常量值。名称可能指代表示单元格、单元格范围、公式或常量值的一系列字符。为范围分配一个名称意味着可以通过其名称引用该单元格范围。使用易于理解的名称,例如“产品”,来引用难以理解的范围,例如“销售!C20:C30”。标签可以用在引用同一工作表上的数据的公式中;如果要表示另一个工作表上的范围,可以使用名称。*已命名区域是Microsoft Excel的最强大的功能之一,特别是在作为列表控件、数据透视表、图表等的源范围时。
使用Microsoft Excel处理已命名区域
创建已命名范围
以下步骤描述了使用MS Excel为单元格或单元格范围命名的方法。此方法适用于Microsoft Office Excel 2003、Microsoft Excel 97、2000和2002。
- 选择要命名的单元格或单元格范围。
- 单击公式栏左端的名称框。
- 输入单元格的名称。
- 按ENTER。
使用Aspose.Cells处理命名范围
在这里,我们使用Aspose.Cells API来完成任务。 Aspose.Cells提供了一个代表Microsoft Excel文件的类,Workbook。Workbook 类包含一个Worksheets集合,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供Cells集合。
创建已命名范围
通过调用 Cells 集合的重载 CreateRange 方法,可以创建命名范围。 CreateRange 方法的典型版本使用以下参数:
- 左上角单元格的名称,范围中左上角单元格的名称。
- 右下角单元格的名称,范围中右下角单元格的名称。
调用 CreateRange 方法时,它将返回新创建的范围,作为 Range 类的实例。使用此 Range 对象来配置命名范围。例如,使用 Name 属性设置范围的名称。以下示例展示了如何创建跨越 B4:G14 的单元格命名范围。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Creating a named range | |
Range range = worksheet.Cells.CreateRange("B4", "G14"); | |
// Setting the name of the named range | |
range.Name = "TestRange"; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.out.xls"); |
将数据输入到命名范围内的单元格
您可以按照模式将数据插入到范围内单个单元格中
- C#: Range[row,column]
- VB: Range(row,column)
假设您有一个跨越 A1:C4 的命名范围。该矩阵包含 4 * 3 = 12 个单元格。单个范围单元按顺序排列:Range[0,0],Range[0,1],Range[0,2],Range[1,0],Range[1,1],Range[1,2],Range[2,0],Range[2,1],Range[2,2],Range[3,0],Range[3,1],Range[3,2]。
使用以下属性来识别范围中的单元格:
- FirstRow 返回命名范围中第一行的索引。
- FirstColumn 返回命名范围中第一列的索引。
- RowCount 返回命名范围中的总行数。
- ColumnCount 返回命名范围中的总列数。
以下示例显示如何向指定范围的单元格输入一些值。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet in the workbook. | |
Worksheet worksheet1 = workbook.Worksheets[0]; | |
// Create a range of cells based on H1:J4. | |
Range range = worksheet1.Cells.CreateRange("H1", "J4"); | |
// Name the range. | |
range.Name = "MyRange"; | |
// Input some data into cells in the range. | |
range[0, 0].PutValue("USA"); | |
range[0, 1].PutValue("SA"); | |
range[0, 2].PutValue("Israel"); | |
range[1, 0].PutValue("UK"); | |
range[1, 1].PutValue("AUS"); | |
range[1, 2].PutValue("Canada"); | |
range[2, 0].PutValue("France"); | |
range[2, 1].PutValue("India"); | |
range[2, 2].PutValue("Egypt"); | |
range[3, 0].PutValue("China"); | |
range[3, 1].PutValue("Philipine"); | |
range[3, 2].PutValue("Brazil"); | |
// Save the excel file. | |
workbook.Save(dataDir + "rangecells.out.xls"); |
标识命名范围中的单元格
您可以按照以下模式向范围内的单个单元格插入数据:
- C#: Range[row,column]
- VB: Range(row,column)
如果您有一个跨越 A1:C4 的命名范围。该矩阵包含 4 * 3 = 12 个单元格。单个范围单元按顺序排列:Range[0,0],Range[0,1],Range[0,2],Range[1,0],Range[1,1],Range[1,2],Range[2,0],Range[2,1],Range[2,2],Range[3,0],Range[3,1],Range[3,2]。
使用以下属性来识别范围中的单元格:
- FirstRow 返回命名范围中第一行的索引。
- FirstColumn 返回命名范围中第一列的索引。
- RowCount 返回命名范围中的总行数。
- ColumnCount 返回命名范围中的总列数。
以下示例显示如何向指定范围的单元格输入一些值。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting the specified named range | |
Range range = workbook.Worksheets.GetRangeByName("TestRange"); | |
// Identify range cells. | |
Console.WriteLine( "First Row : " + range.FirstRow); | |
Console.WriteLine( "First Column : " + range.FirstColumn); | |
Console.WriteLine( "Row Count : " + range.RowCount); | |
Console.WriteLine( "Column Count : " + range.ColumnCount); |
访问命名范围
访问特定的命名范围
调用 Worksheets 集合的 GetRangeByName 方法,以按指定名称获取范围。典型的 GetRangeByName 方法接受命名范围的名称,并将所指定的命名范围作为 Range 类的实例返回。以下示例显示如何通过名称访问指定的范围。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting the specified named range | |
Range range = workbook.Worksheets.GetRangeByName("TestRange"); | |
if (range != null) | |
Console.WriteLine("Named Range : " + range.RefersTo); |
访问电子表格中的所有命名范围
调用 Worksheet 集合的 GetNamedRanges 方法,获取电子表格中的所有命名范围。GetNamedRanges 方法返回 Worksheets 集合中所有命名范围的数组。
以下示例显示如何访问工作簿中的所有命名范围。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Getting all named ranges | |
Range[] range = workbook.Worksheets.GetNamedRanges(); | |
if(range != null) | |
Console.WriteLine("Total Number of Named Ranges: " + range.Length); |
复制命名范围
Aspose.Cells提供了Range.Copy()方法,用于将具有格式的单元格范围复制到另一个范围。
以下示例显示如何将源单元格范围复制到另一个命名范围。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get all the worksheets in the book. | |
WorksheetCollection worksheets = workbook.Worksheets; | |
// Get the first worksheet in the worksheets collection. | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Create a range of cells. | |
Range range1 = worksheet.Cells.CreateRange("E12", "I12"); | |
// Name the range. | |
range1.Name = "MyRange"; | |
// Set the outline border to the range. | |
range1.SetOutlineBorder(BorderType.TopBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.BottomBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.LeftBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
range1.SetOutlineBorder(BorderType.RightBorder, CellBorderType.Medium, Color.FromArgb(0, 0, 128)); | |
// Input some data with some formattings into | |
// A few cells in the range. | |
range1[0, 0].PutValue("Test"); | |
range1[0, 4].PutValue("123"); | |
// Create another range of cells. | |
Range range2 = worksheet.Cells.CreateRange("B3", "F3"); | |
// Name the range. | |
range2.Name = "testrange"; | |
// Copy the first range into second range. | |
range2.Copy(range1); | |
// Save the excel file. | |
workbook.Save(dataDir + "copyranges.out.xls"); |