在GridDesktop工作表中放大或缩小视图

使用Aspose.Cells.GridDesktop进行放大或缩小视图

Aspose.Cells提供Aspose.Cells.GridDesktop.Worksheet类,该类具有广泛的属性和方法,用于管理工作表。 要设置工作表的缩放因子,请使用工作表类的Zoom属性。 通过向Zoom属性分配数字(整数)值来设置缩放因子。

我们使用TrackBar(.NET)控件构建类似于MS Excel的缩放滑块。 在WinForm项目中,我们将工具箱中的Aspose.Cells.GridDesktop控件放置到表单中,并指定一些属性以设置其名称,大小或其他方面。 现在,我们将TrackBar控件放置在位于GridDesktop控件下方的右下角,还会放置一个Label控件,用于显示通过TrackBar控件的手柄指定的百分比值。 我们在TrackBar的滚动事件中添加相关代码线,因此当您滚动Trackbar控件时,GridDesktop应放大或缩小以显示其中的数据/内容。

下面提供了一个完整的示例,演示如何使用Zoom属性来设置GridDesktop的活动工作表的缩放因子。 我们首先将一个模板Excel文件导入到GridDesktop中。

写下面的代码在窗体的Load事件中,将模板Excel文件设置为GridDesktop和trackbar值。

// 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 = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Importing the template Excel file to GridDesktop
gridDesktop1.ImportExcelFile(dataDir + "EmployeeSales.xlsx");
// Set the default value of the TrackBar control
trackBar1.Value = 100;
// Set the custom label's text to the trackbar's value for display
label1.Text = trackBar1.Value.ToString() + "%";

现在将下面的代码复制到跟踪滚动事件中并运行应用程序。 您会注意到移动跟踪条会更改工作表的缩放属性。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Set the Zoom factor of the active worksheet to the Trackbar's value
gridDesktop1.Worksheets[gridDesktop1.GetActiveWorksheet().Index].Zoom = trackBar1.Value;
// Show the percentage value of the specified Zoom
label1.Text = trackBar1.Value.ToString() + "%";