【问题标题】:What should Excel Styles.xml look like - OpenXMLExcel Styles.xml 应该是什么样子 - OpenXML
【发布时间】:2017-10-11 09:51:17
【问题描述】:

我正在生成一个大的 excel 文件,并且我正在尝试将数据的标题设置为粗体。

如果我注释掉所有 CellFormat 代码并创建电子表格,则文件会正确创建,但如果我不注释掉这些行,则 Excel 会给我一个错误:Repaired Records: Format from /xl/styles.xml。 (显然我先点了Yes修复文件。)

这是我的代码的样子:

Public Function Create_Spreadsheet_Stylesheet(ByRef stylePart As WorkbookStylesPart) As WorkbookStylesPart
    Dim font1Id As UInt32Value,
        font2Id As UInt32Value

    Dim font1 As New Font With {
        .FontName = New FontName With {.Val = "arial"},
        .FontSize = New FontSize With {.Val = 9}
    }

    Dim font2 As New Font With {
        .Bold = New Bold,
        .FontName = New FontName With {.Val = "arial"},
        .FontSize = New FontSize With {.Val = 9}
    }

    stylePart.Stylesheet = New Stylesheet
    stylePart.Stylesheet.Fonts = New Fonts

    stylePart.Stylesheet.Fonts.Append(font1)
    font1Id = Convert.ToUInt32(stylePart.Stylesheet.Fonts.ChildElements.Count - 1)

    stylePart.Stylesheet.Fonts.Append(font2)
    font2Id = Convert.ToUInt32(stylePart.Stylesheet.Fonts.ChildElements.Count - 1)

    stylePart.Stylesheet.Save()

    Dim cf1 As New CellFormat() With {
        .FontId = font1Id,
        .FillId = 0,
        .BorderId = 0
    }

    Dim cf2 As New CellFormat() With {
        .FontId = font2Id,
        .FillId = 0,
        .BorderId = 0
    }

    stylePart.Stylesheet.CellFormats = New CellFormats ' I would comment this line out
    stylePart.Stylesheet.CellFormats.Append(cf1) ' And this one
    stylePart.Stylesheet.CellFormats.Append(cf2) ' And this one

    stylePart.Stylesheet.Save()

    Return stylePart
End Function

styles.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<x:styleSheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
   <x:fonts>
      <x:font>
         <x:sz val="9" />
         <x:name val="arial" />
      </x:font>
      <x:font>
         <x:b />
         <x:sz val="9" />
         <x:name val="arial" />
      </x:font>
   </x:fonts>
   <x:cellXfs>
      <x:xf fontId="0" fillId="0" borderId="0" />
      <x:xf fontId="1" fillId="0" borderId="0" />
   </x:cellXfs>
</x:styleSheet>

我在代码中做错了什么,或者我必须更改什么才能让 excel 使用 cellFormat。

我在互联网上查看了很多关于如何将单元格加粗的示例,并且我一直在关注本教程:

https://social.msdn.microsoft.com/Forums/windows/en-US/4ae9ba85-d5d2-4ce8-a0ba-dece26ed7d2a/open-xml-sdk-for-making-font-bold?forum=oxmlsdk

【问题讨论】:

    标签: excel vb.net openxml-sdk


    【解决方案1】:

    我认为这里的问题在于,在您的单元格格式中,您引用了 FillId = 0 的填充和 BorderId = 0 的边框。由于您在这里重新创建了样式表:

    stylePart.Stylesheet = New Stylesheet
    

    您的文档中没有此类填充或边框。 您有两种解决方案:

    1. 创建基本填充(白色背景)和基本边框(无 边框)对象并在添加单元格之前将它们添加到您的样式表中 格式,就像您添加字体一样。
    2. 尝试从 CellFormat 的定义中删除对那些 Border 和 Fill 的引用。也许它会起作用。

    请注意,在您下面的示例中,他们使用的是现有的 excel 文件,该文件可能在其样式表中存储了某些边框和填充,因此他们不必这样做。

    【讨论】:

      猜你喜欢
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 2015-12-09
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多