Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Excel自动化:有没有办法知道一张工作表将打印多少页?_C#_Excel - Fatal编程技术网

C# Excel自动化:有没有办法知道一张工作表将打印多少页?

C# Excel自动化:有没有办法知道一张工作表将打印多少页?,c#,excel,C#,Excel,有时,纸张太大,无法打印出来,因此如果是这样,我希望避免打印 我目前的计划: 打开excel文档 复印第一页 创建新文档 在新文档中插入文本和公式 在显示文本时对列进行自动调整 打印文本(尚未实现) 显示公式时对列进行自动拟合 打印公式(尚未实现) 复制/粘贴的原因是即使文档/工作表/单元格使用密码进行写保护,也能够自动调整列 但是我想避免打印它,例如,如果当前的工作表超过100页。我怎么检查这个 // Open Excel xlApp = new Excel.A

有时,纸张太大,无法打印出来,因此如果是这样,我希望避免打印

我目前的计划:

  • 打开excel文档
  • 复印第一页
  • 创建新文档
  • 在新文档中插入文本和公式
  • 在显示文本时对列进行自动调整
  • 打印文本(尚未实现)
  • 显示公式时对列进行自动拟合
  • 打印公式(尚未实现)
  • 复制/粘贴的原因是即使文档/工作表/单元格使用密码进行写保护,也能够自动调整列

    但是我想避免打印它,例如,如果当前的工作表超过100页。我怎么检查这个

            // Open Excel
            xlApp = new Excel.ApplicationClass();
            xlApp.Visible = true;
    
            // Open document
            xlWorkBook = xlApp.Workbooks.Open(filename, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
    
            // Select the sheet we want to copy
            Excel.Sheets xlSheets = null;
            xlSheets = xlWorkBook.Sheets as Excel.Sheets;
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
    
            // Mark all cells and copy
            xlWorkSheet.UsedRange.Copy(misValue);
    
            // Make a new empty document
            Excel.Workbook xlWorkBook2;
            xlWorkBook2 = xlApp.Workbooks.Add(misValue);
    
            // Select the first sheet and insert
            Excel.Worksheet xlWorkSheet2;
            xlWorkSheet2 = (Excel.Worksheet)xlWorkBook2.Worksheets.get_Item(1);
            xlWorkSheet2.Name = "temp";
    
            // Just copies to range starting at cell A1
            Excel.Range range = (Excel.Range)((Excel.Worksheet)xlWorkSheet2).Cells[1, 1];
            range.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteValues, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, misValue, misValue);
            range.PasteSpecial(Microsoft.Office.Interop.Excel.XlPasteType.xlPasteFormulas, Microsoft.Office.Interop.Excel.XlPasteSpecialOperation.xlPasteSpecialOperationNone, misValue, misValue);
    
            // Adjust width of cells (autofit must be called on a column)
            xlWorkSheet2.Columns.AutoFit();
    
            // Show formulas
            xlWorkSheet2.Application.ActiveWindow.DisplayFormulas = true;
            xlWorkSheet2.Columns.AutoFit();
    
            xlWorkSheet2.PrintPreview(misValue);
    
            // Close Excel
            xlWorkBook2.Close(false, false, misValue);
            xlWorkBook.Close(false, false, misValue);
            xlApp.Quit();
    
    回答了。在我的代码中,它将实现为:

    int numberOfPages = xlWorkSheet2.PageSetup.Pages.Count;
    
    它是有效的