如何从Excel导出到具有特定大小(或纵横比)的图像?

如何从Excel导出到具有特定大小(或纵横比)的图像?,excel,vba,Excel,Vba,我需要将excel工作表导出到具有特定大小(或至少是纵横比)的图像 我通过在页面布局模式中将列和行设置为相同的大小,创建了一个方形网格。 如果我通过脚本(请参见下面的脚本)或仅通过复制粘贴到绘制中导出所需的区域,则其纵横比与我预期的不同 Data from my experiments: Row height: 0.38 cm Column width: 0.38 cm Area: 64 columns x 40 boxes --> Ratio of 1.6:1 Copy/Paste

我需要将excel工作表导出到具有特定大小(或至少是纵横比)的图像

我通过在页面布局模式中将列和行设置为相同的大小,创建了一个方形网格。 如果我通过脚本(请参见下面的脚本)或仅通过复制粘贴到绘制中导出所需的区域,则其纵横比与我预期的不同

Data from my experiments:

Row height: 0.38 cm
Column width: 0.38 cm
Area: 64 columns x 40 boxes --> Ratio of 1.6:1

Copy/Paste to paint:         897 x 601  (1.49:1)
VBA Export (Zoom-coef. 1):  1024 x 680  (1.51:1)
VBA Export (Zoom-coef. 2):  2048 x 1360 (1.51:1)
是什么造成了长宽比上的这种奇怪差异

VBA图像导出脚本:

Sub ExportImage()

    exportpath = "C:\Users\ ..."
    rr = "A1:BL40"
    Set xWs = ActiveWorkbook.ActiveSheet

    'Captures current window view
    sView = ActiveWindow.View

    'Sets the current view to normal so there are no "Page X" overlays on the image
    ActiveWindow.View = xlNormalView

    'Temporarily disable screen updating
    Application.ScreenUpdating = False

    'Selection Print Area
    xWs.PageSetup.PrintArea = xWs.Range(rr).Resize(xWs.Range(rr).Rows.Count, xWs.Range(rr).Columns.Count).Address

    'Export print area as correctly scaled PNG image, courtasy of Winand
    'Lukasz: zoom_coef can be constant = 0 to 5 can work too, but save is 0 to 4

    zoom_coef = 2

    Set area = xWs.Range(xWs.PageSetup.PrintArea)
    area.CopyPicture xlPrinter
    Set chartobj = xWs.ChartObjects.Add(0, 0, area.Width * zoom_coef, area.Height * zoom_coef)
    chartobj.Activate
    chartobj.Chart.Paste
    chartobj.Chart.Export exportpath, "png"
    chartobj.Delete

    'Returns to the previous view
    ActiveWindow.View = sView

    'Re-enables screen updating
    Application.ScreenUpdating = True

End Sub

编辑:问题似乎在于,用完全不同且神秘的单位测量行高和列宽的方法非常愚蠢

这是一个选择吗?我真的很想找到一种方法,首先导出一个精确的大小,而不是调整大小和牺牲质量之后。