删除 Excel 文件中工作表的现有打印机设置

可能的使用场景

有时开发人员希望阻止 Excel 包含*。垃圾桶*保存的 XLSX 文件中的打印机设置文件。打印机设置文件位于*“[文件“root”]\xl\printerSettings”。*本文档说明如何使用 Aspose.Cells API 删除现有打印机设置。

删除 Excel 文件中工作表的现有打印机设置

Aspose.Cells 允许您删除为 Excel 文件中的不同工作表指定的现有打印机设置。以下示例代码说明了如何删除工作簿中所有工作表的现有打印机设置。请看其示例 Excel 文件, 输出Excel文件,控制台输出以及屏幕截图以供参考。

截屏

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

示例代码

//Source directory
string sourceDir = RunExamples.Get_SourceDirectory();
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
//Load source Excel file
Workbook wb = new Workbook(sourceDir + "sampleRemoveExistingPrinterSettingsOfWorksheets.xlsx");
//Get the sheet counts of the workbook
int sheetCount = wb.Worksheets.Count;
//Iterate all sheets
for (int i = 0; i < sheetCount; i++)
{
//Access the i-th worksheet
Worksheet ws = wb.Worksheets[i];
//Access worksheet page setup
PageSetup ps = ws.PageSetup;
//Check if printer settings for this worksheet exist
if (ps.PrinterSettings != null)
{
//Print the following message
Console.WriteLine("PrinterSettings of this worksheet exist.");
//Print sheet name and its paper size
Console.WriteLine("Sheet Name: " + ws.Name);
Console.WriteLine("Paper Size: " + ps.PaperSize);
//Remove the printer settings by setting them null
ps.PrinterSettings = null;
Console.WriteLine("Printer settings of this worksheet are now removed by setting it null.");
Console.WriteLine("");
}//if
}//for
//Save the workbook
wb.Save(outputDir + "outputRemoveExistingPrinterSettingsOfWorksheets.xlsx");

控制台输出

 PrinterSettings of this worksheet exist.

Sheet Name: Sheet1

Paper Size: PaperLegal

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet2

Paper Size: PaperEnvelopeB5

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet3

Paper Size: PaperA6

Printer settings of this worksheet are now removed by setting it null.

PrinterSettings of this worksheet exist.

Sheet Name: Sheet4

Paper Size: PaperA3

Printer settings of this worksheet are now removed by setting it null.