设置打印选项
设置打印选项
这些打印选项允许用户:
- 选择工作表上的特定打印区域。
- 打印标题。
- 打印网格线。
- 打印行/列标题。
- 获得草稿质量。
- 打印注释。
- 打印单元格错误。
- 定义页面排序。
Aspose.Cells支持Microsoft Excel提供的所有打印选项,并且开发人员可以轻松地使用PageSetup类提供的属性来配置工作表的这些选项。下面更详细地讨论了如何使用这些属性。
设置打印区域
默认情况下,打印区域包括包含数据的工作表的所有区域。开发人员可以为工作表确定特定的打印区域。
要选择特定的打印区域,请使用PageSetup类的PrintArea属性。将定义打印区域的单元格范围分配给此属性。
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the PageSetup of the worksheet | |
PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
// Specifying the cells range (from A1 cell to T35 cell) of the print area | |
pageSetup.PrintArea = "A1:T35"; | |
// Save the workbook. | |
workbook.Save(dataDir + "SetPrintArea_out.xls"); |
设置打印标题
Aspose.Cells允许您指定要在打印的工作表的所有页面上重复显示的行和列标题。为此,请使用PageSetup类的PrintTitleColumns和PrintTitleRows属性。
要重复显示的行或列是通过传递它们的行号或列号来定义的。例如,行被定义为 $1:$2,列被定义为 $A:$B。
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the PageSetup of the worksheet | |
Aspose.Cells.PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
// Defining column numbers A & B as title columns | |
pageSetup.PrintTitleColumns = "$A:$B"; | |
// Defining row numbers 1 & 2 as title rows | |
pageSetup.PrintTitleRows = "$1:$2"; | |
// Save the workbook. | |
workbook.Save(dataDir + "SetPrintTitle_out.xls"); |
设置其他打印选项
PageSetup类还提供了几个其他属性,以设置一般的打印选项,如下:
- PrintGridlines:一个布尔属性,定义是否打印网格线或不打印。
- PrintHeadings:一个布尔属性,定义是否打印行和列标题或不打印。
- BlackAndWhite:一个布尔属性,定义是否以黑白模式打印工作表或不打印。
- PrintComments:定义是否在工作表上显示打印批注还是在工作表末尾显示。
- PrintDraft:一个布尔属性,定义是否打印不带图形的工作表。
- PrintErrors:定义是否按显示的方式、空白、短横线或N/A打印单元格错误。
要设置PrintComments和PrintErrors属性,Aspose.Cells还提供了两个枚举PrintCommentsType和PrintErrorsType,其中包含要分配给PrintComments和PrintErrors属性的预定义值。
PrintCommentsType枚举中的预定义值如下所示。
打印备注类型 | 描述 |
---|---|
PrintInPlace | 指定将批注打印为显示在工作表上的形式。 |
PrintNoComments | 指定不打印批注。 |
PrintSheetEnd | 指定将批注打印在工作表末尾。 |
PrintErrorsType 枚举的预定义值如下所示,并附有其描述。
打印错误类型 | 描述 |
---|---|
PrintErrorsBlank | 指定不打印错误。 |
PrintErrorsDash | 指定打印错误为"–"。 |
PrintErrorsDisplayed | 指定打印错误为显示的形式。 |
PrintErrorsNA | 指定打印错误为"#N/A"。 |
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the PageSetup of the worksheet | |
PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
// Allowing to print gridlines | |
pageSetup.PrintGridlines = true; | |
// Allowing to print row/column headings | |
pageSetup.PrintHeadings = true; | |
// Allowing to print worksheet in black & white mode | |
pageSetup.BlackAndWhite = true; | |
// Allowing to print comments as displayed on worksheet | |
pageSetup.PrintComments = PrintCommentsType.PrintInPlace; | |
// Allowing to print worksheet with draft quality | |
pageSetup.PrintDraft = true; | |
// Allowing to print cell errors as N/A | |
pageSetup.PrintErrors = PrintErrorsType.PrintErrorsNA; | |
// Save the workbook. | |
workbook.Save(dataDir + "OtherPrintOptions_out.xls"); |
设置页面顺序
PageSetup 类提供了Order 属性,用于对工作表的多个页面进行打印排序。有两种排序页面的可能性如下。
- 先向下再向右: 在打印右侧页面之前,将所有页面向下打印。
- 先向右再向下: 在打印下方页面之前,从左到右打印页面。
Aspose.Cells提供一个枚举PrintOrderType,其中包含所有预定义的排序类型。
PrintOrderType 枚举的预定义值如下所示。
打印顺序类型 | 描述 |
---|---|
DownThenOver | 表示打印顺序为先向下再向右。 |
OverThenDown | 表示打印顺序为先向右再向下。 |
// 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); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Obtaining the reference of the PageSetup of the worksheet | |
PageSetup pageSetup = workbook.Worksheets[0].PageSetup; | |
// Setting the printing order of the pages to over then down | |
pageSetup.Order = PrintOrderType.OverThenDown; | |
// Save the workbook. | |
workbook.Save(dataDir + "SetPageOrder_out.xls"); |