asp.net - 无法在 OpenXMl Cell 样式中使用多种字体

标签 asp.net excel vb.net openxml

我在样式表中定义了两种不同的字体,但如果我使用 StyleIndex=1 的第二种样式。我无法打开生成的电子表格。任何帮助将不胜感激

我的代码

    Private Function GenerateStyleSheet() As Stylesheet

        Dim ss As Stylesheet = New Stylesheet()
        Dim fonts1 As Fonts = New Fonts()

        Dim f1 As Font = New Font()
        Dim f1Size As FontSize = New FontSize()
        f1Size.Val = 11D

        f1.Append(f1Size)

        Dim f2 As Font = New Font()
        Dim b2 As Bold = New Bold()
        Dim f2Size As FontSize = New FontSize()
        f2Size.Val = 11D
        f2.Append(b2)
        f2.Append(f2Size)

        fonts1.Append(f1)

        fonts1.Append(f2)
        fonts1.Count = fonts1.ChildElements.Count
        ss.Append(fonts1)
        Return ss

    End Function





Function getBoldTextCell(ByVal cell As String, ByRef row As Row, ByVal val As String) As Row
    Dim refCell As Cell = Nothing
    Dim newCell As New Cell()
    newCell.StyleIndex = 1 // 0 works 
    newCell.CellReference = cell
    row.InsertBefore(newCell, refCell)
    newCell.CellValue = New CellValue(val)
    newCell.DataType = New EnumValue(Of CellValues)(CellValues.String)


    Return (row)
End Function

XML 代码:

<x:row xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:c r="A1" s="0" t="str">
    <x:v>Request #</x:v>
  </x:c>
  <x:c r="B1" t="str">
    <x:v>1</x:v>
  </x:c>
</x:row>

样式代码:

<x:fonts count="2" xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <x:font>
    <x:sz val="11" />
  </x:font>
  <x:font>
    <x:b />
    <x:sz val="11" />
  </x:font>
</x:fonts>

最佳答案

你不能定义这样的样式。您需要创建一个包含 Fonts 、 Fills 、 Borders 的样式,并根据定义的 Fonts 、 Fills 、 Borders 创建单元格格式,如下所示。

    // <Fonts>
    Font font0 = new Font();         // Default font

    Font font1 = new Font();         // Bold font
    Bold bold = new Bold();
    font1.Append(bold);

    Fonts fonts = new Fonts();      // <APENDING Fonts>
    fonts.Append(font0);
    fonts.Append(font1);

    // <Fills>
    Fill fill0 = new Fill();        // Default fill

    Fills fills = new Fills();      // <APENDING Fills>
    fills.Append(fill0);

    // <Borders>
    Border border0 = new Border();     // Defualt border

    Borders borders = new Borders();    // <APENDING Borders>
    borders.Append(border0);

    // <CellFormats>
    CellFormat cellformat0 = new CellFormat() { FormatId = 0, FillId = 0, BorderId = 0 }; 
    CellFormat cellformat1 = new CellFormat(new Alignment() { Horizontal  HorizontalAlignmentValues.Center, Vertical = VerticalAlignmentValues.Center }) { FontId = 1 }; 


    // <APENDING CellFormats>
    CellFormats cellformats = new CellFormats();
    cellformats.Append(cellformat0);
    cellformats.Append(cellformat1);


    // Append FONTS, FILLS , BORDERS & CellFormats to stylesheet <Preserve the ORDER>
    workbookstylesheet.Append(fonts);
    workbookstylesheet.Append(fills);
    workbookstylesheet.Append(borders);
    workbookstylesheet.Append(cellformats);

稍后在定义单元格时,添加样式引用

    cell.StyleIndex=0 ; // Default style
    cell1.StyleIndex=1 ; // Our defined style 1

关于asp.net - 无法在 OpenXMl Cell 样式中使用多种字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27363337/

相关文章:

c# - 如何在 MVC 中的 @Html.ActionLink 中获取更新的文本框更改值

vba - 如何将Excel拆分为多个具有固定行数的工作簿

excel - 使用 vba 插入公式的结果

vb.net - VB 中的 shell 命令

c# - 使用 C# 将列表传递给 <%# Eval ("") %>

c# - IE 不适用于 javascript onclick 事件...适用于 FF 和 Chrome

vb.net - 我正在尝试估计我的 Dictionary(Of String, Long()) 的内存需求

windows - 有没有办法以编程方式将焦点集中到 Windows 中的应用程序?

asp.net - 是否可以在标准 Asp.Net WebForms 应用程序中使用 SSRS 2008 R2 ReportViewer Web 部件?

excel - 下拉列表和单元格格式的条件语句