0
我正在生成一個大型的excel文件,我試圖使數據的標題變爲粗體。Excel Styles.xml應該如何 - OpenXML
如果我註釋掉所有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。
我已經看了很多在互聯網上的例子就如何大膽細胞,我在這裏已經按照本教程: