使用行和列 GridWeb

插入行和列

本主题说明如何使用 Aspose.Cells.GridWeb API 将新行和列插入到工作表中。可以在工作表中的任何位置插入行或列。

插入行

要在工作表中的任意位置插入一行:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体或页面。
  2. 访问要向其添加行的工作表。
  3. 通过指定要插入行的行索引来插入行。
For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Inserting a new row to the worksheet before 2nd row
sheet.getCells().insertRow(1);

插入列

要在工作表中的任意位置插入一列:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体或页面。
  2. 访问要向其添加列的工作表。
  3. 通过指定要插入列的列索引来插入列。
For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Inserting a new column to the worksheet before column "B"
sheet.getCells().insertColumn(1);

删除行和列

本主题演示如何使用 Aspose.Cells.GridWeb API 从工作表中删除行和列。借助此功能,开发人员可以在运行时删除行或列。

删除行

要从工作表中删除一行:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体或页面。
  2. 访问要从中删除行的工作表。
  3. 通过指定行索引从工作表中删除一行。
For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Deleting 2nd row from the worksheet
sheet.getCells().deleteRow(1);

删除列

要从工作表中删除列:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体或页面。
  2. 访问要从中删除列的工作表。
  3. 通过指定列索引从工作表中删除列。
For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Deleting 2nd column from the worksheet
sheet.getCells().deleteColumn(1);

设置行高和列宽

有时单元格值比它们所在的单元格宽或位于多行上。除非更改行和列的高度和宽度,否则这些值对用户不完全可见。 Aspose.Cells.GridWeb完全支持设置行高和列宽。本主题借助示例详细讨论这些功能。

使用行高和列宽

设置行高

设置行高:

  1. 将 Aspose.Cells.GridWeb 控件添加到您的 Web 窗体/页面。
  2. 访问工作表的 GridCells 集合。
  3. 设置任何指定行中所有单元格的高度。

输出:第 1 行的高度已设置为 50 点

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the cells collection of the worksheet that is currently active
GridCells cells = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex()).getCells();
//Setting the height of 1st row to 50 points
cells.setRowHeight(0, 50);

设置列宽

要设置列的宽度:

  1. 将 Aspose.Cells.GridWeb 控件添加到您的 Web 窗体/页面。
  2. 访问工作表的 GridCells 集合。
  3. 设置任何指定列中所有单元格的宽度。
For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the cells collection of the worksheet that is currently active
GridCells cells = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex()).getCells();
//Setting the width of 1st column to 150 points
cells.setColumnWidth(0, 150);

自定义行和列标题

与 Microsoft Excel 一样,Aspose.Cells.GridWeb 也对行(数字,如 1、2、3 等)和列(按字母顺序,如 A、B、C 等)使用标准标题或标题。 Aspose.Cells.GridWeb 还可以自定义标题。本主题讨论使用 Aspose.Cells.GridWeb API 在运行时自定义行和列标题。

自定义行标题

要自定义行的标题或标题:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体/页面。
  2. 访问 GridWorksheetCollection 中的工作表。
  3. 设置任何指定行的标题。

第 1 行和第 2 行的标题已自定义

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet that is currently active
GridWorksheet worksheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Setting the header of 1st row to "ID"
worksheet.setRowCaption(1, "ID");
//Setting the header of 2nd row to "Name"
worksheet.setRowCaption(2, "Name");

自定义列标题

要自定义列的标题或标题:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体/页面。
  2. 访问 GridWorksheetCollection 中的工作表。
  3. 设置任何指定列的标题。

第 1 列和第 2 列的标题已自定义

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the worksheet that is currently active
GridWorksheet worksheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Setting the header of 1st column to "ID"
worksheet.SetColumnCaption(0, "ID");
//Setting the header of 2nd column to "Name"
worksheet.SetColumnCaption(1, "Name");

冻结和解冻行和列

本主题说明如何冻结和解冻行和列。冻结列或行允许用户在滚动到工作表的其他部分时保持列标题或行标题可见。在处理包含大量数据的工作表时,此功能非常有用。当用户滚动时,只有数据向下滚动,标题保持不变,使日期更容易阅读。仅 Internet Explorer 6.0 或更高版本支持冻结窗格功能。

冻结行和列

要冻结特定数量的行和列:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体/页面。
  2. 访问工作表。
  3. 冻结一些行和列。

处于冻结状态的行和列

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Freezing 4th row and 3rd column
sheet.freezePanes(3, 2, 3, 2);

解冻行和列

解冻行和列:

  1. 将 Aspose.Cells.GridWeb 控件添加到 Web 窗体/页面。
  2. 访问工作表。
  3. 解冻行和列。

解冻后的工作表

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the reference of the worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Unfreezing rows and columns
sheet.unFreezePanes();

保护行和列

本主题讨论了一些技术,用于保护行和列中的单元格免受最终用户执行的任何类型的操作。开发人员可以使用两种技术实现这种保护:将行和列中的单元格设置为只读,或者限制 GridWeb 的上下文菜单选项。

限制上下文菜单选项

GridWeb 提供了一个上下文菜单,最终用户可以使用该菜单对控件执行操作。该菜单提供了许多用于操作单元格、行和列的选项。

完整的上下文选项

待办事项:图片_替代_文本

通过限制上下文菜单中可用的选项,可以限制对行和列的任何类型的客户端操作。这可以通过将 GridWeb 控件的 EnableClientColumnOperations 和 EnableClientRowOperations 属性设置为 false 来完成。也可以通过将 GridWeb 控件的 EnableClientFreeze 属性设置为 false 来限制用户冻结行和列。

限制行和列选项后的上下文菜单

待办事项:图片_替代_文本

For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
//Accessing the first worksheet that is currently active
GridWorksheet sheet = gridweb.getWorkSheets().get(gridweb.getActiveSheetIndex());
//Restricting column related operations in context menu
gridweb.setEnableClientColumnOperations(false);
//Restricting row related operations in context menu
gridweb.setEnableClientRowOperations(false);
//Restricting freeze option of context menu
gridweb.setEnableClientFreeze(false);