行和列的分组和取消分组
介绍
在 Microsoft Excel 文件中,您可以创建一个数据大纲,以便通过单击鼠标来显示和隐藏不同级别的细节。
单击大纲符号1、2、3、+和- 快速显示工作表中仅提供摘要或标题部分的行或列,或者您可使用符号查看摘要或标题下的详细信息,如下图所示:
分组行和列 |
---|
![]() |
行和列的分组管理
Aspose.Cells提供一个类,Workbook表示Microsoft Excel文件。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。工作表由Worksheet类表示。Worksheet类提供一个Cells集合,表示工作表中的所有单元格。
Cells集合提供了几种管理工作表行或列的方法,以下将更详细地讨论其中的一些。
分组行和列
通过调用Cells集合的GroupRows和GroupColumns方法,可以对行或列进行分组。这两种方法都带有以下参数:
- 第一个行/列索引,即组中的第一行或列。
- 最后一个行/列索引,即组中的最后一行或列。
- 是否隐藏,一个布尔参数,指定是否在分组后隐藏行/列。
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows (from 0 to 5) and making them hidden by passing true | |
worksheet.Cells.GroupRows(0, 5, true); | |
// Grouping first three columns (from 0 to 2) and making them hidden by passing true | |
worksheet.Cells.GroupColumns(0, 2, true); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |
分组设置
Microsoft Excel 允许您配置用于显示的分组设置:
- 详细信息下面的摘要行。
- 详细信息右侧的摘要列。
开发者可以使用Worksheet类的Outline属性配置这些组设置。
将摘要行显示在详细信息下方
可以通过将Outline类的SummaryRowBelow属性设置为true或false来控制是否在详细信息下方显示摘要行。
// 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); | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows and first three columns | |
worksheet.Cells.GroupRows(0, 5, true); | |
worksheet.Cells.GroupColumns(0, 2, true); | |
// Setting SummaryRowBelow property to false | |
worksheet.Outline.SummaryRowBelow = false; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); |
将摘要列显示在详细信息右侧
开发者还可以通过将Outline类的SummaryColumnRight属性设置为true或false来控制是否在详细信息右侧显示摘要列。
// 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); | |
Workbook workbook = new Workbook(dataDir + "sample.xlsx"); | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Grouping first six rows and first three columns | |
worksheet.Cells.GroupRows(0, 5, true); | |
worksheet.Cells.GroupColumns(0, 2, true); | |
worksheet.Outline.SummaryColumnRight = true; | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); |
取消分组行和列
要取消任何已分组的行或列,调用Cells集合的UngroupRows和UngroupColumns方法。这两个方法都带有两个参数:
- 第一个行或列索引,即要取消分组的第一行/列。
- 最后一个行或列索引,即要取消分组的最后一行/列。
UngroupRows有一个额外的重载,它带有一个布尔型第三个参数。将其设置为true会移除所有分组信息。否则,只有外部的组信息会被移除。
// 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); | |
// Creating a file stream containing the Excel file to be opened | |
FileStream fstream = new FileStream(dataDir + "book1.xls", FileMode.Open); | |
// Instantiating a Workbook object | |
// Opening the Excel file through the file stream | |
Workbook workbook = new Workbook(fstream); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Ungrouping first six rows (from 0 to 5) | |
worksheet.Cells.UngroupRows(0, 5); | |
// Ungrouping first three columns (from 0 to 2) | |
worksheet.Cells.UngroupColumns(0, 2); | |
// Saving the modified Excel file | |
workbook.Save(dataDir + "output.xls"); | |
// Closing the file stream to free all resources | |
fstream.Close(); |