【问题标题】:How to set Excel margins with EPPlus如何使用 EPPlus 设置 Excel 边距
【发布时间】:2016-01-15 18:06:27
【问题描述】:

我需要能够以编程方式设置 Excel 工作簿的边距(打印机设置)。
我找到了这个文件:

ExcelPrinterSettings.cs

有一个类具有以下构造函数:

ExcelPrinterSettings(XmlNamespaceManager ns,
    XmlNode topNode,ExcelWorksheet ws)

但我不知道前两个参数应该传递什么。
我已经有了制作工作表的代码,所以我可以将它作为第三个参数传递。

非常感谢您的任何建议。

【问题讨论】:

    标签: excel epplus


    【解决方案1】:

    我使用 epplus 和此代码在目标 excel 文件中设置打印机设置:

    ExcelWorksheet ew;
    ew.PrinterSettings.TopMargin = tartetTopMarginValueInCm / 2.54M;
    ew.PrinterSettings.RightMargin = targetRightMarginValueInCm / 2.54M;
    ...
    ew.PrinterSettings.HeaderMargin = targetHeaderMarginInCm / 2.54M;
    

    不要忘记将厘米转换为英寸(如果您想使用厘米,因为所有 epplus 打印机设置值都以英寸为单位)。

    Excel 中的 页面设置 与 Epplus 中的 PrinterSettings 的屏幕:

    【讨论】:

    • 非常感谢。由于代码完成/智能感知,我已经找到了它。它似乎确实有效。我根本没有使用 ExcelPrinterSettings.cs。
    【解决方案2】:

    与上述相同,稍作调整(100% 有效)

    ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
    
    ws.PrinterSettings.TopMargin =(decimal).5 / 2.54M; // narrow border
    ws.PrinterSettings.RightMargin = (decimal).5 / 2.54M; //narrow border
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      相关资源
      最近更新 更多