c# - Excel 自动化 : Any way of knowing how many pages a sheet will be printed out on?

标签 c# excel

有时纸张太大而无法打印出来,所以如果是这种情况,我想避免打印它。

我目前的计划:

  1. 打开 excel 文档
  2. 复制第一张纸
  3. 创建一个新文档
  4. 在新文档中插入文本和公式
  5. 显示文本时在列上自动调整
  6. 打印文本(尚未实现)
  7. 在显示公式时在列上自动调整
  8. 打印公式(尚未实现)

复制/粘贴的原因是即使文档/工作表/单元格被密码写保护,也能够自动调整列。

但是,例如,如果当前工作表超过 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();

最佳答案

This post回答了它。在我的代码中,它将实现为:

int numberOfPages = xlWorkSheet2.PageSetup.Pages.Count;

而且有效。

关于c# - Excel 自动化 : Any way of knowing how many pages a sheet will be printed out on?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6097861/

相关文章:

excel - 计算一个范围内数字的多个实例

c# - WebAPI Controller 方法未显示在 swagger 中

c# - 实例化具有初始值的类型是否比两个单独的语句便宜?

c# - EXISTS 不引入子查询时,select 列表中只能指定一个表达式

excel - 需要将 Cobol 数据文件转换为 CSV、Excel

java - Spring 在上传到服务器时损坏 Excel(xlsx, xls) 文件 (org.apache.poi)

excel - 使用vba在excel中设置求和公式

c# - 如何遍历 WPF 工具包 Datagrid 的行

C++ 中的 C# 只读(与 const 的细微差别)

python - 将电子表格的列存储在 Python 字典中