【问题标题】:Interop - Excel to Word copy - Shrink table width to fit Word document page width互操作 - Excel 到 Word 副本 - 缩小表格宽度以适合 Word 文档页面宽度
【发布时间】:2016-09-09 09:17:12
【问题描述】:

我正在将一个单元格范围从 Excel 复制到包含多个表格的 Word。 我需要将我的文档设置为纵向。问题是复制的表格宽度大于 Word 文档页面的宽度。所以我只能看到表格的一部分,另一部分超出了Word文档,因此看不到它。

创建 Word 文档后,我可以手动缩小表格宽度以适应 Word 文档的页面宽度。如何使用 Office Interop 以编程方式实现这一目标?

document.PageSetup.PageWidth = (float)500 不适合我。 我看到有一个属性为AutoFitBehavior:Word.WdAutoFitBehavior.wdAutoFitWindow,但不确定它如何应用于Word文档。

对此有任何帮助吗?

【问题讨论】:

    标签: c# ms-word interop office-interop


    【解决方案1】:

    当您在问题正文中提出一些问题时,我可以回答其中的一些问题:

    我需要将我的文档设置为纵向:

    你应该插入一个分节符然后改变方向:

    doc.Words.Last.InsertBreak(Wd.WdBreakType.wdSectionBreakNextPage);
    doc.Sections.Last.PageSetup.Orientation = orientation;
    

    或者在创建文档后设置方向

    doc.PageSetup.Orientation = WdOrientation.wdOrientLandscape;
    

    创建 Word 文档后,我可以手动缩小表格宽度以适应 Word 文档的页面宽度。如何使用 Office Interop 以编程方式实现这一目标?

    你只需要将 PreferredWidth 的类型设置为 wdPreferredWidthPercent 后将其设置为 100%:

    table.PreferredWidthType = Wd.WdPreferredWidthType.wdPreferredWidthPercent;
    table.PreferredWidth = 100.0f;
    

    我看到有一个属性为AutoFitBehavior:Word.WdAutoFitBehavior.wdAutoFitWindow,但不确定它如何应用于Word文档:

    你可以像这样使用AutoFitBehavior

    table.AllowAutoFit = true;
    table.AutoFitBehavior(Wd.WdAutoFitBehavior.wdAutoFitWindow);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多