Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel Styles.xml应该是什么样的-OpenXML_Excel_Vb.net_Openxml Sdk - Fatal编程技术网

Excel Styles.xml应该是什么样的-OpenXML

Excel Styles.xml应该是什么样的-OpenXML,excel,vb.net,openxml-sdk,Excel,Vb.net,Openxml Sdk,我正在生成一个大型excel文件,并试图将数据的标题设置为粗体 如果我注释掉所有的CellFormat代码并创建电子表格,那么文件将正确创建,但是如果我没有注释掉这些行,那么excel会给我一个错误:Repaired Records:Format from/xl/styles.xml。(显然,我单击了Yes首先修复了文件。) 这就是我的代码的样子: Public Function Create_Spreadsheet_Stylesheet(ByRef stylePart As WorkbookS

我正在生成一个大型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

我已经在互联网上看到了很多关于如何加粗单元格的示例,我一直遵循本教程:


我认为这里的问题是,在单元格格式中,您使用
FillId=0
引用Fill,使用
BorderId=0
引用Border。由于您在此处重新创建了样式表:

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

  • 创建基本填充(白色背景)和基本边框(无 边框)对象,并在添加单元格之前将其添加到样式表中 格式,就像添加字体一样
  • 尝试从CellFormat的定义中删除对这些边框的引用并填充。也许它会起作用
  • 请注意,在下面的示例中,他们使用的是一个现有的excel文件,该文件的样式表中可能存储了某些边框和填充,因此他们不必这样做