Excel范围的打印尺寸

时间:2019-04-08 20:56:09

标签: c# excel printing page-setup

我有一个Excel工作表,其中所有列均具有相同的宽度,所有行均具有相同的高度。页面设置被设置为适合工作表内容的一页宽,但是它可以超过一页。我希望文档始终打印相同的文档。但是,问题是,取决于计算机和所安装的打印机,有时页面的最后一行最终出现在下一页(或上一页的第一行)上。

我正在用C#编写VSTO插件,以设置工作表的缩放比例和边距,以使指定范围始终适合一页。

该想法是获取工作表的可打印区域,将其与范围大小进行比较以适合一页,然后相应地调整缩放比例和边距。参见下面为解决该问题而编写的代码。

class Rapport
    {
        public static void RapportSetupPage()
        {
            Excel.Application excelObj = Globals.ThisAddIn.Application;
            Excel.Workbook wb = excelObj.ActiveWorkbook;
            Excel.Worksheet ws = wb.ActiveSheet;

            float topMargin= (float)(ws.PageSetup.TopMargin / 72 * 100);
            float botMargin = (float)(ws.PageSetup.BottomMargin / 72 * 100);
            float leftMargin = (float)(ws.PageSetup.LeftMargin / 72 * 100);
            float rightMargin = (float)(ws.PageSetup.RightMargin / 72 * 100);

            RectangleF rangeSize=new RectangleF(0, 0, (float)ws.Range["Impression_Page"].Width / 72 * 100, (float)ws.Range["Impression_Page"].Height / 72 * 100);

            PrinterSettings printerSet = new PrinterSettings();

            foreach (var pname in PrinterSettings.InstalledPrinters)
            {
                if (excelObj.ActivePrinter.Contains(pname.ToString()))
                {
                    printerSet.PrinterName = pname.ToString();
                }
            }

            PageSettings pageSet = new PageSettings(printerSet);
            RectangleF paperSize = new RectangleF();

            foreach (PaperSize psize in printerSet.PaperSizes)
            {
                if ((int)ws.PageSetup.PaperSize==psize.RawKind)
                {
                    pageSet.PaperSize = psize;
                    paperSize.Height = psize.Height;
                    paperSize.Width = psize.Width;         
                }
            }

            RectangleF printableArea = pageSet.PrintableArea;

            //Determine the minimum Margin
            topMargin = Math.Max(topMargin, printableArea.Y);
            botMargin = Math.Max(botMargin, paperSize.Height-printableArea.Bottom);
            leftMargin = Math.Max(leftMargin, printableArea.X);
            rightMargin = Math.Max(rightMargin, paperSize.Width-printableArea.Right );

            if (ws.PageSetup.CenterHorizontally)
            {
                leftMargin = Math.Max(leftMargin, rightMargin);
                rightMargin = leftMargin;
            }

            if (ws.PageSetup.CenterVertically)
            {
                topMargin = Math.Max(topMargin, botMargin);
                botMargin = topMargin;
            }

            printableArea.X = leftMargin;
            printableArea.Y = topMargin;
            printableArea.Width = paperSize.Width - leftMargin - rightMargin;
            printableArea.Height = paperSize.Height - topMargin - botMargin;

            float zoom = 0;

            if ((printableArea.Width/rangeSize.Width)<(printableArea.Height / rangeSize.Height))
            {
                zoom = printableArea.Width / rangeSize.Width;

            }
            else
            {
                zoom = printableArea.Height / rangeSize.Height;
            }


            if (ws.PageSetup.CenterHorizontally)
            {
                leftMargin = (paperSize.Width - rangeSize.Width * zoom)/2;
                rightMargin = leftMargin;
            }
            else
            {
                rightMargin= paperSize.Width - leftMargin - rangeSize.Width * zoom;
            }

            if (ws.PageSetup.CenterVertically)
            {
                topMargin = (paperSize.Height - rangeSize.Height * zoom) / 2;
                botMargin = topMargin;
            }
            else
            {
                botMargin = paperSize.Height - topMargin - rangeSize.Height * zoom;

            }

            ws.PageSetup.TopMargin= topMargin/100*72 ;
            ws.PageSetup.BottomMargin=botMargin/100*72;
            ws.PageSetup.LeftMargin=leftMargin/100*72;
            ws.PageSetup.RightMargin =rightMargin/100*72;
            ws.PageSetup.Zoom = zoom * 100;
            ws.PageSetup.FitToPagesTall = false;
            ws.PageSetup.FitToPagesWide = false;

        }

    }

代码有效,但计算出的左右边距错误。计算出的有关页面高度的所有信息都很好,并且计算出的缩放因子也很完美。但是,当页面的宽度控制缩放因子时,结果不起作用

该范围的宽度/高度比(以磅为单位)似乎与打印结果的宽度/高度比不同。在我的情况下,ws.Range [“ Impression_Page”]。Width = 553.5和ws.Range [“ Impression_Page”]。Height为766.5,比率为0.722。如果我打印工作表并物理测量结果,则比率为0.759。

问题是如何获得以英寸为单位的范围的打印宽度?

0 个答案:

没有答案