工作表视图

分页预览

所有工作表都可以以两种模式查看:

  • 普通视图。
  • 分页预览。

普通视图是工作表的默认视图。分页预览是一种编辑视图,显示工作表的打印效果。分页预览显示每页的数据,以便调整打印区域和分页线路。使用Aspose.Cells,开发者可以启用普通视图或分页预览模式。

控制视图模式

Aspose.Cells提供了一个Workbook类,表示Microsoft Excel文件。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。

工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法来管理工作表。要启用普通或分页预览模式,使用Worksheet类的setPageBreakPreview方法。

启用普通视图

使用Worksheet类的setPageBreakPreview方法将任何工作表设置为普通视图,并传递false作为参数。

启用分页预览

使用Worksheet类的setPageBreakPreview方法将任何工作表设置为分页预览,并传递true作为参数。

下面给出一个完整的示例,演示了如何使用Worksheet类的setPageBreakPreview方法为Excel文件的第一个工作表启用分页预览模式。

在下面的截图中,您可以看到Book1.xls文件处于普通视图。

Book1.xls:修改前的工作表

todo:image_alt_text

使用Workbook类打开Book1.xls,并将模式切换为第一个工作表的分页预览。修改后的文件保存为output.xls。

Ouput.xls:修改后的工作表

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(PageBreakPreview.class) + "worksheets/";
// Instantiating a Excel object by excel file path
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Adding a new worksheet to the Workbook object
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
// Displaying the worksheet in page break preview
worksheet.setPageBreakPreview(true);
// Saving the modified Excel file in default format
workbook.save(dataDir + "PageBreakPreview_out.xls");
// Print message
System.out.println("Page break preview is enabled for sheet 1, please check the output document.");

缩放因子

Microsoft Excel提供了一个功能,允许用户设置工作表的缩放比例。此功能帮助用户以更大或更小的视图查看工作表内容。用户可以将缩放因子设置为任何值。

使用Microsoft Excel设置缩放因子

todo:image_alt_text

Aspose.Cells还允许开发人员设置工作表的缩放因子。

控制缩放因子

Aspose.Cells提供了一个Workbook类,表示Microsoft Excel文件。Workbook类包含一个WorksheetCollection,允许访问Excel文件中的每个工作表。

工作表由Worksheet类表示。Worksheet类提供了广泛的属性和方法来管理工作表。要设置工作表的缩放因子,使用Worksheet类的setZoom 方法。

下面给出一个完整的示例,演示了如何使用setZoom 方法设置Excel文件中第一个工作表的缩放因子。

在下面的截图中,您可以看到Book1.xls文件处于默认视图。

Book1.xls:修改前的工作表

todo:image_alt_text

使用 Workbook 类 打开 Book1.xls 文件,并将第一个工作表的缩放系数设置为 75。修改后的文件保存为 output.xls。

Output.xls: 修改后的工作表

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ZoomFactor.class) + "worksheets/";
// Instantiating a Excel object by excel file path
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
// Setting the zoom factor of the worksheet to 75
worksheet.setZoom(75);
// Saving the modified Excel file in default format
workbook.save(dataDir + "ZoomFactor_out.xls");
// Print message
System.out.println("Zoom factor set to 75% for sheet 1, please check the output document.");

冻结窗格

冻结窗格是Microsoft Excel提供的一个功能。冻结窗格允许您在工作表中滚动时选择要保持可见的数据。

在 Microsoft Excel 中使用冻结窗格

todo:image_alt_text

Aspose.Cells 还允许开发人员在运行时将冻结窗格应用于工作表。

Aspose.Cells 提供一个代表 Microsoft Excel 文件的 Workbook 类。Workbook 类包含一个 WorksheetCollection,允许访问 Excel 文件中的每个工作表。

工作表由 Worksheet 类表示。Worksheet 类提供了一系列属性和方法来管理工作表。要配置冻结窗格,请调用 Worksheet 类的 freezePanes 方法。 freezePanes 方法接受以下参数:

  • ,冻结将从该行开始。
  • ,冻结将从该列开始。
  • 冻结行,顶部窗格中可见的行数。
  • 冻结列,左侧窗格中可见的列数

下面给出一个完整示例,展示如何使用 Worksheet 类的 freezePanes 方法来冻结工作表的行和列(从 C4 开始,由第 4 行和第 3 列表示,其中行和列从 0 开始计数)。第一个工作表的 Excel 文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(FreezePanes.class) + "worksheets/";
// Instantiating a Excel object by excel file path
Workbook workbook = new Workbook(dataDir + "book.xls");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
// Applying freeze panes settings
worksheet.freezePanes(3, 2, 3, 2);
// Saving the modified Excel file in default format
workbook.save(dataDir + "FreezePanes_out.xls");
// Print Message
System.out.println("Panes freeze successfull.");

在下面的屏幕截图中,您可以看到没有冻结窗格的 Book1.xls 文件。

Book1.xls: 任何修改之前的工作表视图

todo:image_alt_text

使用 Workbook 类打开 Book1.xls 文件,然后在第一个工作表上冻结了几行和列。修改后的文件保存为 output.xls。

Outlook.xls: 修改后的工作表视图

todo:image_alt_text

拆分窗格

如果您需要拆分屏幕以在同一工作表中获得两个不同的视图,请使用拆分窗格。Microsoft Excel提供了一个非常方便的功能,允许您查看工作表的多个副本,并且您可以独立滚动工作表的每个窗格:拆分窗格。

窗格同时工作。如果您在一个窗格中进行更改,则更改将同时显示在另一个窗格中。Aspose.Cells为用户提供了拆分窗格功能。

应用和移除拆分窗格

拆分窗格

Aspose.Cells提供了代表Microsoft Excel文件的Workbook类。 Workbook类提供了各种属性和方法来管理Excel文件。要实现分割视图,请使用Worksheet类的split方法。要删除分割窗格,请使用removeSplit方法。

在示例中,我们使用一个简单的模板文件进行加载,然后应用拆分窗格功能到第一个工作表的一个单元格上。然后保存更新后的文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(SplitPanes.class) + "worksheets/";
// Instantiate a new workbook
// Open a template file
Workbook book = new Workbook(dataDir + "book.xls");
// Set the active cell
book.getWorksheets().get(0).setActiveCell("A20");
// Split the worksheet window
book.getWorksheets().get(0).split();
// Save the excel file
book.save(dataDir + "SplitPanes_out.xls", SaveFormat.EXCEL_97_TO_2003);
// Print Message
System.out.println("Panes split successfully.");

运行以上代码后,生成的文件具有分割视图。

输出文件中的分割窗格

todo:image_alt_text

移除窗格

开发人员可以使用Worksheet类的removeSplit方法来移除分割窗格。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(RemovePanes.class) + "worksheets/";
// Instantiating a Excel object by excel file path
Workbook workbook = new Workbook(dataDir + "Book1.xls");
// Accessing the first worksheet in the Excel file
WorksheetCollection worksheets = workbook.getWorksheets();
Worksheet worksheet = worksheets.get(0);
worksheet.setActiveCell("A1");
worksheet.removeSplit();
// Saving the modified Excel file in default format
workbook.save(dataDir + "RemovePanes_out.xls");
// Print Message
System.out.println("Panes Removed successfully.");

高级主题