【问题标题】:Print gridlines in OpenXML Spreadsheet在 OpenXML 电子表格中打印网格线
【发布时间】:2017-10-04 14:36:50
【问题描述】:

我当前的任务包括以编程方式生成具有各种功能的 .xslx 文件,例如自动筛选或使用 Excel 2010 在打印时显示网格线。

但是我未能正确添加网格线的 PrintOptions。
根据MSDN Dokumentation,PrintOptions 是 Worksheet 的叶子,但 DocumentFormat.OpenXml.Spreadsheet.WorkSheet 命名空间不包含附加 PrintOptions 的函数,使用 .Append() 或 .AppendChild() 将导致损坏的电子表格。

Dim po = New PrintOptions With {.GridLines = True}  
sheetPart.Worksheet.Append(po)

我还使用 OpenXML Productivity Tool 将我自己创建的电子表格与 Excel 2010 中的电子表格进行了比较,我注意到电子表格和 Excel 之间的唯一区别是我的电子表格有一个 xml 命名空间,而 Excel 没有。

有人能告诉我将 PrintOptions 插入电子表格的正确方法是什么吗?我现在在这两条线上度过了几天。

【问题讨论】:

    标签: vb.net excel-2010 openxml openxml-sdk


    【解决方案1】:

    来自 Vincent Tan 的 SpreadsheetOpenXmlFromScratch

    Dim po As New PrintOptions()
    po.HorizontalCentered = True
    po.VerticalCentered = True
    ' print the row (1,2,3,...) and column (A,B,C,...) headings
    po.Headings = True
    ' print the grid lines
    **po.GridLines = True
    ' By default, GridLinesSet is true.
    ' Only when both GridLines and GridLinesSet are true, then grid lines are printed.**
    ' I don't know why there's an additional flip switch...
    po.GridLinesSet = True
    ws.Append(po)
    

    您的代码没有显示您处理 GridLinesSet = true

    【讨论】:

    • 遗憾地添加“GridLinesSet = True”没有帮助,工作表仍然损坏。
    【解决方案2】:

    显然有一个必须插入元素的顺序。

    如果您的 Worksheet 中有 PageSetup 元素,则必须在 PrintOptions 之后附加该元素,否则您将在 Office 2010 中获得损坏的电子表格。

    如果您想在 Landscape、FitToWidth 和 Gridlines 中插入电子表格,这是正确的插入方式:

    Dim po = New PrintOptions With {.GridLines = True}
    sheetPart.Worksheet.Append(po)
    
    Dim ps = New PageSetup With {.Orientation = OrientationValues.Landscape} 
    Dim sp As New SheetProperties
    sp.PageSetupProperties = New PageSetupProperties With {.FitToPage = True}
    sheetPart.Worksheet.SheetProperties = sp
    ps.FitToWidth = CUInt(1)
    ps.FitToHeight = CUInt(0)
    sheetPart.Worksheet.Append(ps)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多