如何使用C#在Print Preview Excel中计算打印页数?

问题描述 投票:0回答:1

我想知道高级打印页面的总数。要计算打印前的打印页数,我将进入“打印”以查看计数数。单击文件->打印,然后我可以查看打印的页数。enter image description here

我可以计算一张纸中的打印页数(上图中是2)。但是,当我将打印设置更改为“整个工作簿”时,“打印预览”中的页数将会更改。我不知道如何获取打印页数。enter image description here

Excel.Worksheet esh1 = oWB.Worksheets["List of holidays"];
MessageBox.Show(esh1.PageSetup.Pages.Count.ToString());

您能帮我解决这个问题吗?谢谢。

c# excel office-interop excel-interop
1个回答
0
投票

[您可以尝试这样做,就像艾哈迈德·阿卜杜勒·哈密德(Ahmed Abdelhameed)所说的,只有一个区别-仅对可见纸页计数:]

            Excel.Workbook xlWB = Globals.ThisAddIn.Application.ActiveWorkbook;
            long pagesCount = 0;

            foreach (Excel.Worksheet xlSht in xlWB.Sheets)
            {
                if (xlSht.Visible == Excel.XlSheetVisibility.xlSheetVisible)
                {
                    pagesCount += xlSht.PageSetup.Pages.Count;
                }
            }

            MessageBox.Show(pagesCount.ToString());
© www.soinside.com 2019 - 2024. All rights reserved.